VMware Cloud Community
aravinds3107
Virtuoso
Virtuoso

List VM properties with VMware PowerCLI

Looking for a script which provides exports the output in the following format (CSV), I did look for some sample script in the forum but the output is different to what I am currently looking  

VM Attribute

VM Name

VM’s with snapshot

Vm1, vm2

VM’s with tools outdate

Vm5,vm10

 

I have got few more properties to add on to the report, if I can get an example to start with I can give a try.

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful |Blog: http://aravindsivaraman.com/ | Twitter : ss_aravind
6 Replies
RvdNieuwendijk
Leadership
Leadership

You can use the following example to create the desired report. The code is easily expandable with other properties.

$vm = Get-VM

# VM’s with snapshot

$vms = $vm |

  Where-Object {$_ | Get-Snapshot} |

  Select-Object -ExpandProperty Name

if ($vms) {$vms = [string]::Join(',', $vms)}

[pscustomobject]@{

  "VM Attribute" = "VM’s with snapshot"

  "VM Name" = $vms

}

# VM’s with tools outdate

$vms = $vm |

  Where-Object {$_.ExtensionData.Summary.Guest.ToolsStatus -ne 'OK'} |

  Select-Object -ExpandProperty Name

if ($vms) {$vms = [string]::Join(',', $vms)}

[pscustomobject]@{

  "VM Attribute" = "VM’s with tools outdate"

  "VM Name" = $vms

}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
aravinds3107
Virtuoso
Virtuoso

The given script is producing the output in the below format

name value
VM AttributeVM's with snapshot
VM Namevm1, vm5,etc..
VM AttributeVM's with tools outdate
VM Namevm2,vm3,etc..

But I am looking for output as below in a CSV file

VM Attribute

VM Name

VM’s with snapshot

Vm1, vm2

VM’s with tools outdate

Vm5,vm10

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful |Blog: http://aravindsivaraman.com/ | Twitter : ss_aravind
Reply
0 Kudos
DZ1
Hot Shot
Hot Shot



This should work for you:


$AllReport = @()



Get-VM | foreach {



$vm = $_



$vmReport = "" | Select Name, @{ N="Tools Status"; E={ $vm.extensiondata.guest.toolsStatus -match "Old" } }, @{ N="Snapshot count"; E={ $vm | where { $vm | Get-Snapshot } } }



$vmReport.Name = $vm.name



$vmReport."Tools Status" = $vm.extensiondata.guest.toolsStatus



$vmReport."Snapshot count" = ($vm | Get-Snapshot).count



$AllReport += $vmReport



Write $vmReport





$AllReport | Export-Csv -NoTypeInformation "c:\Foldername\filename.csv" #CHANGE ME


Reply
0 Kudos
DZ1
Hot Shot
Hot Shot


Sorry, the code was cut off.



$AllReport = @()



Get-VM | foreach {



$vm = $_



$vmReport = "" | Select Name, @{ N="Tools Status"; E={ $vm.extensiondata.guest.toolsStatus -match "Old" } },



 @{ N="Snapshot count"; E={ $vm | where { $vm | Get-Snapshot } } }



$vmReport.Name = $vm.name



$vmReport."Tools Status" = $vm.extensiondata.guest.toolsStatus



$vmReport."Snapshot count" = ($vm | Get-Snapshot).count



$AllReport += $vmReport





$AllReport | Export-Csv -NoTypeInformation "c:\Foldername\filename.csv" #CHANGE ME


Reply
0 Kudos
aravinds3107
Virtuoso
Virtuoso

I am using a similar script, but the output which I am looking is different to what which is being currently produced.. Check the previous post for the output I am currently looking..

Thanks!!

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful |Blog: http://aravindsivaraman.com/ | Twitter : ss_aravind
Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

My previous script is using the [pscustomobject] typecast which is a PowerShell 3.0 feature. You are probably using PowerShell 2.0. I modified the script using the New-Object cmdlet, to make it PowerShell 2.0 compatible.

$vm = Get-VM

# VM’s with snapshot

$vms = $vm |

  Where-Object {$_ | Get-Snapshot} |

  Select-Object -ExpandProperty Name

if ($vms) {$vms = [string]::Join(',', $vms)}

New-Object -TypeName PSObject -Property @{

  "VM Attribute" = "VM’s with snapshot"

  "VM Name" = $vms

}

# VM’s with tools outdate

$vms = $vm |

  Where-Object {$_.ExtensionData.Summary.Guest.ToolsStatus -ne 'OK'} |

  Select-Object -ExpandProperty Name

if ($vms) {$vms = [string]::Join(',', $vms)}

New-Object -TypeName PSObject -Property @{

  "VM Attribute" = "VM’s with tools outdate"

  "VM Name" = $vms

}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos