Score:2

Export a list of VMs without a specific tag with PowerCLI

mx flag

I'm trying to export a CSV file that has a list of all VM's in a cluster that don't have a specific tag that 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
    }
}
Score:3
gg flag

If you want a CSV, you may get more consistent results by changing Out-File ... to be:

Export-Csv -InputObject $VM -Path $ExportTo -Append -NoTypeInformation

I believe the output of Get-VM is a JSON object, so outputting as a file may be causing the format to not be what you're looking for. Even with Export-Csv, you may still find that some of the data isn't readily converted to CSV format, so you can clean up the output even more by selecting just the tags or attributes that you want and then exporting that all to CSV.

Here's the code that I tested:

Get-Module -Name VMware* -ListAvailable | Import-Module -Force
Connect-ViServer -Server [SERVERNAME] -Credential (Get-Credential)
$ExportTo = ".\rightSizingFilter3.csv"
$VMs = Get-Cluster -Name [CLUSTERNAME] | Get-VM
 
foreach ($VM in $VMs) {
    If ( ((Get-Tagassignment $VM).Tag.Name -notcontains "testtag") ) {

         Export-Csv -InputObject $VM -Path $ExportTo -Append -NoTypeInformation
    }
}
witchkinkofAngmar avatar
mx flag
yeah that works great. thanks for the help
Score:1
mx flag

I also 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
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.