Hi I'd like an excel sheet to list the following into columns.....
VM Name, no of cpus, Ram allocated, all hd's allocated size, all hd's current usage, all nic & vlans, powerstate
Try something like this
$report = foreach($vm in Get-VM){
$obj = [PSCustomObject]@{
Name = $vm.Name
NumCPU = $vm.NumCpu
MemoryGB = $vm.MemoryGB
PowerState = $vm.PowerState
}
1..4 | %{
Add-Member -InputObject $obj -MemberType NoteProperty -Name "HD$($_)" -Value ''
Add-Member -InputObject $obj -MemberType NoteProperty -Name "HD$($_)CapacityGB" -Value ''
Add-Member -InputObject $obj -MemberType NoteProperty -Name "HD$($_)AllocatedGB" -Value ''
}
$i = 1
foreach($hd in Get-HardDisk -VM $vm){
if($i -le 4){
$disk = $vm.ExtensionData.LayoutEx.Disk | where{$_.Key -eq $hd.ExtensionData.Key}
$files = $vm.ExtensionData.LayoutEx.File | where{$disk.Chain.FileKey -contains $_.Key}
$obj."HD$($i)" = $hd.Name
$obj."HD$($i)CapacityGB" = [math]::Round($hd.CapacityGB,1)
$obj."HD$($i)AllocatedGB" = [math]::Round(($files | Measure-Object -Property Size -Sum | select -ExpandProperty Sum)/1GB,1)
$i++
}
}
1..4 | %{
Add-Member -InputObject $obj -MemberType NoteProperty -Name "NIC$($_)" -Value ''
Add-Member -InputObject $obj -MemberType NoteProperty -Name "NIC$($_)Portgroup" -Value ''
Add-Member -InputObject $obj -MemberType NoteProperty -Name "NIC$($_)VLANid" -Value ''
}
$i = 1
foreach($nic in Get-NetworkAdapter -VM $vm){
Get-VirtualPortGroup -Name $nic.NetworkName -VM $vm | %{
if($i -le 4){
$obj."NIC$($i)" = $nic.Name
$obj."NIC$($i)Portgroup" = $_.Name
$obj."NIC$($i)VLANid" = &{
if($_ -is [VMware.VimAutomation.ViCore.Types.V1.Host.Networking.DistributedPortGroup]){
if($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]){
$_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId
}
elseif($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchVlanSpec]){
if($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [VMware.Vim.NumericRange[]]){
[string]::Join(',',($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | %{"$($_.Start)-$($_.End)"}))
}
else{
$_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
}
}
}else{$_.VlanId}}
}
}
$i++
}
$obj
}
$report | Export-Csv -Path .\vm-report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Do you mean the total size used by all the harddisks, or the size for each harddisk?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
for each hard disk.
eg hdd1 allocated 100gb hdd 1 60gb in use
thanks
How do envisage to show that in a CSV?
The Export-Csv has issues when presented with rows of varying length.
Or is there a maximum of harddisks (for example 4) that can be used, eventually with empty columns for VMs with less than 4 harddisks.
Same for the vNICs.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
no more than 4 for hdd and nics
Try something like this
$report = foreach($vm in Get-VM){
$obj = [PSCustomObject]@{
Name = $vm.Name
NumCPU = $vm.NumCpu
MemoryGB = $vm.MemoryGB
PowerState = $vm.PowerState
}
1..4 | %{
Add-Member -InputObject $obj -MemberType NoteProperty -Name "HD$($_)" -Value ''
Add-Member -InputObject $obj -MemberType NoteProperty -Name "HD$($_)CapacityGB" -Value ''
Add-Member -InputObject $obj -MemberType NoteProperty -Name "HD$($_)AllocatedGB" -Value ''
}
$i = 1
foreach($hd in Get-HardDisk -VM $vm){
if($i -le 4){
$disk = $vm.ExtensionData.LayoutEx.Disk | where{$_.Key -eq $hd.ExtensionData.Key}
$files = $vm.ExtensionData.LayoutEx.File | where{$disk.Chain.FileKey -contains $_.Key}
$obj."HD$($i)" = $hd.Name
$obj."HD$($i)CapacityGB" = [math]::Round($hd.CapacityGB,1)
$obj."HD$($i)AllocatedGB" = [math]::Round(($files | Measure-Object -Property Size -Sum | select -ExpandProperty Sum)/1GB,1)
$i++
}
}
1..4 | %{
Add-Member -InputObject $obj -MemberType NoteProperty -Name "NIC$($_)" -Value ''
Add-Member -InputObject $obj -MemberType NoteProperty -Name "NIC$($_)Portgroup" -Value ''
Add-Member -InputObject $obj -MemberType NoteProperty -Name "NIC$($_)VLANid" -Value ''
}
$i = 1
foreach($nic in Get-NetworkAdapter -VM $vm){
Get-VirtualPortGroup -Name $nic.NetworkName -VM $vm | %{
if($i -le 4){
$obj."NIC$($i)" = $nic.Name
$obj."NIC$($i)Portgroup" = $_.Name
$obj."NIC$($i)VLANid" = &{
if($_ -is [VMware.VimAutomation.ViCore.Types.V1.Host.Networking.DistributedPortGroup]){
if($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]){
$_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId
}
elseif($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchVlanSpec]){
if($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [VMware.Vim.NumericRange[]]){
[string]::Join(',',($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | %{"$($_.Start)-$($_.End)"}))
}
else{
$_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
}
}
}else{$_.VlanId}}
}
}
$i++
}
$obj
}
$report | Export-Csv -Path .\vm-report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That's exactly what I wanted.
Thank you very much for this.