VMware Cloud Community
sambemyfriend
Contributor
Contributor

vm report using powercli

hello VMGurus,

I am using following script for vm report as suggested by vlife201110141

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

but using this script I am not able to get the complete path of the folder in output files, it just gives me the parent folder name. can someone help me, how can I get the complete folder path using this script?

thanks

Sam

0 Kudos
2 Replies
vkaranam
Enthusiast
Enthusiast

Hey Sam,

I am trying your Script and it ran fine once and now i am getting the following error


Get-View : Cannot validate argument on parameter 'Id'. The argument is null or
empty. Supply an argument that is not null or empty and then try the command ag
ain.
At line:6 char:35
+    $row.FolderName = (get-view -id <<<<  $vm.parent).name
    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingVal
   idationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView


can you suggest me what is wrong in that.

Thanks

VK

0 Kudos
vlife201110141
Enthusiast
Enthusiast

I borrowed full folder path idea from Luc and modified a bit. Here it is ...

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