VMware Cloud Community
rxjoseph
Enthusiast
Enthusiast
Jump to solution

Need some help to Check Tags of VMs

Hi LucD

Firstly i would like to tank you for all the excellent work you for for the community and help thousands of people with scripting. Hats off for your help

I am writing a script to Tag VMs which are SRM placeholders 

I have put an example here 

$Tag = Get-Tag -name 'SRMplaceholder' -Category 'Veeam'

## Select SRM Placeholder VMs only

$SRMplaceholderVMS= Get-VM | where {$_.ExtensionData.Config.ManagedBy.extensionKey -like "com.vmware.vcDr*" -and $_.ExtensionData.Config.ManagedBy.Type -ieq 'placeholderVm'}


foreach ($SRMplaceholderVM in $SRMplaceholderVMS ) {

 

New-TagAssignment -Entity $SRMplaceholderVM  -Tag $Tag


}

 

The script works ok, however some of the VMs have already got the SRM Placeholder tags so i don't need to Tag those Vms again

I need to simple check if a VM already has a Tag with the tag name 'SRMplaceholder' if so i can skip and go to the next VM and only tag the VMs which doesn't have a SRMplaceholder Tag

I am struggling to do a check as its not checking it properly

Appreciate your help

 

Many thanks

 

RXJ

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do something like this.
If the Tag is not found it is assigned

$Tag = Get-Tag -name 'SRMplaceholder' -Category 'Veeam'
## Select SRM Placeholder VMs only
$SRMplaceholderVMS= Get-VM | where {$_.ExtensionData.Config.ManagedBy.extensionKey -like "com.vmware.vcDr*" -and $_.ExtensionData.Config.ManagedBy.Type -ieq 'placeholderVm'}

foreach ($SRMplaceholderVM in $SRMplaceholderVMS ) {
    if(-Not (Get-TagAssignment -Entity $SRMplaceholderVM -Category 'Veeam' | where{$_.Tag -eq $Tag})){
        New-TagAssignment -Entity $SRMplaceholderVM  -Tag $Tag
    }
}

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

You could do something like this.
If the Tag is not found it is assigned

$Tag = Get-Tag -name 'SRMplaceholder' -Category 'Veeam'
## Select SRM Placeholder VMs only
$SRMplaceholderVMS= Get-VM | where {$_.ExtensionData.Config.ManagedBy.extensionKey -like "com.vmware.vcDr*" -and $_.ExtensionData.Config.ManagedBy.Type -ieq 'placeholderVm'}

foreach ($SRMplaceholderVM in $SRMplaceholderVMS ) {
    if(-Not (Get-TagAssignment -Entity $SRMplaceholderVM -Category 'Veeam' | where{$_.Tag -eq $Tag})){
        New-TagAssignment -Entity $SRMplaceholderVM  -Tag $Tag
    }
}

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

rxjoseph
Enthusiast
Enthusiast
Jump to solution

Hi LucD

 

Thank you, something new I learnt today and as always appreciate your help 

Many thanks

 

RXJ

 

0 Kudos