Thanks LucD as always, you help me a lot.
the script is working fine and I tried to add some details related to VM and I made it like this, unfortunately, I get an error message related to this line **$guestHD = Get-VMGuestDisk -HardDisk $HardDisk**
$VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {
ForEach ($VM in ($Cluster | Get-VM | Where-Object{$_.Name -notmatch '^vcls'} | Sort-Object -Property Name)) {
ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
foreach ($VM in (Get-View -ViewType VirtualMachine -Property Guest.GuestFullName, Config.GuestFullName)){
$guestHD = Get-VMGuestDisk -HardDisk $HardDisk
"" | Select-Object -Property @{N="VM";E={$VM.Name}},
@{N="Configured OS";E={$Config.GuestFullName}},
@{N="Running OS";E={$Guest.GuestFullName}},
@{N="Datacenter";E={$Datacenter.name}},
@{N="Cluster";E={$Cluster.Name}},
@{N="Hard Disk";E={$HardDisk.Name}},
@{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},
@{N="VMConfigFile";E={$VM.ExtensionData.Config.Files.VmPathName}},
@{N="VMDKpath";E={$HardDisk.FileName}},
@{N="VMDK Size";E={[math]::Round(($vm.extensiondata.layoutex.file|Where-Object{$_.name -contains $harddisk.filename.replace(".","-flat.")}).size/1GB)}},
@{N="Drive Size";E={$HardDisk.CapacityGB.foreach{[math]::Round($_)}}},
@{N='GuestDiskPath';E={$guestHD.DiskPath -join "`n"}},
@{N='GuestCapacityGB';E={($guestHD.CapacityGB.foreach{[math]::Round($_)}) -join "`n"}},
@{N='GuestFreeGB';E={($guestHD.FreeSpaceGB.foreach{[math]::Round($_)}) -join "`n"}},
@{N='GuestDiskType';E={$guestHD.FileSystemType -join "`n"}}
}
}
}
}
}
$VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path ".\VmInfo.csv"