I have a report i've been using for sometime now but i was asked to add some additional information, drive space and ESX host the guest is currently running on. I was trying to cobble together 2 scripts I had into one since I had another script that did just drive space but it's not working, suggestions?
$viservers = "myserver"
$HostReport = @()
foreach ($singleViserver in $viservers)
{
Connect-VIServer $singleViserver -user USERNAME -password 'PASSWORD'
$VCreport = @()
Get-VM |Get-View | %{
$Report = "" | select Name, VMuuid, ESXhost, VMos, CPU, RAM, IP, FQDN, Status, Customer, vServer
$Report.vServer = $defaultVIServer
$vmImpl = $_
$Report.Name = $_.Name
$Report.VMuuid = $_.Summary.Config.uuid
$Report.ESXhost = $_.Runtime.Host.Name
$Report.VMos = $_.Summary.Config.GuestFullName
$Report.CPU = $_.Summary.Config.numCPU
$Report.RAM = $_.Summary.Config.memorySizeMB
$Report.IP = $_.Guest.IPAddress
$Report.FQDN = $_.Guest.HostName
$Report.Status = $_.Runtime.PowerState
$Report.Customer = (Get-VIObjectByVIView $_).CustomFields["Customer"]
$i = 0
$disks = ($vmImpl | Get-VMGuest).Disks
foreach($hd in (Get-HardDisk -VM $vmImpl)){
$row | Add-Member -Name ("Disk " + $i + " Name" ) -Value $hd.Name -MemberType NoteProperty
$row | Add-Member -Name ("Disk " + $i + " Path" ) -Value $hd.Filename -MemberType NoteProperty
$row | Add-Member -Name ("Disk " + $i + " Root" ) -Value (&{if($disks){$disks[$i].Path}}) -MemberType NoteProperty
$row | Add-Member -Name ("Disk " + $i + " Size (GB)" ) -Value ($hd.CapacityKB/1MB) -MemberType NoteProperty
$i++
}
$HostReport += $Report
$VCreport += $Report
}
#Disconnects after each vCenter
Disconnect-VIServer -Confirm:$False
$VCreport
}
$HostReport | Export-Csv ".\Full-HostReport.csv" -Delimiter "|" –NoTypeInformation
Try it like this
$viservers = "myserver" $HostReport = @() foreach ($singleViserver in $viservers) { Connect-VIServer $singleViserver -user USERNAME -password 'PASSWORD' $VCreport = @() Get-VM |%{ $Report = "" | select Name, VMuuid, ESXhost, VMos, CPU, RAM, IP, FQDN, Status, Customer, vServer
$Report.vServer = $defaultVIServer
$Report.Name = $_.Name $Report.VMuuid = $_.Extensiondata.Summary.Config.uuid $Report.ESXhost = $_.Host.Name $Report.VMos = $_.Extensiondata.Summary.Config.GuestFullName $Report.CPU = $_.Extensiondata.Summary.Config.numCPU $Report.RAM = $_.Extensiondata.Summary.Config.memorySizeMB $Report.IP = $_.Extensiondata.Guest.IPAddress $Report.FQDN = $_.Extensiondata.Guest.HostName $Report.Status = $_.Extensiondata.Runtime.PowerState # $Report.Customer = (Get-VIObjectByVIView $_).CustomFields["Customer"]
$i = 0
$disks = ($_ | Get-VMGuest).Disks foreach($hd in (Get-HardDisk -VM $_)){ $Report | Add-Member -Name ("Disk " + $i + " Name" ) -Value $hd.Name -MemberType NoteProperty $Report | Add-Member -Name ("Disk " + $i + " Path" ) -Value $hd.Filename -MemberType NoteProperty $Report | Add-Member -Name ("Disk " + $i + " Root" ) -Value (&{if($disks){$disks[$i].Path}}) -MemberType NoteProperty $Report | Add-Member -Name ("Disk " + $i + " Size (GB)" ) -Value ($hd.CapacityKB/1MB) -MemberType NoteProperty $i++ } $HostReport += $Report
$VCreport += $Report } #Disconnects after each vCenter
Disconnect-VIServer -Confirm:$False
$VCreport } $HostReport | Export-Csv ".\Full-HostReport.csv" -Delimiter "|" –NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try it like this
$viservers = "myserver" $HostReport = @() foreach ($singleViserver in $viservers) { Connect-VIServer $singleViserver -user USERNAME -password 'PASSWORD' $VCreport = @() Get-VM |%{ $Report = "" | select Name, VMuuid, ESXhost, VMos, CPU, RAM, IP, FQDN, Status, Customer, vServer
$Report.vServer = $defaultVIServer
$Report.Name = $_.Name $Report.VMuuid = $_.Extensiondata.Summary.Config.uuid $Report.ESXhost = $_.Host.Name $Report.VMos = $_.Extensiondata.Summary.Config.GuestFullName $Report.CPU = $_.Extensiondata.Summary.Config.numCPU $Report.RAM = $_.Extensiondata.Summary.Config.memorySizeMB $Report.IP = $_.Extensiondata.Guest.IPAddress $Report.FQDN = $_.Extensiondata.Guest.HostName $Report.Status = $_.Extensiondata.Runtime.PowerState # $Report.Customer = (Get-VIObjectByVIView $_).CustomFields["Customer"]
$i = 0
$disks = ($_ | Get-VMGuest).Disks foreach($hd in (Get-HardDisk -VM $_)){ $Report | Add-Member -Name ("Disk " + $i + " Name" ) -Value $hd.Name -MemberType NoteProperty $Report | Add-Member -Name ("Disk " + $i + " Path" ) -Value $hd.Filename -MemberType NoteProperty $Report | Add-Member -Name ("Disk " + $i + " Root" ) -Value (&{if($disks){$disks[$i].Path}}) -MemberType NoteProperty $Report | Add-Member -Name ("Disk " + $i + " Size (GB)" ) -Value ($hd.CapacityKB/1MB) -MemberType NoteProperty $i++ } $HostReport += $Report
$VCreport += $Report } #Disconnects after each vCenter
Disconnect-VIServer -Confirm:$False
$VCreport } $HostReport | Export-Csv ".\Full-HostReport.csv" -Delimiter "|" –NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
looks like it all works except the customer field...
$Report.Customer = (Get-VIObjectByVIView $_).CustomFields["Customer"]
Try:
$Report.Customer = $_.CustomFields["Customer"]
Regards, Robert
also just noticed that its only exporting information on hard drisk 1 & 2, instead of 0,1,2,3,4, etc
that works, but still the issue with not exporting all the drive info
That's because the Export-Csv takes the first row in the report to calculate how many columns need to be in the CSV. Now if the first VM only has 2harddisks, the CSV will only have columns for 2 hard disks. One solution is to take the $report array before you export it and order it by number of columns in descending order.
$HostReport = $HostReport | Sort-Object -Property {($_ | gm).count} -Descending
$HostReport | Export-Csv "c:\report.Csv" -NoTypeinfomation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I have tried the script and it returns only the hard disks that really exist. The hard disks normally start counting at "Hard disk 1". If a virtual machine has four hard disks, the script returns "Hard disk 1" up to "Hard disk 4". If a virtual machine has only two hard disks the script only returns "Hard disk 1" and "Hard disk 2".
This actually seemed to work out better.
$HostReport | Sort-Object -Property {($_ | gm -Name "Disk * Name").Count} -Descending | Export-Csv ".\Full-HD-HostReport.csv" -Delimiter "|" -NoTypeInformation
That is indeed more the PowerShell way, a clean pipe-construct without any intermediate variables.
I know some people that would like if all script were like that, one long pipe construction
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference