Skip to content

Instantly share code, notes, and snippets.

@cicorias
Last active April 13, 2021 23:17
Show Gist options
  • Save cicorias/3568e4063c9f2e646d09e0305aba905a to your computer and use it in GitHub Desktop.
Save cicorias/3568e4063c9f2e646d09e0305aba905a to your computer and use it in GitHub Desktop.

PowerShell things

$c = 1..5
$c | ForEach-Object { Start-Job -ScriptBlock {Start-Sleep -Seconds 2; Write-Host "Job $_ doing something"} -Name "asajob-Job-$_" }
Write-Host "lastexitcode $LASTEXITCODE"
$loopWaitingTimeSeconds = 5
Write-Host "##[section]Waiting on jobs to pass Running State..."
do {
$stillRunning = (Get-Job -State Running).Count
Start-Sleep -Seconds $loopWaitingTimeSeconds
Get-Job -State Running | Select-Object Name, PSBeginTime, JobStateInfo.State, @{Name="RunTime";Expression={ (Get-Date) - $_.PSBeginTime}} | Write-Host
Write-Host "---------------------------"
} while ($stillRunning -gt 0)
Write-Host "lastexitcode $LASTEXITCODE"
Write-Host "##[section]Checking on Reason code if any Failed jobs..."
Get-Job -State Completed | Select-Object JobStateInfo.State
Write-Host "lastexitcode $LASTEXITCODE"
Write-Host "##[section]For all jobs emit the results and remove the job..."
# Get-Job | Receive-Job -Wait -WriteJobInResults -AutoRemoveJob -ErrorVariable myerror -OutVariable myout # | Out-File -FilePath asajobs.out -Force -Width 200 -Append
# NOTE: ENABLE the following line to SEE the error message details
# $lastJob = Start-Job -ScriptBlock {Get-Job -Name "asajob*" | Receive-Job -Wait -AutoRemoveJob -WriteJobInResults} -Name cleanupJob
# Receive-Job -Job $lastJob -Wait -WriteJobInResults -AutoRemoveJob
# $jobs = Get-Job
# foreach ($item in $jobs) {
# Receive-Job -Job $item -Wait -AutoRemoveJob
# }
# Write-Host "##[section]For all jobs emit the results and remove the job..."
# Get-Job | Write-Host "$($_.Name) results:"; Receive-Job -Job $_ -Wait -AutoRemoveJob | Write-Host
Write-Host "lastexitcode $LASTEXITCODE"
$jobs = [ordered]@{}
function AddJob {
param (
[string]
$name,
[int]
$priority,
[string]
$command,
[string]
$argumentList
)
$jobs.Add($name, @{"priority" = $priority; "command" = $command; "args" = $argumentList} )
}
AddJob "two" 2 "az" "account list -o tsv"
AddJob "one" 1 "az" "account list -o tsv"
AddJob -name "three" -priority 1 -command "az" -argumentList "account list -o tsv"
Start-Job
Write-Host $jobs["one"]
function Invoke-Process($FilePath, $ArgumentList) {
Write-Host $ArgumentList
$a = $ArgumentList -replace "`n"," " -replace "`r"," "
Write-Host $a
}
Invoke-Process -FilePath "az" -ArgumentList "foo ""Microsoft.StreamAnalytics/StreamingJobs"" bar"
#https://ss64.com/ps/syntax-scriptblock.html
$Message = "Output:"
# $NewScriptBlock = [scriptblock]::Create("$using:Message $_ `
# Start-Sleep 1")
$NewScriptBlock = {
$var1 = $using:Message
"$var1 - $_"
Start-Sleep 1
}
$Message = "NewOutput:"
1..8 | ForEach-Object -Parallel $NewScriptBlock -ThrottleLimit 10
# 1..8 | ForEach-Object -Parallel {
# "$using:Message $_"
# Start-Sleep 1
# } -ThrottleLimit 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment