VMware Cloud Community
vin01
Expert
Expert

function that links VMware and WMI disk, partition and volume info

Hi

I used the below script to get guest os internal hardisk information but unexpectedly its not writing any output.Any changes should i do..

Function Get-VMDriveReport {

param($VMName)

  if (Test-Connection -ComputerName $VMName -Count 1 -Quiet) {

  $VM = Get-VM $VMName

  $Datacenter = $VM | Get-Datacenter

  $Cluster = $VM | Get-Cluster

  $Datastrore = $VM | Get-Datastore

  $VMView = Get-View -ViewType VirtualMachine -Filter @{ "Name" = $VMName }

  $VMHardDisks = Get-HardDisk -VM $VMName

  $VirtualSCSIControllers = $VMView.Config.Hardware.Device | where {$_.DeviceInfo.Label -match "SCSI Controller"}

  $VirtualDiskDevices = $VMView.Config.Hardware.Device | where {$_.ControllerKey -eq $VirtualSCSIControllers.Key}

  $DiskDrives = Get-WmiObject Win32_DiskDrive -ComputerName $VMName

  foreach($DiskDrive in $DiskDrives){

  $Disk = New-Object psobject

  $Disk | Add-Member -MemberType NoteProperty -Name VM_Name -Value $VMName -Force

  $Disk | Add-Member -MemberType NoteProperty -Name VM_ID -Value $VM.Id -Force

  $Disk | Add-Member -MemberType NoteProperty -Name VM_Host -Value $VM.VMHost -Force

  $Disk | Add-Member -MemberType NoteProperty -Name VM_HostID -Value $VM.VMHostId -Force

  $Disk | Add-Member -MemberType NoteProperty -Name VM_Cluster -Value $Cluster.Name -Force

  $Disk | Add-Member -MemberType NoteProperty -Name VM_Datastore -Value $Datastrore.Name -Force

  $Disk | Add-Member -MemberType NoteProperty -Name VM_Datacenter -Value $Datacenter.Name -Force

  $Disk | Add-Member -MemberType NoteProperty -Name Drive_Model -Value $DiskDrive.model -Force

  $Disk | Add-Member -MemberType NoteProperty -Name Drive_Partitions -Value $DiskDrive.Partitions -Force

  $Disk | Add-Member -MemberType NoteProperty -Name Drive_Size -Value ([System.Math]::Round(($DiskDrive.Size/1GB),0)) -Force

  $Disk | Add-Member -MemberType NoteProperty -Name Drive_DeviceID -Value ($DiskDrive.Deviceid.Substring(4)) -Force

  $Partitions = Get-WmiObject -ComputerName $VMName -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($DiskDrive.DeviceID.Replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"

  if( $Partitions ) {

  foreach($Part in $Partitions){

  $Disk | Add-Member -MemberType NoteProperty -Name Part_WindowsDisk -Value (($Part.Name).Split(",")[0]) -Force

  $Disk | Add-Member -MemberType NoteProperty -Name Part_Size -Value $Part.Size -Force

  $Disk | Add-Member -MemberType NoteProperty -Name Part_SizeGB -Value ([System.Math]::Round(($Part.Size/1GB),0)) -Force

  $Disk | Add-Member -MemberType NoteProperty -Name Part_Name -Value $Part.Name -Force 

  $Vols = Get-WmiObject -ComputerName $VMName -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($Part.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"

  foreach($Vol in $Vols){

  $Disk | Add-Member -MemberType NoteProperty -Name Vol_Label -Value $Vol.VolumeName -Force

  $Disk | Add-Member -MemberType NoteProperty -Name Vol_Name -Value $Vol.Name -Force

  $Disk | Add-Member -MemberType NoteProperty -Name Vol_Compressed -Value $Vol.Compressed -Force

  $Disk | Add-Member -MemberType NoteProperty -Name Vol_FileSystem -Value $Vol.FileSystem -Force

  $Disk | Add-Member -MemberType NoteProperty -Name Vol_Size -Value $Vol.Size -Force

  $Disk | Add-Member -MemberType NoteProperty -Name Vol_SizeGB -Value ([System.Math]::Round(($Vol.Size/1GB),0)) -Force

  $Disk | Add-Member -MemberType NoteProperty -Name Vol_FreeSpace -Value $Vol.FreeSpace -Force

  $Disk | Add-Member -MemberType NoteProperty -Name Vol_FreeSpaceGB -Value ([System.Math]::Round(($Vol.FreeSpace/1GB),0)) -Force

  }

  }

  }

  $VMDisk = $VirtualDiskDevices | Where { $_.UnitNumber -eq $DiskDrive.SCSITargetID }

  if ($VMDisk){

  $VMDiskBacking = $VMDisk.Backing

  $Disk | Add-Member -MemberType NoteProperty -Name VMDisk_Label -Value $VMDisk.DeviceInfo.Label -Force

  $Disk | Add-Member -MemberType NoteProperty -Name VMDisk_SizeKB -Value $VMDisk.CapacityInKB -Force

  $Disk | Add-Member -MemberType NoteProperty -Name VMDisk_SizeGB -Value ([System.Math]::Round(($VMDisk.CapacityInKB/1MB),0)) -Force

  $Disk | Add-Member -MemberType NoteProperty -Name VMDisk_DiskMode $VMDiskBacking.DiskMode

  $Disk | Add-Member -MemberType NoteProperty -Name VMDisk_FileName $VMDiskBacking.FileName

  $Disk | Add-Member -MemberType NoteProperty -Name VMDisk_Datastore $VMDiskBacking.Datastore

  $VMHardDisk = $VMHardDisks | where { $_.Filename -eq $VMDiskBacking.FileName }

  $Disk | Add-Member -MemberType NoteProperty -Name VMHardDisk_Name $VMHardDisk.Name

  $Disk | Add-Member -MemberType NoteProperty -Name VMHardDisk_DeviceName $VMHardDisk.DeviceName

  $Disk | Add-Member -MemberType NoteProperty -Name VMHardDisk_ScsiCanonicalName $VMHardDisk.ScsiCanonicalName

  }

  $Disk

  }

  }

}

==============

Desired OutPut :

==============

VM_Name : vmname
VM_ID : VirtualMachine-vm-####
VM_Host : vmname.mydom.com
VM_HostID : HostSystem-host-####
VM_Cluster : myCluster
VM_Datastore : myDatastore
VM_Datacenter : myDatacenter
Drive_Model : NETAPP LUN SCSI Disk Device
Drive_Partitions : 1
Drive_Size : 25
Drive_DeviceID : PHYSICALDRIVE3
Part_WindowsDisk : Disk #3
Part_Size : 26842464768
Part_SizeGB : 25
Part_Name : Disk #3, Partition #0
Vol_Label : sqldata
Vol_Name : M:
Vol_Compressed : False
Vol_FileSystem : NTFS
Vol_Size : 26840395776
Vol_SizeGB : 25
Vol_FreeSpace : 26271551488
Vol_FreeSpaceGB : 24
VMDisk_Label : Hard disk 3
VMDisk_SizeKB : 26214400
VMDisk_SizeGB : 25
VMDisk_DiskMode : independent_persistent
VMDisk_FileName : [myDatastore] vmname/vmname_3.vmdk
VMDisk_Datastore : Datastore-datastore-####
VMHardDisk_Name : Hard disk 3
VMHardDisk_DeviceName : vml.020009000060a98000572d537047346a43774e47754c554e202020
VMHardDisk_ScsiCanonicalName : naa.53a99990572d537047346a43774e4775

=============================================================================

But not writing any output....

Regards Vineeth.K
0 Kudos
11 Replies
hpreyers
Contributor
Contributor

There is nothing wrong with the script itself. When you execute these line of code it will not produce anything because it is put in a function. If there would be problem with the script, it would probably return some kind of error. I think it is just the way you're executing this.

You should do the following:

connect-viserver 'your_virtual_center_server'

execute the script or paste the lines of code

call the function with a vm as parameter

0 Kudos
vin01
Expert
Expert

Thanks for reply..I already connected with vcenter

Used script as below but no result

$vmname = "test1","Test2","test3"

.\GetVMDriveReport.ps1 $vmname

no result

================

$vmname |.\GetVMDriveReport.ps1

no result

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership

You have to dot-source the .ps1 file, before calling the function.

Like this

. ./GetVMDriveReport.ps1

Get-VMDriveReport $vmName

That's 2 dot with a blank in between in the 1st line


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vin01
Expert
Expert

Thanks LucD

I used as below it executed but not showing complete info

. ./GetVMDriveReport.ps1 |Get-VMDriveReport $vmname

==output======

VM_Name          : testvm

VM_ID            : VirtualMachine-vm-8347

VM_Host          : 00.00.00.00

VM_HostID        : HostSystem-host-971

VM_Cluster       : testcluster

VM_Datastore     :

VM_Datacenter    : testdatcenter

Drive_Model      :

Drive_Partitions :

Drive_Size       : 0

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership

In the ForEach loops for DiskDrives, Partitions and Vols, you overwrite the properties in the $Disk object.

For example, for each DiskDrive you use the same property named Drive_size.

You would need to adapt the property names.

For example:

$Disk | Add-Member -MemberType NoteProperty -Name "Drive_Size_$($DiskDrive.index)" -Value ([System.Math]::Round(($DiskDrive.Size/1GB),0)) -Force

This adds the index of the DiskDrive to the property name.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
hpreyers
Contributor
Contributor

Is this a fully functional VM? It stops at the wmi section, not reporting on model and partitions.

0 Kudos
vin01
Expert
Expert

Yes its fully functional vm.

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership

The VM might be powered on, but can you reach it with the Get-WmiObject call ?

Do a

Get-WmiObject Win32_DiskDrive -ComputerName $VMName

Does that return anything ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vin01
Expert
Expert

Yes LucD i'm not able to get diskdrive using  Get-WmiObject : Access is denied.Thanks i'm able know the issue.Is there any other way to get below info.If not all info atleast the info which we can get with out guest os access.

VM_Name : vmname

VM_ID : VirtualMachine-vm-####

VM_Host : vmname.mydom.com

VM_HostID : HostSystem-host-####

VM_Cluster : myCluster

VM_Datastore : myDatastore

VM_Datacenter : myDatacenter

Drive_Model : NETAPP LUN SCSI Disk Device

Drive_Partitions : 1

Drive_Size : 25

Drive_DeviceID : PHYSICALDRIVE3

Part_WindowsDisk : Disk #3

Part_Size : 26842464768

Part_SizeGB : 25

Part_Name : Disk #3, Partition #0

Vol_Label : sqldata

Vol_Name : M:

Vol_Compressed : False

Vol_FileSystem : NTFS

Vol_Size : 26840395776

Vol_SizeGB : 25

Vol_FreeSpace : 26271551488

Vol_FreeSpaceGB : 24

VMDisk_Label : Hard disk 3

VMDisk_SizeKB : 26214400

VMDisk_SizeGB : 25

VMDisk_DiskMode : independent_persistent

VMDisk_FileName : [myDatastore] vmname/vmname_3.vmdk

VMDisk_Datastore : Datastore-datastore-####

VMHardDisk_Name : Hard disk 3

VMHardDisk_DeviceName : vml.020009000060a98000572d537047346a43774e47754c554e202020

VMHardDisk_ScsiCanonicalName : naa.53a99990572d537047346a43774e4775

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership

If you have the VMware Tools installed on that VM, you could use Invoke-VMScript and run the Get-WmiObject call from inside the guest OS.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vin01
Expert
Expert

Yes tools are installed and running.can you rewrite the script for above output.

Regards Vineeth.K
0 Kudos