VMware Cloud Community
StuartB20111014
Enthusiast
Enthusiast

Script to extract VM data. Almost there....

Hi,

I have to find the following data from several hundred VMs on specific hosts.

VM Name

Host it belongs to

Memory allocated

VCpu

Disk space used

Network Port group details (ie name)

Now the first few bits do seem to work with this script. However getting port group details can only be done with a get-networkapaters. I need to kind of join the two, but not sure how to do it:

Connect-VIserver "myserver";

foreach ($srv in Get-Content "d:\hostlist.csv") {

    Get-VMHost $srv | Get-VM | Select-Object "name","version","vmhost","guest","powerstate","numcpu","usedspacegb";

}

The hostlist.csv is actually just a text file and contains host names.

Any help appreciated.

I managed to scratch up

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership

That's where a calculated property comes in handy.

You can get the portgroupname(s) like this

Connect-VIserver "myserver";
foreach ($srv in Get-Content "d:\hostlist.csv") {
    Get-VMHost $srv | Get-VM | 
        Select-Object name,version,vmhost,guest,powerstate,numcpu,usedspacegb,
            @{N="Portgroups";E={[string]::Join(',',(Get-NetworkAdapter -VM $_ | %{$_.NetworkName}))}}
}


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

Reply
0 Kudos
StuartB20111014
Enthusiast
Enthusiast

Hi Luc,

Thanks for that. I see what you did with tacking the sql style join on the end. One quick question though. I know I can use export-csv to export it. Where should it go?

Many thanks

Stuart

Reply
0 Kudos
LucD
Leadership
Leadership

Hi Stuart,

The Join is there to avoid problems with VMs that have more than 1 NIC, it just converts an array of portgroupnames into a single string.

The Export-Csv is a bit trickier since you have the Select in a foreach loop.

That can be solved like this

Connect-VIserver "myserver";
$report = foreach ($srv in Get-Content "d:\hostlist.csv") {
    Get-VMHost $srv | Get-VM | 
        Select-Object name,version,vmhost,guest,powerstate,numcpu,usedspacegb,
            @{N="Portgroups";E={[string]::Join(',',(Get-NetworkAdapter -VM $_ | %{$_.NetworkName}))}}
}

$report | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture 


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

Reply
0 Kudos