VMware Cloud Community
gbouziden
Enthusiast
Enthusiast
Jump to solution

How to loop through VMs without a specific tag with powercli

I'm trying to export a CSV file that has a list of all VM's in a cluster that doesn't have a specific tag I'm using for rightsizing. However, the CSV isn't populating with anything other than this: ÿþ

 

Get-Module -Name VMware* -ListAvailable | Import-Module -Force
$exportto = "C:\Users\username\Desktop\rightSizingFilter3.csv"
$VMs = Get-Cluster -name clustername | Get-VM
 
foreach ($VM in $VMs){
    If (((Get-Tagassignment $VM).Tag.Name -notcontains "testtag")){
         Out-file $exportto -Append
    }
}

 

 

Tags (2)
Reply
0 Kudos
1 Solution

Accepted Solutions
gbouziden
Enthusiast
Enthusiast
Jump to solution

I got it to work from the snippet below: 

$RS = foreach ($VM in $VMs){
    If (((Get-Tagassignment $VM).Tag.Name -notcontains "testtag")){
        Write-Output $VM
    }
}
$RS | Out-file $exportto -Append

 

View solution in original post

Reply
0 Kudos
4 Replies
a_p_
Leadership
Leadership
Jump to solution

Moderator note: Moved to VMware PowerCLI Discussions

Reply
0 Kudos
gbouziden
Enthusiast
Enthusiast
Jump to solution

I got it to work from the snippet below: 

$RS = foreach ($VM in $VMs){
    If (((Get-Tagassignment $VM).Tag.Name -notcontains "testtag")){
        Write-Output $VM
    }
}
$RS | Out-file $exportto -Append

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do something like this

$exportto = "C:\Users\username\Desktop\rightSizingFilter3.csv"

Get-Cluster -Name clustername |
Get-VM |
Get-TagAssignment |
where{$_.Tag.Name -notmatch 'testtag'} |
Select @{N='VM';E={$_.Entity.Name}},
  @{N='Tag';E={$_.Tag.ToString()}} |
Export-Csv -Path $exportto -UseCulture -NoTypeInformation


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

gbouziden
Enthusiast
Enthusiast
Jump to solution

@LucDThat works great

Reply
0 Kudos