Skip to content

Instantly share code, notes, and snippets.

@lantrix
Created March 14, 2015 08:50
Show Gist options
  • Select an option

  • Save lantrix/8762f17fcd704913bb2d to your computer and use it in GitHub Desktop.

Select an option

Save lantrix/8762f17fcd704913bb2d to your computer and use it in GitHub Desktop.
Individually tag a bunch of AWS items - error handling per item (less efficient as not one operation)
$a = Get-EC2NetworkInterface -Region ap-southeast-2
$tag = New-Object Amazon.EC2.Model.Tag
$tag.Key = "Environment"
$tag.Value = "Dev"
$count = 0
$throwError =""
foreach ($i in $a) {
Clear-Variable -Name throwError #Reset Failure Variable
$count++
if ($count -gt 0) {
Write-Host -ForegroundColor Cyan -NoNewline "$count of $($a.Count) : "
write-host -NoNewline $i.NetworkInterfaceId
try {
New-EC2Tag -Tag $tag -Resource $i.NetworkInterfaceId
} catch {
#Catch throw test
$throwError = $_
} finally {
if ([bool]$throwError) {
Write-Host -ForegroundColor Red $throwError
pause
} else {
Write-Host -ForegroundColor Green "...tagged"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment