Skip to content

Instantly share code, notes, and snippets.

@sheldonhull
Last active January 30, 2021 00:39
Show Gist options
  • Select an option

  • Save sheldonhull/141a5059fc4c4d96f725a2157e189bfa to your computer and use it in GitHub Desktop.

Select an option

Save sheldonhull/141a5059fc4c4d96f725a2157e189bfa to your computer and use it in GitHub Desktop.
[Invoke-Build tasks] useful examples of InvokeBuild tasks to automate all things using AWS SDK or other #powershell
#Requires -Modules PowerShellHumanizer, AWS.Tools.Common, AWS.Tools.SimpleSystemsManagement, InvokeBuild
# Example of using InvokeBuild with Invoking an SSM Automation Document and waiting for a response
# Ideally this would be good to have in seperate functions, but for quick adhoc work this works well.
task ssm-test-aws-ssm-automation {
@(
'Aws.Tools.Common'
'AWS.Tools.SimpleSystemsManagement'
) | ForEach-Object {
try
{
Import-Module $_ -Scope Global
}
catch
{
Write-Build DarkYellow "error noted on trying to import: $($_.Exception.Message.ToString()) . Likely assembly already loaded"
}
}
Import-Module PowershellHumanizer -ErrorAction SilentlyContinue -Force # can't use autoloading per extensions to .net objects
$StopWatch = [diagnostics.stopwatch]::StartNew()
$Params = @{
'InstanceId' = $ENV:INSTANCE_ID
'Param1' = $ENV:PARAM1
'Param2' = $ENV:PARAM2
}
$sendSSMCommandSplat = @{
DocumentName = 'my-aws-ssm-automation-doc'
Parameter = $params
DocumentVersion = '$LATEST'
}
$result = Start-SSMAutomationExecution -Mode Auto @sendSSMCommandSplat
$result | Get-SSMAutomationExecution | Select-Object -ExpandProperty StepExecutions | Select-Object StepName, Action, StepStatus, ValidNextSteps
Write-Build Green "Issued command with following parameters: $(($result | Get-SSMAutomationExecution).Parameters | Format-List | Out-String)"
do
{
$execution = $result | Get-SSMAutomationExecution
Write-Build DarkGray "SSMAutomationExecution: $($execution.AutomationExecutionStatus) $($Execution.CurrentStepName) $($Execution.CurrentAction) $($StopWatch.Elapsed.Humanize())"
Start-Sleep -Seconds 5
}
while ([string]::IsNullOrWhiteSpace($execution.CurrentAction) -eq $false -or $execution.AutomationExecutionStatus -eq 'InProgress')
Write-Build DarkGreen "$($result | Get-SSMAutomationExecution | Select-Object -ExpandProperty StepExecutions | Select-Object StepName, Action, StepStatus, ValidNextSteps,FailureMessage | Format-List -Force | Out-String)"
$result | Get-SSMAutomationExecution | Select-Object -ExpandProperty StepExecutions | ForEach-Object {
$e = $_
Write-Build DarkGray "$($e | Select-Object StepName, Action, StepStatus, ValidNextSteps | Format-Table -AutoSize -Wrap | Out-String)"
Write-Build Green "Output Payload: `n$($e.Outputs.OutputPayload | ConvertFrom-Json | Format-List -Force | Out-String)"
}
if ($result.FailureMessage)
{
Write-Build Red "SSM Failure: $($result.FailureMessage)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment