Category Archives: PowerCLI

Match Windows Drive Letter to VM Disk with PowerCLI

 

Matching OS Drive Letters to VM Disks is definitely something each VMware admin has wanted at some point or another. Unfortunately, it is not the easiest of tasks. Using a suggestion I found in one of the forum posts that the solution might be a combination of two different scripts referenced here and here, I decided to try it out.

After combining and editing the scripts a bit it seems to work really well for me, although I am sure individual results may vary. Anyway here is the script…

 


$DiskInfo= @()
$Vm = "vmname"

$i=0


$disks=Get-VM $vm| Get-HardDisk -DiskType "RawPhysical","RawVirtual"
$logtopart=Get-WmiObject -Class Win32_LogicalDiskToPartition -computername $VM 
$disktopart=Get-WmiObject Win32_DiskDriveToDiskPartition -computername $VM 
$logical=get-wmiobject win32_logicaldisk -computername $Vm
$volume=get-wmiobject win32_volume -computername $Vm
$partition=get-wmiobject win32_diskpartition -computername $Vm
if (($VmView = Get-View -ViewType VirtualMachine -Filter @{"Name" = $Vm})) {
    $WinDisks = Get-WmiObject -Class Win32_DiskDrive -ComputerName $VmView.Name
    foreach ($VirtualSCSIController in ($VMView.Config.Hardware.Device | where {$_.DeviceInfo.Label -match "SCSI Controller"})) {
        foreach ($VirtualDiskDevice in ($VMView.Config.Hardware.Device | where {$_.ControllerKey -eq $VirtualSCSIController.Key})) {
            $VirtualDisk = "" | Select SCSIController, DiskName, SCSI_Id, DiskFile,  DiskSize, WindowsDisk, NAA,drive, volumename
            $VirtualDisk.SCSIController = $VirtualSCSIController.DeviceInfo.Label
            $VirtualDisk.DiskName = $VirtualDiskDevice.DeviceInfo.Label
            $VirtualDisk.SCSI_Id = "$($VirtualSCSIController.BusNumber) : $($VirtualDiskDevice.UnitNumber)"
            $VirtualDisk.DiskFile = $VirtualDiskDevice.Backing.FileName
            $VirtualDisk.DiskSize = $VirtualDiskDevice.CapacityInKB * 1KB / 1GB
            $virtualdisk.naa=$disks|? {$_.name -like $VirtualDiskDevice.DeviceInfo.Label}|select -expand scsicanonicalname




            # Match disks based on SCSI ID
            $DiskMatch = $WinDisks | ?{($_.SCSIPort -2 ) -eq $VirtualSCSIController.BusNumber -and $_.SCSITargetID -eq $VirtualDiskDevice.UnitNumber}
            if ($DiskMatch){
                $VirtualDisk.WindowsDisk = "Disk $($DiskMatch.Index)"
            $i++
            }
            else {Write-Host "No matching Windows disk found for SCSI id $($VirtualDisk.SCSI_Id)"}
            

            $matchdisktopar=$disktopart|Where {$_.Antecedent -eq $diskmatch.__Path}
             $matchlogtopart=$logtopart| Where {$_.Antecedent -eq $matchdisktopar.Dependent}
             $logicalmatch=$logical| where {$_.path.path -eq $matchlogtopart.dependent}
             $VirtualDisk.volumename=$logicalmatch.volumename
             $virtualdisk.drive=$logicalmatch.deviceid


             $DiskInfo += $VirtualDisk
        }
    }
    $DiskInfo | Out-GridView
}
else {Write-Host "VM $Vm Not Found"}

A couple things to note, you obviously need WMI open on the target windows machine, and the permissions to perform WMI calls against said machine.

Here is how it looks once completed.

 

 

The NAA is blank because none of these disks are RDMs, otherwise, NAA would be populated as well. Also for some reason it is not grabbing the C drive….might have to look at that.

 

Regardless it is pretty handy to have in the back pocket when you need it.

 

Let me know what you think

Using PowerCLI to Automate SRM Post Power On Commands

Long Title… But first things first. http://www.benmeadowcroft.com/projects/srm-cmdlets-for-powercli/ Ben Meadowcroft created several cmdlets and even a module using the SRM API. Having spent a good deal of time in the SRM API myself, let me tell you….IT IS A MESS. Long story short Ben, spent a lot of time creating this really helpful module, and you… Continue Reading

Finding Templates with PowerCLI

Although this doesn’t happen often sometimes I find that I have misplaced a template. I mean that a template I know exists is not in the inventory and I am not sure on which datastore I need to go to re-add it back to the inventory. Granted I don’t personally have many datastores to look… Continue Reading

Time for 2014 Top vBlogs!

It is that time of year again where everyone votes for their Top Blogs!!! Last year I was excited to be placed 135, which was much higher than my previous year 163. This year I hope I can move up even more. A big thanks also goes out to Eric Siebert, who puts this together… Continue Reading

vNoob Guide to Getting Tail

Being in the Tech Industry it can something some of us think about quite often. We think about it at work, when we go home, when we are out with friends, in the shower, and everywhere. For those who don’t have much experience with it, it can be awkward. For those who have the courage… Continue Reading

New PowerCLI/PowerShell Scripts Section

On the Top navigation bar, you will now find a “Scripts” link. I went through a bunch of my posts(and I have more to go through) looking for scripts and powercli tidbits that I think would be helpful for most people. This will also make it easier to peruse what scripts are available from vNoob!… Continue Reading

Check ESXi Host VAAI with PowerCLI

vStorage APIs for Array Integration, or VAAI, allows the vSphere ESXi hosts to offload some of their storage workloads to the Storage arrays. Some of these workloads might be Storage vMotion a VM or provisioning a vmdk among others. VAAI is generally enabled by default on Hosts and most Storage Arrays support it. There are… Continue Reading