Using PowerShell to get VM Inventory

Hi,

i am running the below to get the VM inventory. However i need the syntax to get the VHD information / size.

Get-SCVirtualMachine -VMHost HOSTNAME -VMMServer LocalHost | Select-Object Name, ComputerName, Status, Description, VHDType, Memory, DynamicMemoryEnabled, DynamicMemoryMinimumMB, DynamicMemoryMaximumMB, Location, OperatingSystem, VMId | Export-csv "C:\VMInventory"  -NoTypeInformation

Thanks,

Avinash

May 4th, 2014 1:05pm

Hi Avinash,

To add the VHD information, you can try the cmdlet Get-SCVirtualDiskDrive, and also refer to the script below:

$report = @()
$VMs = Get-SCVirtualMachine -VMHost HOSTNAME -VMMServer LocalHost 
foreach ($VM in $VMs) {
$VHDs = $VM | Get-SCVirtualDiskDrive
$i = "1"
foreach ($VHDconf in $VHDs){ 
if($i -eq "1"){
$data = New-Object PSObject -property @{
VMName=$VM.Name
vCPUs=$VM.CPUCount
MemoryGB= $VM.Memory/1024
VHDName = $VHDconf.VirtualHardDisk.Name
VHDSize = $VHDconf.VirtualHardDisk.MaximumSize/1GB
VHDCurrentSize = [Math]::Round($VHDconf.VirtualHardDisk.Size/1GB)
VHDType = $VHDconf.VirtualHardDisk.VHDType
VHDBusType = $VHDconf.BusType
VHDBus = $VHDconf.Bus
VHDLUN = $VHDconf.Lun
VHDDatastore = $VHDconf.VirtualHardDisk.HostVolume
}
$i= "2"
}else{
$data = New-Object PSObject -property @{
VMName=""
vCPUs=""
MemoryGB= ""
VHDName = $VHDconf.VirtualHardDisk.Name
VHDSize = $VHDconf.VirtualHardDisk.MaximumSize/1GB
VHDCurrentSize = [Math]::Round($VHDconf.VirtualHardDisk.Size/1GB)
VHDType = $VHDconf.VirtualHardDisk.VHDType
VHDBusType = $VHDconf.BusType
VHDBus = $VHDconf.Bus
VHDLUN = $VHDconf.Lun
VHDDatastore = $VHDconf.VirtualHardDisk.HostVolume
				}
			}
			$report +=$data	
		}
	}

$report
$report|Export-csv "C:\VMInventory.csv"  -NoTypeInformation

Reference from:

VM inventory function with Powershell on SCVMM 2012

If you have any feedback on our support, please click here.

Best Regards,

Anna

TechNet Community Support

  • Proposed as answer by Praveen Shrivastava Monday, May 12, 2014 9:57 AM
  • Marked as answer by avmen Monday, May 12, 2014 10:04 AM
Free Windows Admin Tool Kit Click here and download it now
May 5th, 2014 3:30pm

Hi Anna,

The script worked perfectly - Thanks!

Is it possible to please advise the object name that i can use to include the snapshot inventory as well?

May 6th, 2014 12:00pm

Hi Avmen,

Sorry for the delay.

I feel a little confused with your quetion "advise the object name that i can use to include the snapshot inventory ", would you please explain more in detail?

If you want to add the properties listed in your initial posting to the output, please modify the script like this:

$data = New-Object PSObject -property @{
VMName=$VM.Name
Status=$VM.Status
Description=$VM.Description
DynamicMemoryEnabled=$VM.DynamicMemoryEnabled
DynamicMemoryMinimumMB=$VM.DynamicMemoryMinimumMB
DynamicMemoryMaximumMB=$VM.DynamicMemoryMaximumMB
Location=$VM.Location
OperatingSystem=$VM.OperatingSystem
VMId=$VM.VMId
vCPUs=$VM.CPUCount
MemoryGB= $VM.Memory/1024
VHDName = $VHDconf.VirtualHardDisk.Name
VHDSize = $VHDconf.VirtualHardDisk.MaximumSize/1GB
VHDCurrentSize = [Math]::Round($VHDconf.VirtualHardDisk.Size/1GB)
VHDType = $VHDconf.VirtualHardDisk.VHDType
VHDBusType = $VHDconf.BusType
VHDBus = $VHDconf.Bus
VHDLUN = $VHDconf.Lun
VHDDatastore = $VHDconf.VirtualHardDisk.HostVolume
}

If you have any feedback on our support, please click here.

Best Regards,

Anna

TechNet Community Support


Free Windows Admin Tool Kit Click here and download it now
May 11th, 2014 4:03pm

If your VM have more than 1 vhd, you need to get all vhds. And if you have a checkpoints, you need to get all checkpoints vhds:

...

$totalhdd = 0 
$snapshotshdd = 0 
$snapshotscount = 0

foreach ($vm in $vms) {   
    $vhds = $vm.VirtualHardDisks    
    foreach ($vhd in $vhds) { $totalhdd += ($vhd.Size -as [double]) / 1GB}  
    
    # Snapshots search
    foreach ($vmc in $vm.VMCheckpoints) {
        $snapshotscount++
        foreach ($vdd in $vmc.VirtualDiskDrives) {
            $vhd = Get-SCVirtualHardDisk -ID $vdd.VirtualHardDiskID
            $snapshotshdd += ($vhd.Size -as [double]) / 1GB 
            
        }
    }
}
...

September 3rd, 2015 11:58am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics