-
-
Save 53eb1/1de9dab3ee6a18ce8531d3e1e095c6b6 to your computer and use it in GitHub Desktop.
Match OS Disk - vmware vHDD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Match Windows Volume and corresponding vHDD in VCenter | |
.DESCRIPTION | |
This script match a Windows Volume to its corresponding vHDD in VCenter | |
.PARAMETER VMname | |
Name of the VM | |
.PARAMETER Volume | |
Name of the Windows Volume | |
.EXAMPLE | |
MatchOSDiskvHDD.ps1 -VMname "Server" -Volume "D:\" | |
Match Volume D:\ of Server to it's corresponding vHDD | |
.EXAMPLE | |
MatchOSDiskvHDD.ps1 -VMname "Server" -Volume "L:\LogData\" | |
Match Volume L:\LogData\ of Server to it's corresponding vHDD | |
.NOTES | |
Name: MatchOSDiskvHDD.ps1 | |
Version: 1.0 | |
Author: Sebastian Helfrich | |
.LINK | |
https://communities.vmware.com/message/2707246?tstart=0#2707246 - adapted FMONs Script | |
.PREREQUISITE | |
- disk.EnableUUID="true" | |
- User for Invoke-VMScript with appropriate rights | |
- current vmware Tools version | |
#> | |
Param ( | |
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] | |
[Alias("VM","ComputerName")] | |
[string[]]$VMname, | |
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] | |
[Alias("VolumeName")] | |
[ValidateScript({If (($_.Substring($_.Length - 1) -eq "\")) { | |
$True | |
} Else { | |
Throw "Please check Volumename. \ Missing." | |
}})] | |
[string[]]$Volume, | |
[Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] | |
[ValidateRange(1,500)] | |
[Alias("DiskExtendbyValue")] | |
[INT]$ExtendbyValue | |
) | |
$username = 'User' ##Add Username here | |
$pwd = "" ##Add Password here | |
function Load-SnapIns | |
{ | |
Add-PSSnapin VMware.VimAutomation.Core | |
Add-PSSnapin VMware.DeployAutomation | |
set-powercliconfiguration -defaultVIServerMode Multiple -Scope ([VMware.VimAutomation.ViCore.Types.V1.ConfigurationScope]::Session) -Confirm:$false | out-null | |
} | |
function Login-VCenter | |
{ | |
#Check if a vcenter is already connected | |
$VIServercount = $global:DefaultVIServers.count | |
If ($VIServercount -gt 0) { Disconnect-VIServer * -Confirm:$false } | |
$vCenters = @( | |
"VCenter1" | |
"VCenter2" | |
) | |
#Connect to dedicated VCenter server | |
Connect-VIServer -Server $vCenters | Out-Null | |
} | |
#Clear console | |
Clear-Host | |
#Load VMware PowerCLI snap-ins in PowerShell | |
Load-SnapIns | |
#Login VCenters | |
Login-VCenter | |
#################################################################################################################################################### | |
$vmObj = Get-View -ViewType VirtualMachine -Filter @{'Name'="^$($vmname)";'Runtime.PowerState'='PoweredOn'} | |
$GVM = Get-VIObjectByVIView -VIView $vmObj | |
#######Build Invoke-VMScript for remote execution####### | |
$RemVolLabel =$Volume | |
#Creation of Disk check invoke-vmscript Scriptblock | |
$Skript= @' | |
[string]$RemVolLabel="" | |
#Get Disk ID with diskpart | |
[object]$RemDiskp = "sel vol $($RemVolLabel)","Detail volume" | diskpart.exe; | |
$RemDiskp = [system.String]::Join(" ", $RemDiskp); | |
[int]$pos = $RemDiskp.IndexOf("* Disk "); | |
[string]$RemDiskID = ($RemDiskp.Substring($pos+1).Split(' ')[2]) -replace("[^`\d]"); | |
#Check if disk is VMware Virtual disk. If not, exit script | |
$RemW32Diskdrive = (get-wmiobject win32_diskdrive) | where {$_.Index -eq $RemDiskID}; | |
[string]$RemDiskMod = $RemW32Diskdrive.Model.Substring(0,14); | |
If (!($RemDiskMod -eq "VMware Virtual")) {exit}; | |
#Get Partition Standard (GPT or MBR) | |
[string]$RemPartStd = ((get-wmiobject win32_diskpartition | where {$_.DiskIndex -eq $RemDiskID}) | select -expand type); | |
$RemPartStd = $RemPartStd.substring(0,3); | |
If ($RemPartStd -eq "GPT") {$RemPartStdResult = "GPT"} | |
else {$RemPartStdResult = "MBR"}; | |
#Get Disk SN | |
############################### | |
#Check if mountpoint (true = mountpoint) | |
$RemDevice = $RemVolLabel.trim('\') | |
$RemMountCheck = If(Get-WmiObject Win32_LogicalDisk | where-object { $_.DeviceID -eq $RemDevice }) {$RemMountResult=$false} else {$RemMountResult=$true} | |
#If no Mountpoint (MBR and GPT) | |
If (!($RemMountResult)){$Output=$RemVolLabel+","+$RemDiskID+","+$RemW32Diskdrive.SerialNumber+","+$RemW32Diskdrive.Size+","+$RemPartStdResult+","+$RemDiskMod;$Output} | |
#If Mountpoint [GPT] | |
If (($RemMountResult) -and ($RemPartStdResult -eq "GPT")) { | |
[string]$RemPhysDriveID="`\`\.`\PHYSICALDRIVE"+$RemDiskID; | |
$RemMntGPT= Get-Wmiobject win32_diskdrive | where { $_.DeviceID -eq $RemPhysDriveID}; | |
$Output = $RemVolLabel + "," + $RemMntGPT.Index + "," + $RemMntGPT.Serialnumber + "," + $RemMntGPT.Size + ","+$RemPartStdResult+"," + $RemMntGPT.Model.Substring(0,20);$Output} | |
#If Mountpoint [MBR] | |
If (( $RemMountResult ) -and ( $RemPartStdResult -eq "MBR" )) { | |
If (!($RemVolLabel -match '.+?\\$')) {$RemVolLabel = $RemVolLabel + '\'} | |
$source = [regex]::escape($RemVolLabel) | |
$RemVolDevID = (Get-WmiObject -Query "SELECT * FROM Win32_Volume WHERE Name='$source'").DeviceID.TrimEnd("`\") | |
$trim = ([string]($RemVolDevID).substring(1)).Replace("?","??") | |
$key_path = "HKLM:`\SYSTEM`\MountedDevices" | |
$1 = (Get-ItemProperty -Path $key_path).$trim | |
$D1=[System.Convert]::ToString($($1)[3],16) | |
$D2=[System.Convert]::ToString($($1)[2],16) | |
$D3=[System.Convert]::ToString($($1)[1],16) | |
$D4=[System.Convert]::ToString($($1)[0],16) | |
$DOut = $D1+$D2+$D3+$D4 | |
[string]$3 = [convert]::toint64("$DOut",16) | |
$MBR = ((gwmi win32_diskdrive) | where {$_.Signature -eq $($3)}) | |
$Output = $RemVolLabel + "," + $MBR.Index + "," + $MBR.Serialnumber + "," + $MBR.Size + ","+$RemPartStdResult+"," +$RemDiskMod | |
$Output | |
} | |
'@ #Script End | |
$Skript = $Skript.Replace('$RemVolLabel=""',"`$RemVolLabel=`"$($Volume)`"") | |
$RemVMOut = ((Invoke-VMScript -VM $GVM -Scripttype powershell -ScriptText $Skript -Guestuser $username -Guestpassword $pwd -WarningAction SilentlyContinue).ScriptOutput).ToString().split("{,}") | |
#Get all attached vHDDs of the VM | |
$VMHDDs = $GVM | Get-HardDisk | |
#Datacenter and virtual disk manager | |
$vmDatacenterView = $GVM | Get-Datacenter | Get-View | |
$virtualDiskManager = Get-View -Id VirtualDiskManager-virtualDiskManager | |
#Create Hash Table | |
$vmresults = @() | |
foreach ($vmHardDisk in $VMHDDs) | |
{ | |
[string]$string = $vmHardDisk.Filename | |
$vmHardDiskUuid = ($vmHardDisk.ExtensionData.Backing.Uuid | ForEach-Object {$_.replace(' ','').replace('-','')}) | |
$vmresult = "" | Select-Object vmName,vmHardDiskVmdk,vmHardDiskName,vmHardDiskSize,vmHardDiskUuid | |
$vmresult.vmName = $vmName | |
$vmresult.vmHardDiskVmdk = $vmHardDisk.filename.split(']')[1].trim() | |
$vmresult.vmHardDiskName = $vmHardDisk.Name | |
$vmresult.vmHardDiskSize = $vmHardDisk.CapacityGB | |
$vmresult.vmHardDiskUuid = $vmHardDiskUuid | |
$vmresults += $vmresult | |
} | |
$vmresults | out-null | |
#Match disk | |
$OSvhddmatch = $vmresults | where { $_.vmHardDiskUuid -eq $RemVMOut[2] } | |
########################################### | |
##############OUTPUT####################### | |
Write-Output "########## Match Windows OS Disk with VMware vHDD v1.0 (22.11.2017) ##########" | |
Write-Output " " | |
Write-Output "Server: $($VMname)" | |
Write-Output " " | |
Write-Output "OS Label: $($RemVMOut[0])" | |
Write-Output "OS Disk ID: $($RemVMOut[1])" | |
Write-Output "OS Disk SN: $($RemVMOut[2])" | |
Write-Output "OS Disk Signature: $($RemVMOut[3])" | |
Write-Output "OS Disk PartType: $($RemVMOut[4])" | |
Write-Output "OS Disk Model: $($RemVMOut[5])" | |
Write-Output " " | |
Write-Output "VM vHDD Size: $($OSvhddmatch.vmHardDiskSize)" | |
Write-Output "VM vHDD Name : $($OSvhddmatch.vmHardDiskName)" | |
Write-Output "" | |
Disconnect-VIServer * -Confirm:$false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment