gbouziden
Enthusiast
Enthusiast

How to loop through VMs without a specific tag with powercli

Jump to solution

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)
0 Kudos
1 Solution

Accepted Solutions
gbouziden
Enthusiast
Enthusiast

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

0 Kudos
4 Replies
a_p_
Leadership
Leadership

Moderator note: Moved to VMware PowerCLI Discussions

0 Kudos
gbouziden
Enthusiast
Enthusiast

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

0 Kudos
LucD
Leadership
Leadership

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

@LucDThat works great

0 Kudos