how to create new repository in github via powershell
Function New-GitHubRepository {
[cmdletbinding(SupportsShouldProcess)]
Param(
[Parameter(Position = 0, Mandatory, HelpMessage = "Enter the new repository name")]
[ValidateNotNullorEmpty()]
[string]$Name,
[string]$Description,
[switch]$Private,
[switch]$NoWiki,
[switch]$NoIssues,
[switch]$NoDownloads,
[switch]$AutoInitialize,
#license templates found at https://github.com/github/choosealicense.com/tree/gh-pages/_licenses
[ValidateSet("MIT","apache-2.0","gpl-3.0","ms-pl","unlicense")]
[string]$LicenseTemplate,
[Alias("token")]
[ValidateNotNullorEmpty()]
[string]$UserToken = $gitToken,
#write full native response to the pipeline
[switch]$Raw
)
Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)"
#display PSBoundparameters formatted nicely for Verbose output
[string]$pb = ($PSBoundParameters | Format-Table -AutoSize | Out-String).TrimEnd()
Write-Verbose "[BEGIN ] PSBoundparameters: `n$($pb.split("`n").Foreach({"$("`t"*2)$_"}) | Out-String) `n"
#create the header
$head = @{
Authorization = 'Basic ' + $UserToken
}
#create a hashtable from properties
$hash = @{
name = $Name
description = $Description
private = $Private -as [boolean]
has_wiki = (-Not $NoWiki)
has_issues = (-Not $NoIssues)
has_downloads = (-Not $NoDownloads)
auto_init = $AutoInitialize -as [boolean]
}
if ($LicenseTemplate) {
$hash.add("license_template",$LicenseTemplate)
}
$body = $hash | ConvertTo-Json
Write-Verbose "[PROCESS] Sending json"
Write-Verbose $body
#define parameter hashtable for Invoke-RestMethod
$paramHash = @{
Uri = "https://api.github.com/user/repos"
Method = "Post"
body = $body
ContentType = "application/json"
Headers = $head
UseBasicParsing = $True
DisableKeepAlive = $True
}
#should process
if ($PSCmdlet.ShouldProcess("$name [$description]")) {
$r = Invoke-RestMethod @paramHash
if ($r.id -AND $Raw) {
Write-Verbose "[PROCESS] Raw result"
$r
} elseif ($r.id) {
write-Verbose "[PROCESS] Formatted results"
$r | Select-Object @{Name = "Name";Expression = {$_.name}},
@{Name = "Description";Expression = {$_.description}},
@{Name = "Private";Expression = {$_.private}},
@{Name = "Issues";Expression = {$_.has_issues}},
@{Name = "Wiki";Expression = {$_.has_wiki}},
@{Name = "URL";Expression = {$_.html_url}},
@{Name = "Clone";Expression = {$_.clone_url}}
} else {
Write-Warning "Something went wrong with this process"
}
if ($r.clone_url) {
$msg = @"
To push an existing local repository to Github run these commands:
-> git remote add origin $($r.clone_url)"
-> git push -u origin master
"@
Write-Host $msg -ForegroundColor Green
}
}
Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)"
}
3.75
8
Function New-GitHubRepo{
<#
.SYNOPSIS
Creates a new remote repository in GitHub
.DESCRIPTION
Creates a new remote repository in GitHub
.PARAMETER UserName
GitHub Username
.PARAMETER ProjectName
Name for the new remote GitHub repository
.EXAMPLE
New-GitHubRepo -UserName GUser -ProjectName "MyRepo"
.NOTES
Author: Michael Heath
Date: 04/27/2019
#>
Param(
[Parameter(Mandatory = $true)][String]$UserName,
[Parameter(Mandatory = $true)][String]$ProjectName
)
# This works for entering password
# New output file
$OutFile = ".\ex.cmd"
# Var for Quote
$q = [char]34
# Create part of the command line with the project name
$t = "$q{\$q @@ name\$q @@ :\$q @@ $ProjectName\$q}$q"
# Remove the space and the @@ symbols
$t = $t.replace(" @@ ", "")
# Add the curl command and the project
$t = "curl -u $UserName https://api.github.com/user/repos -d " + $t
# put contents in the ex.cmd file
"@echo off" | out-file $OutFile -Encoding ascii
$t | Out-File $OutFile -Encoding ascii -Append
# Execute the ex.cmd file - you will be prompted for your password
cmd.exe /C ".\ex.cmd"
Thank you!
8
0
Are there any code examples left?
New code examples in category Shell/Bash
-
Shell/Bash 2022-03-27 22:30:33 wise highlight ubuntu
-
Shell/Bash 2022-03-27 22:25:19 how to send a pull request in git
-
Shell/Bash 2022-03-27 21:55:06 how to open terminal in sublime text ubuntu
-
Shell/Bash 2022-03-27 21:25:19 how to switch branch
-
Shell/Bash 2022-03-27 21:20:25 python
-
Shell/Bash 2022-03-27 21:20:04 set user as admin gitlab
-
Shell/Bash 2022-03-27 21:10:09 apache2 mod_jk proxy to tomcat connector
-
Shell/Bash 2022-03-27 20:35:12 chown
-
Shell/Bash 2022-03-27 20:15:08 awk last match