Skip to content

Instantly share code, notes, and snippets.

@cicorias
Last active April 1, 2021 14:20
Show Gist options
  • Save cicorias/50e415d5f95192c673ba4005c9a44be5 to your computer and use it in GitHub Desktop.
Save cicorias/50e415d5f95192c673ba4005c9a44be5 to your computer and use it in GitHub Desktop.
Make a test file and upload
#!/usr/bin/env pwsh
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]
$subscriptionId,
[Parameter(Mandatory = $true)]
[string]
$resourceGroup,
[Parameter(Mandatory = $true)]
[string]
$storageAccountName,
[Parameter(Mandatory = $true)]
[string]
$storageContainer,
[Parameter(Mandatory = $false)]
[bool]
$useInteractiveLogin,
[Parameter(Mandatory = $false)]
[int]
$retryMax = 5
)
<#PSScriptInfo
.VERSION 1.4
.GUID b787dc5d-8d11-45e9-aeef-5cf3a1f690de
.AUTHOR Adam Bertram
.COMPANYNAME Adam the Automator, LLC
.TAGS Processes
#>
<#
.DESCRIPTION
Invoke-Process is a simple wrapper function that aims to "PowerShellyify" launching typical external processes. There
are lots of ways to invoke processes in PowerShell with Start-Process, Invoke-Expression, & and others but none account
well for the various streams and exit codes that an external process returns. Also, it's hard to write good tests
when launching external proceses.
This function ensures any errors are sent to the error stream, standard output is sent via the Output stream and any
time the process returns an exit code other than 0, treat it as an error.
#>
function Invoke-Process {
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$FilePath,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$ArgumentList
)
$ErrorActionPreference = 'Stop'
try {
if (!(Test-Path 'env:TMP')) {
# WSL TMP or TEMP is not always set
$env:TMP = "/tmp"
}
$stdOutTempFile = Join-Path -Path $env:TMP -ChildPath $((New-Guid).Guid)
$stdErrTempFile = Join-Path -Path $env:TMP -ChildPath $((New-Guid).Guid)
$startProcessParams = @{
FilePath = $FilePath
ArgumentList = $ArgumentList
RedirectStandardError = $stdErrTempFile
RedirectStandardOutput = $stdOutTempFile
Wait = $true;
PassThru = $true;
NoNewWindow = $true;
}
if ($PSCmdlet.ShouldProcess("Process [$($FilePath)]", "Run with args: [$($ArgumentList)]")) {
$cmd = Start-Process @startProcessParams
$cmdOutput = Get-Content -Path $stdOutTempFile -Raw
$cmdError = Get-Content -Path $stdErrTempFile -Raw
if ($cmd.ExitCode -ne 0) {
if ($cmdError) {
throw $cmdError.Trim()
}
if ($cmdOutput) {
throw $cmdOutput.Trim()
}
}
else {
if ([string]::IsNullOrEmpty($cmdOutput) -eq $false) {
Write-Output -InputObject $cmdOutput
}
}
}
}
catch {
$PSCmdlet.ThrowTerminatingError($_)
}
finally {
Remove-Item -Path $stdOutTempFile, $stdErrTempFile -Force -ErrorAction Ignore
}
}
.\generateTestData.ps1 > sampleTestData.json
gzip --force sampleTestData.json
if ($useInteractiveLogin) {
# only need to login if have not already
az login
}
$ipAllowed = $false
try {
Invoke-Process -FilePath az -ArgumentList "account set --subscription $subscriptionId"
# get the IP address this machine uses on the internet -- this site like ident.me provides a simple IP address return
$ipaddress = (Invoke-WebRequest -uri "https://api.ipify.org/").Content
Write-Host "adding $ipaddress to allow"
Invoke-Process -FilePath az -ArgumentList "storage account network-rule add -g $resourceGroup --account-name $storageAccountName --ip-address $ipaddress"
$result = Invoke-Process -FilePath az -ArgumentList "storage account show -n $storageAccountName --query networkRuleSet.ipRules[]"
if ($result.Contains($ipaddress)) {
Write-Host "** found ip address $ipaddress **"
} else {
Write-Host "DID NOT FIND IP $ipaddress ADDRESS IN LIST"
$PSCmdlet.ThrowTerminatingError("ip rule not showing $ipaddress")
}
$ipAllowed = $true
Write-Host "getting storage keys"
$storageAccountKey = Invoke-Process -FilePath az -ArgumentList "storage account keys list -g $resourceGroup -n $storageAccountName --query [0].value -o json"
$tries = $retryMax
while( $tries -gt 0) {
$tries -= 1
try {
Write-Host "ensuring container created"
Invoke-Process -FilePath az -ArgumentList "storage container create -n $storageContainer --account-key $storageAccountKey --account-name $storageAccountName"
Write-Host "uploading blob"
Invoke-Process -FilePath az -ArgumentList "storage blob upload -f sampleTestData.json.gz -c $storageContainer -t Block -n $((New-Guid).Guid).json --account-key $storageAccountKey --account-name $storageAccountName"
break
} catch {
Write-Host "tries $tries failed for reason $_"
Start-Sleep -Seconds 2
}
}
} catch {
Write-Host "failed for reason $_"
$PSCmdlet.ThrowTerminatingError($_)
} finally {
if ($ipAllowed) {
Write-Host "removing allow for $ipaddress"
Invoke-Process -FilePath az -ArgumentList "storage account network-rule remove -g $resourceGroup --account-name $storageAccountName --ip-address $ipaddress"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment