VMware Cloud Community
Syncr0s
Contributor
Contributor
Jump to solution

VM List on conditions to be generated.

Hi All,

I am trying to create a list of Windows VMs where I can do, but any export is filled with empty lines. Regardless of whether I use Export-CSV or Out-File when a VM is not Windows I get an empty line. I am trying to get a list to use as a Get-Content for another script to query the Guest OS using WMI.

The question is, is there an easy way to eliminate the empty lines?

Below is my code, and I just exchange Out-File for Export-CSV in my attempts. Out-File is better I think so I don't have the "" to contend with in my import of data in the other script.

$allvms = @()

$vmlist = Get-Vm | where {$_.PowerState -eq "PoweredOn"}

ForEach ($vm in $vmlist){

    $vmv = $vm | Get-View 

    $vms = "" | Select Name #, OS   

    if ($vmv.Config.GuestFullName -match "Windows"){

        $vms.Name = $vm.name

        }

    else{

    }

    $allvms += $vms

}

$allvms | Out-File "D:\temp\computers.txt"

I could probably get this script to call the WMI query data in this script, but as I will be generating several sets of computer names from other sources too, I want to run the scripts necessary.

Geoff

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Can't you just update the script like this

$allvms = @()

$vmlist = Get-Vm | where {$_.PowerState -eq "PoweredOn"}

ForEach ($vm in $vmlist){

    $vmv = $vm | Get-View 

    $vms = "" | Select Name #, OS   

    if ($vmv.Config.GuestFullName -match "Windows"){

        $vms.Name = $vm.name

        $allvms += $vms

    }

}

$allvms | Out-File "D:\temp\computers.txt"


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

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Although the script could be optimised, it returns results, and they are written to the file.

Are you sure there VMs that fit the 2 conditions (powered on and running a Windows guest OS) ?

Is there anything in the $allvms array ?


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

0 Kudos
Syncr0s
Contributor
Contributor
Jump to solution

It does return results which are written to the file.

In fact it actually returns the correct results, which is a bonus. The file looks like

Name                                                                                                                        
----                                                                                                                        
10290                                                                                                                  
                                                                                                                            
07920                                                                                                                  
08210                                                                                                                  
09914                                                                                                                  

25137

It actually seems to have extra carriage returns which don't show here, or 'notepad', but show in 'notepad++'.

The gaps are what I want to eliminate easily if possibly. I know I can read through the array and write the information if it isn't 'Null' to another array, but that is tedious, but if it is necessary, and I will put that in the script.

Geoff

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you also get those blank lines when you change the last line to

$allvms | Export-Csv "D:\temp\computers.csv" -NoTypeInformation -UseCulture


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

0 Kudos
Syncr0s
Contributor
Contributor
Jump to solution

LucD wrote:

Do you also get those blank lines when you change the last line to

$allvms | Export-Csv "D:\temp\computers.csv" -NoTypeInformation -UseCulture

Yes, I get the same thing. I was using -NoTypeInformation previously, but that just eliminates the top row.

I can't copy and paste the results into here as it seems the editor window is too smart and removes the extra line, but it shows in notepad and wordpad.

The difference obivously are the header lines between out-file and export-csv.

Geoff

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I mean, do you also get the blank lines when you open the CSV file with Excel ?


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

0 Kudos
Syncr0s
Contributor
Contributor
Jump to solution

Yes. The same thing.

I can accept why. The array which gets created has elements which are unset. Whether that is blank or null is not really for argument, as it is behaving as I expect it would. It is just a matter of manipulating the results, and I was keen to know if anyone was aware of a command which can do it.

It seems as though I will need to use the method to write the array to another array without the blanks, or even to call the WMI script directly and write that information out using a higher placed Select statement.

Geoff

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can't you just update the script like this

$allvms = @()

$vmlist = Get-Vm | where {$_.PowerState -eq "PoweredOn"}

ForEach ($vm in $vmlist){

    $vmv = $vm | Get-View 

    $vms = "" | Select Name #, OS   

    if ($vmv.Config.GuestFullName -match "Windows"){

        $vms.Name = $vm.name

        $allvms += $vms

    }

}

$allvms | Out-File "D:\temp\computers.txt"


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

0 Kudos
Syncr0s
Contributor
Contributor
Jump to solution

Brilliantly simple!

I cannot believe that just moving the array increment up one loop level has resolved the problem.

Just for anyone else thinking of the same thing, the full PS1 file is.

If ((Get-PSSnapin "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null) {

Add-PSSnapin "VMware.VimAutomation.Core"

}

$VCServer = Read-Host "Enter the vCenter server name"

$Username= Read-Host "Enter the username"

$Password = Read-Host "Enter password"

Connect-VIServer $VCServer -User $username -Password $password -port 443

$allvms = @()

$vmlist = Get-Vm | where {$_.PowerState -eq "PoweredOn"}

ForEach ($vm in $vmlist){

    $vmv = $vm | Get-View

    $vms = "" | Select Name  

    if ($vmv.Config.GuestFullName -match "Windows"){

        $vms.Name = $vm.name

        $allvms += $vms

        }

}

$allvms | Out-File "D:\temp\computers.txt"

Disconnect-VIserver -Confirm:$false

Geoff

0 Kudos