Hi All,
I need some help in modifying and fixing the below script to display the column result properly in.CSV:
The column Host, Cluster, IPAddress and Disk Used in GB somehow is not displaying at all in the exported .CSV
Thanks in advance.
Try with these changes I made.
Connect-VIServer $VCServerName
$ExportFilePath = "C:\RESULT\Export-VMInfo_$($VC).csv"
$Report = @()
$VMs = Get-Datacenter | Get-VM
$Datastores = Get-Datastore | Select-Object Name, Id
$VMHosts = Get-VMHost | Select-Object Name, Parent
ForEach ($VM in $VMs)
{
$VMView = $VM | Get-View
$VMInfo = { } | Select-Object VMName, Powerstate, OS, Folder, IPAddress, ToolsStatus, Host, Cluster, Datastore, NumCPU, MemMb, DiskGb, DiskFree, DiskUsed
$VMInfo.VMName = $vm.name
$VMInfo.Powerstate = $vm.Powerstate
$VMInfo.OS = $vm.Guest.OSFullName
$VMInfo.IPAddress = $vm.Guest.IPAddress -join ' | '
$VMInfo.ToolsStatus = $VMView.Guest.ToolsStatus
$VMInfo.Host = $vm.VMhost.name
$VMInfo.Cluster = $vm.VMhost.Parent.Name
$VMInfo.Datastore = ($Datastores | Where-Object { $_.ID -match (($vmview.Datastore | Select-Object -First 1) | Select-Object Value).Value } | Select-Object Name).Name
$VMInfo.NumCPU = $vm.NumCPU
$VMInfo.MemMb = [Math]::Round(($vm.MemoryMB), 2)
$VMInfo.DiskGb = [Math]::Round(((Get-Harddisk -VM $vm | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB), 2)
$VMInfo.DiskFree = [Math]::Round((($vm.Guest.Disks | Measure-Object -Property FreeSpace -Sum).Sum / 1GB), 2)
$VMInfo.DiskUsed = $VMInfo.DiskGb - $VMInfo.DiskFree
$Report += $VMInfo
}
$Report = $Report | Sort-Object VMName
IF ($Report -ne "")
{
$report | Export-Csv $ExportFilePath -NoTypeInformation
}
$VC = Disconnect-VIServer -Confirm:$False
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try with these changes I made.
Connect-VIServer $VCServerName
$ExportFilePath = "C:\RESULT\Export-VMInfo_$($VC).csv"
$Report = @()
$VMs = Get-Datacenter | Get-VM
$Datastores = Get-Datastore | Select-Object Name, Id
$VMHosts = Get-VMHost | Select-Object Name, Parent
ForEach ($VM in $VMs)
{
$VMView = $VM | Get-View
$VMInfo = { } | Select-Object VMName, Powerstate, OS, Folder, IPAddress, ToolsStatus, Host, Cluster, Datastore, NumCPU, MemMb, DiskGb, DiskFree, DiskUsed
$VMInfo.VMName = $vm.name
$VMInfo.Powerstate = $vm.Powerstate
$VMInfo.OS = $vm.Guest.OSFullName
$VMInfo.IPAddress = $vm.Guest.IPAddress -join ' | '
$VMInfo.ToolsStatus = $VMView.Guest.ToolsStatus
$VMInfo.Host = $vm.VMhost.name
$VMInfo.Cluster = $vm.VMhost.Parent.Name
$VMInfo.Datastore = ($Datastores | Where-Object { $_.ID -match (($vmview.Datastore | Select-Object -First 1) | Select-Object Value).Value } | Select-Object Name).Name
$VMInfo.NumCPU = $vm.NumCPU
$VMInfo.MemMb = [Math]::Round(($vm.MemoryMB), 2)
$VMInfo.DiskGb = [Math]::Round(((Get-Harddisk -VM $vm | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB), 2)
$VMInfo.DiskFree = [Math]::Round((($vm.Guest.Disks | Measure-Object -Property FreeSpace -Sum).Sum / 1GB), 2)
$VMInfo.DiskUsed = $VMInfo.DiskGb - $VMInfo.DiskFree
$Report += $VMInfo
}
$Report = $Report | Sort-Object VMName
IF ($Report -ne "")
{
$report | Export-Csv $ExportFilePath -NoTypeInformation
}
$VC = Disconnect-VIServer -Confirm:$False
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Many thanks, LucD for your assistance.