PowerShell Snippets: Prompt for Multiple Inputs

PowerShellThis post is part of the series on PowerShell Snippets.

Over the last two posts of this series, I’ve shown PowerShell cmdlets which prompt for user input and prompt for secure entry of passwords. A PowerShell script can prompt for multiple inputs as well as output text to screen.

When I originally posted about the Write-Output cmdlet, I only showed how it could be used to output text; you can also use a couple of parameters with this command to change the foreground or background colour.

The below script example, shows two outputs to screen using these parameters as well as prompting the user to input two pieces of data:

Write-Host "Please enter details of the plugin release" -ForegroundColor Green

$ghRepository = Read-Host "Please enter the repository name"

$ghTag = Read-Host "Please enter the tag"

Write-Host "$ghRepository $ghTag will be released." -ForegroundColor Yellow -BackgroundColor Black

PowerShell Snippets: Prompt User for Password

PowerShellThis post is part of the series on PowerShell Snippets.

In this last post, in this series, I showed a PowerShell snippet which would prompt a user for input. This is fine if you are prompting for a username, filename, version tag or similar, but if you are prompting for a password, you would not want to expose to people watching over the users shoulder.

The Read-Host cmdlet has a parameter which will allow for secure prompting of passwords:

$Password = Read-Host "Enter your password" -AsSecureString

Adding the AsSecureString parameter will prompt for the password with a popup input box which obscures the password during entry.

PowerShell Snippets: Prompt for User Input

PowerShellThis post is part of the series on PowerShell Snippets.

The following PowerShell command will prompt the user to input some text which is then stored in the $ghTag parameter for later use:

$ghTag = Read-Host "Please enter the tag"

PowerShell Snippets: Get Numeric Value from String

PowerShellThis post is part of the series on PowerShell Snippets.

In the previous post, I showed how a single element could be retrieved from a GitHub release by executing curl and selecting the required element.

I quickly discovered that the element retrieved in this was contained some additional characters and I needed only the numeric ones. The PowerShell below will extract the numeric elements of the $id variable and return them in the $releaseID variable:

$releaseID = $id -replace "[^0-9]" , ''

PowerShell Snippets: Get One Value from GitHub Json

PowerShellThis post is part of the series on PowerShell Snippets.

While looking into uploading a release asset to a GitHub release, I discovered that to do this I needed the internal numeric id for a release and not just the id I’d given it. The element I needed from the GitHub json was id which can be retrieved using the below PowerShell which executes curl and then selects a single element:


$ghAuthorizationToken = "ghp_authorizationtoken"

$ghUser="username"
$ghRepo="repositoryname"
$ghTag="v1.4.2"

$URL= "https://api.github.com/repos/$ghUser/$ghRepo/releases/tags/$ghTag"

$Json=(curl.exe $URL)|ConvertFrom-json

$id=$Json| Select id

PowerShell Snippets: Use Curl

PowerShellThis post is part of the series on PowerShell Snippets.

The following PowerShell command will execute curl passing in the authorization token and json parameters defined above the curl statement.

The main difference between this and curl on a normal command line is that you need to do curl.exe which is a built in alias for the Invoke-WebRequest cmdlet:

$ghAuthorizationToken = "ghp_authorizationtoken"
$ghUser = "username"
$ghRepo = "repository name"

$json = "
    { \`"tag_name\`": \`"$ghTag\`", \`"target_commitish\`": \`"main\`", \`"name\`": \`"$ghTag\`", \`"body\`": \`"$ghTag release\`", \`"draft\`" :false, \`"prerelease\`": false, \`"generate_release_notes\`": false }
"

curl.exe -X POST -H "Accept: application/vnd.github+json" -H "Authorization: token $ghAuthorizationToken" https://api.github.com/repos/$ghUser/$ghRepo/releases -d "$json"

The variables at the top need to be changed to those for your GitHub account and repository.

PowerShell Snippets: Format Json for Curl

PowerShellThis post is part of the series on PowerShell Snippets.

The following PowerShell shows the format required for use in Curl (which I will cover in the next post of this series). It took me a number of attempts to get the format correct; the backticks (`) are needed to escape the double quotes (") for PowerShell and the backslashes (\) to escape them for the curl command:

$json = "
    { \`"tag_name\`": \`"$ghTag\`", \`"target_commitish\`": \`"main\`", \`"name\`": \`"$ghTag\`", \`"body\`": \`"$ghTag release\`", \`"draft\`" :false, \`"prerelease\`": false, \`"generate_release_notes\`": false }
"

PowerShell Snippets: Run Application (Such as 7-zip)

PowerShellThis post is part of the series on PowerShell Snippets.

The following PowerShell command will execute a Windows application supplying a number of parameters; in this case I am using 7-zip to compress a single one of my ClassicPress plugins to a releases folder (part of this command came from a post I did on compressing all sub folders individually:

$releaseFileName = ".\Releases\$ghRepo.zip"

& "C:\Program Files\7-Zip\7z.exe" a "-xr!.git\" "-xr!*~" $releaseFileName "*"

PowerShell Snippets: Delete File

PowerShellThis post is part of the series on PowerShell Snippets.

The following PowerShell command will check if the named file exists in the current directory and, if so, delete it (replace the highlighted section with the name of the file to be deleted):

$FileName = "azrcrv-smtp.zip"

if (Test-Path $FileName) {
  Remove-Item $FileName
}

Git Command Line Snippets: Upload Release Asset

GitHubThis post is part of the series on Git Command Line Snippets where I am taking a look at performing actions on GitHub using the git command line.

Now that we have the json from GitHub for the release, we can upload the release zip (an extra asset) which is used for installing the plugin on a ClassicPress site:

curl -X POST --header "Authorization: token ghp_authorizationtoken" --header "Content-Type: application/zip" --upload-file "path and filename" "https://uploads.github.com/repos/username/repository name/releases/tag id/assets?name=filename"

The highlighted sections need to be replaced with the parameters for your GitHub account, repo and the release.