Created
March 14, 2015 08:50
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $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