I am trying to add a new EC2 Tag with PowerShell to multiple instances at once. I have all of my instances tagged with the format
name : $Env_$Role_$Number
However, I failed to put a separate Environment: Production
tag for each instance which I need now. I ran to following in PowerShell to get a list of the instances that I need in a table.
$ProdTag = New-Object Amazon.EC2.Model.Filter
$ProdTag.Name = "tag:Name"
$ProdTag.Values = "PROD*"
$Instances = New-Object Amazon.EC2.Model.Filter
$Instances.Name = "tag:resource-type"
$ProdTag.Values = "instance"
Get-EC2Tag -Filter ($ProdTag, $Instances)
This outputs the list of instances I want. How would I grab the ResourceType
column, store it in an array and apply New-EC2Tag -tag (Name="tag: Environment, Value="Production:)
to that array?
I realized that I could store Get-EC2Tag -Filter ($ProdTag, $Instances)
as an object in Powershell: $ProdInstances = (Get-EC2Tag -Filter ($ProdFilter, $InstanceFilter)).ResourceID
Then I created a $ProdEnvTag
object to apply Environment:Production
as a tag to everything in the $ProdInstances
object: $ProdInstances | ForEach-Object {New-EC2Tag -Resource $_ -Tag $ProdEnvTag}
$ProdInstances | ForEach-Object {New-EC2Tag -Resource $_ -Tag $ProdEnvTag}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.