function Get-VMDiskInformation { <# .SYNOPSIS Returns information about the VMWare volumes and associated Windows drives on a given guest. .DESCRIPTION Function connects to vSphere and a given Windows guest and returns the Windows phyical disk information mapped to the disks in VCenter. .PARAMETER VSphereServer FQDN of vSphereServer that hosts the VM .PARAMETER ComputerFQDN FQDN of the target server .PARAMETER Credentials An option parameter to take a credential object in. This is then used to authenticate with VSphere #> [CmdletBinding()] param ( [Parameter(Mandatory=$True)][ValidateNotNullOrEmpty()] [string]$VSphereServer, [Parameter(Mandatory=$True)][ValidateNotNullOrEmpty()] [string]$ComputerFQDN, $Credentials ) Import-Module VMware.VimAutomation.Core # get user credentials to connect to vSphere if ( -not $Credentials ) { $Credentials = $(Get-Credential) } # initialise arrays $g_mounts = @() $results = @() # set error action $ErrorActionPreference = "Stop" # connecting to vSphere $Start = Get-Date Write-Host "Connection to VSphere Server..." $vSphere = Get-VIServer -Server $VSphereServer -Credential $Credentials -Force # retrieving VM object Write-Host "Getting VM Object..." $VMName = Get-View -Viewtype VirtualMachine -Property guest.hostname, name ` | Where-Object { $_.guest.hostname -eq $ComputerFQDN } ` | Select-Object -ExpandProperty Name $VM = Get-VM -Server $vSphere.Name -Name $VMName Write-Host "Getting Disks..." # getting disk information $v_disks = $VM | Get-HardDisk | Sort-Object -Property Id $Session = New-CimSession -ComputerName $ComputerFQDN # Some servers return duplicate entries when grabbing the physical disks, # so we first grab the in-use serial numbers to filter the physical disks $g_disk_serials = Get-Disk -CimSession $Session | Select-Object -ExpandProperty SerialNumber # get physical disk info, but only for disks with serial numbers that appear # in `Get-Disk`. We NEED the physical location to properly map the drives. $g_disks = Get-PhysicalDisk -CimSession $Session ` | Where-Object { $_.SerialNumber -in $g_disk_serials } ` | Select-Object -Property PhysicalLocation,DeviceID,SerialNumber -Unique ` | Sort-Object -Property PhysicalLocation,DeviceID Write-Verbose "Disk Info:" Write-Verbose " - VMWare Disks: $($v_disks.Count)" $v_disks | ForEach-Object { Write-Verbose " - $($_)" } Write-Verbose " - Guest Disks: $($g_disks.Count)" $g_disks | ForEach-Object { Write-Verbose " - $($_)" } Write-Host "Finding mount points..." # adding disk information to g_mounts array, ignore empty mount paths $CimPartInfo = Get-Partition -CimSession $Session ` | Where-Object { $_.AccessPaths } ` | Select-Object -Property DiskNumber,AccessPaths,PartitionNumber foreach ($CimPart in ($CimPartInfo | Where-Object { $_.AccessPaths } )) { $g_mounts += [PSCustomObject]@{ Path = $CimPart.AccessPaths[0] DiskNumber = $CimPart.DiskNumber PartitionNumber = $CimPart.PartitionNumber } } Write-Verbose "Mount Point Info:" $g_mounts | ForEach-Object { Write-Verbose " - ID: $($_.DiskNumber) - $($_.Path)" } # map the vm disks to the guest disks (include mount points if used) for($i=0;$i -lt $v_disks.Count;$i++){ $v_disk = $v_disks[$i] $g_disk = $g_disks[$i] $g_mount = $g_mounts | Where-Object { $_.DiskNumber -eq $g_disk.DeviceId -and $_.Path } # adding disk information to results array if ( $g_drives -notcontains 900 ) { $results += [PSCustomObject]@{ # In cases where a disk has multiple partitions and multiple mount points # we only care about reporting the disk number once. DiskNumber = $g_mount.DiskNumber | Select -First 1 PartitionNumber = $g_mount.PartitionNumber VMWareDisk = $v_disk.Name DiskSerial = $g_disk.SerialNumber Path = $g_mount.Path CapacityGB = $v_disk.CapacityGB } } } # return results array $results }