0

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 list 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
    }
}
  • 1
    How about if you change the `Out-File` line to `$VM | Out-file $exportto -Append` ? – Balthazar Jun 25 '21 at 16:58
  • 1
    @Balthazar I answered the question below. I'm gonna try yours too – witchkinkofAngmar Jun 25 '21 at 17:17
  • don't, yours is better – Balthazar Jun 25 '21 at 17:25
  • 1
    You can also one-line it `$VMs | Where-Object { $_.Tag.Name -notcontains "testtag" } | Out-File $ExportTo -Append` – SimonS Jun 28 '21 at 14:21
  • @SimonS thanks, that seems to be the fastest way yet. Any idea how to just get the hostname though? This is all in one column: `vm_name PoweredOn 2 8.000`. It would be preferential to just have the vm_name. – witchkinkofAngmar Jun 29 '21 at 17:40
  • yes, you can simply `Select-Object` it. after the `Where-Object` stuff you can do `| Select-Object Property1, Property2, Property3 | Out-File $ExportTo -Append` just change the Property names to the ones you want. btw as the accepted answer suggests, `Export-Csv` would be better than `Out-File` if you want a csv in the end. – SimonS Jun 30 '21 at 09:15

2 Answers2

0

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
0

Answer from another section:

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
    }
}