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
    }
}
6
  • 1
    How about if you change the Out-File line to $VM | Out-file $exportto -Append ?
    – Balthazar
    Jun 25, 2021 at 16:58
  • 1
    @Balthazar I answered the question below. I'm gonna try yours too Jun 25, 2021 at 17:17
  • don't, yours is better
    – Balthazar
    Jun 25, 2021 at 17:25
  • 1
    You can also one-line it $VMs | Where-Object { $_.Tag.Name -notcontains "testtag" } | Out-File $ExportTo -Append
    – SimonS
    Jun 28, 2021 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. Jun 29, 2021 at 17:40

2 Answers 2

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .