VMware Cloud Community
sambemyfriend
Contributor
Contributor

creating VM report using powercli

hello vmware experts,

I am new to VMware powercli, don't know how to use powercli to create report. manager asked me to created report in following formate in CSV format:

VMNameFolderName with full PathPowerStateHostNameClusterNameOSVersionGuestIPAddressToolsStatusNumCPURAMMBDataStore1TotalDiskSize1DiskFree1DiskUsed1DataStore2TotalDiskSize2DiskFree2DiskUsed2and so on for Datastore and   HardDrive

will be very grateful if anyone have such a script and can share with me.

Regards,

Samir

0 Kudos
5 Replies
vlife201110141
Enthusiast
Enthusiast

You will find most of what you asked for here. Runtime is a bit long due to many demands

connect-viserver -server "xxx" -user "xxx" -password "xxx"
$report = @()
$vms = Get-View -ViewType "virtualmachine"
foreach($vm in $vms){
     foreach($dev in $vm.Config.Hardware.Device){
          if(($dev.gettype()).Name -eq "VirtualDisk"){
                    $row = "" | select VMName, FolderName, PowerState, HostName, ClusterName, OSVersion, GuestIPAddress, ToolsStatus, NumCPU, MemoryMB, DeviceInfo, CapacityInGB, Datastore
   $row.VMName = $vm.Name
   $row.FolderName = (get-view -id $vm.parent).name
   $row.PowerState = $vm.runtime.powerstate
   $row.HostName = (get-view -id $vm.runtime.host).name
   $clu = (get-view -id $vm.Runtime.Host).parent
   $row.ClusterName = (Get-View -id $clu).name
   $row.OSVersion = $vm.summary.guest.guestfullname
   $row.GuestIPAddress = $vm.guest.ipaddress
   $row.ToolsStatus = $vm.guest.toolsstatus
   $row.NumCPU = $vm.config.hardware.numcpu
   $row.MemoryMB = $vm.config.hardware.MemoryMB
   $row.DeviceInfo = $dev.deviceinfo.label
   $row.CapacityInGB = [system.math]::Round($dev.CapacityInKB / 1048576)
   $row.Datastore = $dev.backing.filename.split("]")[0].trim("[")
   $report += $row
        }
    }
}
$report | Export-Csv C:\exportvms.csv -NoTypeInformation -UseCulture
0 Kudos
sambemyfriend
Contributor
Contributor

Hi vlife,

thanks for the reply, and sorry for the delay in reply as I was suffering from illness. I've tested script which you have provided but it's throwing following error and does not produce any .csv file

You cannot call a method on a null-valued expression.
At C:\Script\vminfo.ps1:5 char:27
+           if(($dev.gettype <<<< ()).Name -eq "VirtualDisk"){
    + CategoryInfo          : InvalidOperation: (gettype:String) [], RuntimeEx
   ception
    + FullyQualifiedErrorId : InvokeMethodOnNull

can you please let me know, how to resolve this error?

Thanks,

Sam

0 Kudos
sambemyfriend
Contributor
Contributor

hi vlife201110141,

Script is running, but I am not getting the complete path of folder using this script. I am just getting the folder name.

can you please help me, how can I get the Full path of folder in Folder Column?

Thanks,

Sam

0 Kudos
vlife201110141
Enthusiast
Enthusiast

connect-viserver -server "xxx" -user "xxx" -password "xxx"
$report = @()
$vms = Get-View -ViewType "virtualmachine"
foreach($vm in $vms){
     foreach($dev in $vm.Config.Hardware.Device){
          if(($dev.gettype()).Name -eq "VirtualDisk"){
                    $row = "" | select VMName, FolderPath, PowerState, HostName, ClusterName, OSVersion, GuestIPAddress, ToolsStatus, NumCPU, MemoryMB, DeviceInfo, CapacityInGB, Datastore
   $row.VMName = $vm.Name
   $current = Get-View $vm.Parent
   $path = $vm.Name
   do {
     $parent = $current
     if($parent.Name -ne "vm"){$path =  $parent.Name + "\" + $path}
     $current = Get-View $current.Parent
   } while ($current.Parent -ne $null)
   $row.FolderPath = $path
   $row.PowerState = $vm.runtime.powerstate
   $row.HostName = (get-view -id $vm.runtime.host).name
   $clu = (get-view -id $vm.Runtime.Host).parent
   $row.ClusterName = (Get-View -id $clu).name
   $row.OSVersion = $vm.summary.guest.guestfullname
   $row.GuestIPAddress = $vm.guest.ipaddress
   $row.ToolsStatus = $vm.guest.toolsstatus
   $row.NumCPU = $vm.config.hardware.numcpu
   $row.MemoryMB = $vm.config.hardware.MemoryMB
   $row.DeviceInfo = $dev.deviceinfo.label
   $row.CapacityInGB = [system.math]::Round($dev.CapacityInKB / 1048576)
   $row.Datastore = $dev.backing.filename.split("]")[0].trim("[")
   $report += $row
        }
    }
}
$report | Export-Csv C:\exportvms.csv -NoTypeInformation -UseCulture

0 Kudos
mpr4ul
Contributor
Contributor

Hi ,

creating VM report using powercli

I am not aware of POWERCLI but if want i can provide in perl scripting to report all those values

0 Kudos