Hi guys,
I customized a script for VM inventory for my requirement. Its working fine. But depending on the number of disk in vm , I get that amount of rows as result.
For example: If VM1 has 2 disks...The output also has 2 rows for each disk for vm1.
Is it possible to get total capacity in a single row, rather than separate disks in multiple rows?
Here is the script I tried:
$sView = @{
ViewType = 'VirtualMachine'
Property = 'Name','runtime.powerState','Guest.hostname','Guest.net','Config.Hardware.numCPU','Summary.Storage.Committed','Summary.Storage.UnCommitted','Config.Hardware.MemoryMB','Runtime.Host','Guest.GuestFullName','Config.Hardware.Device','Config.version','guest.toolsversionstatus','Parent','ResourcePool'
Filter = @{Name = '^((?!replica).)*$'}
}
$vms = Get-View @sView
foreach($vm in $vms)
{
$t = Get-View $vm.ResourcePool -Property Name,Parent
$datacenter = $t.Name
$vm.Config.Hardware.Device | where {$_.GetType().Name -eq "VirtualDisk"} |
Select @{N="VMName";E={$vm.Name}},
@{N="Hostname";E={$vm.guest.hostname}},
@{N='IP Address';E={[string]::Join(',',($vm.Guest.Net | %{$_.IpAddress | where{$_.Split('.').Count -eq 4} | %{$_}}))}},
@{N='OS';E={$vm.Guest.GuestFullName}},
@{N='Host';E={$script:esx = Get-View -Id $vm.Runtime.Host; $script:esx.name}},
@{N='Status';E={$vm.runtime.powerState}},
@{N='CPU';E={$vm.config.Hardware.NumCpu}},
@{N='RAM';E={$vm.Config.Hardware.MemoryMB| %{[math]::Round($_/1kb,2)}}},
@{N="Disk capacity GB";E={$_.CapacityInKB| %{[math]::Round($_/1MB,2)}}},
@{N="Disk datastore";E={$_.Backing.Filename.Split(']')[0].TrimStart('[')}},
@{N="Disk Type";E={if($_.Backing.ThinProvisioned){'Thin'}
else{'Thick'}}},
@{N="TotalHDD"; E={($vm.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualDisk]}).Count}},
@{N="Disk Provision GB";E={$vm.Summary.Storage.UnCommitted| %{[math]::Round($_/1GB,2)}}},
@{N="Disk Used GB";E={$vm.Summary.Storage.Committed| %{[math]::Round($_/1GB,2)}}},
@{N="HW Version";E={$vm.Config.version}},
@{N="Tools Status";E={$vm.guest.toolsversionstatus}}
}
###################End of script###########################
Any help?
My bad, that line should have been
@{N="Disk capacity GB";E={[math]::Round(($hdd | Measure-Object -Property CapacityInKB -Sum).Sum/1MB,2)}},
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
When a VM has multiple harddisks, what would the 'Disk datastore' and 'Disk type' properties contain?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You could concatenate those values with the -join operator.
Something like this
ViewType = 'VirtualMachine'
Property = 'Name','runtime.powerState','Guest.hostname','Guest.net','Config.Hardware.numCPU','Summary.Storage.Committed','Summary.Storage.UnCommitted','Config.Hardware.MemoryMB','Runtime.Host','Guest.GuestFullName','Config.Hardware.Device','Config.version','guest.toolsversionstatus','Parent','ResourcePool'
Filter = @{Name = '^((?!replica).)*$'}
}
Get-View @sView -PipelineVariable vm |
ForEach-Object -Process {
$hdd = $vm.Config.Hardware.Device | where {$_ -is [VMware.Vim.VirtualDisk]}
'' | Select @{N="VMName";E={$vm.Name}},
@{N="Hostname";E={$vm.guest.hostname}},
@{N='IP Address';E={[string]::Join(',',($vm.Guest.Net | %{$_.IpAddress | where{$_.Split('.').Count -eq 4} | %{$_}}))}},
@{N='OS';E={$vm.Guest.GuestFullName}},
@{N='Host';E={$script:esx = Get-View -Id $vm.Runtime.Host; $script:esx.name}},
@{N='Status';E={$vm.runtime.powerState}},
@{N='CPU';E={$vm.config.Hardware.NumCpu}},
@{N='RAM';E={$vm.Config.Hardware.MemoryMB| %{[math]::Round($_/1kb,2)}}},
@{N="Disk capacity GB";E={$_.CapacityInKB| %{[math]::Round($_/1MB,2)}}},
@{N="Disk datastore";E={
($hdd | ForEach-Object -Process {
(Get-View -Id $_.Backing.Datastore -Property Name).Name
}) -join '|'
}},
@{N="Disk Type";E={
($hdd | ForEach-Object -Process {
if($_.Backing.ThinProvisioned){'Thin'}
else{'Thick'}
}) -join '|'
}},
@{N="TotalHDD"; E={$hdd.Count}},
@{N="Disk Provision GB";E={$vm.Summary.Storage.UnCommitted| %{[math]::Round($_/1GB,2)}}},
@{N="Disk Used GB";E={$vm.Summary.Storage.Committed| %{[math]::Round($_/1GB,2)}}},
@{N="HW Version";E={$vm.Config.version}},
@{N="Tools Status";E={$vm.guest.toolsversionstatus}}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Its not working....Disk Capacity value shows as 0 for all vms...
My bad, that line should have been
@{N="Disk capacity GB";E={[math]::Round(($hdd | Measure-Object -Property CapacityInKB -Sum).Sum/1MB,2)}},
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD,
Working perfectly....Keep rocking....