VMware Cloud Community
AlbertWT
Virtuoso
Virtuoso
Jump to solution

Unable to pull the Host, Cluster, IPAddress and Disk Used in GB for VM report?

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

$VCServerName = "PRDVC03-VM"
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.ToString()
$VMInfo.ToolsStatus = $VMView.Guest.ToolsStatus
$VMInfo.Host = $vm.host.name
$VMInfo.Cluster = $vm.host.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((($vm.HardDisks | 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

Thanks in advance.

/* Please feel free to provide any comments or input you may have. */
Tags (1)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try with these changes I made.

$VCServerName = "PRDVC03-VM"

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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

Try with these changes I made.

$VCServerName = "PRDVC03-VM"

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

AlbertWT
Virtuoso
Virtuoso
Jump to solution

Many thanks, LucD for your assistance.

/* Please feel free to provide any comments or input you may have. */
0 Kudos