Wednesday, December 5, 2018

Finding Client MAC Address From VMware Horizon VDI Session

Finding VDI Client Device MAC Address From VMware Horizon VDI Session


      While administering VDI environments, most of the time we will struggle to get Thin Client or VDI Client MAC address for troubleshooting, re-imaging or for providing remote shadow support. Below article explains about a simple "visual basic" script to get this information. In my case, I kept this script on network share and configured a Desktop shortcut on all VDI machines.

       When a user connects or reconnects to a View desktop, Horizon Client gathers information about the client system and View Connection Server sends that information to the remote desktop.

       View Agent writes the client computer information to the system registry path "HKEY_CURRENT_USER\Volatile Environment" on VDI desktops.

Registry values:

ViewClient_MAC_Address
ViewClient_IP_Address

Below script will read the HKEY_CURRENT_USER\Volatile Environment\ViewClient_MAC_Address registry parameter and will give Visual Basic output as below screenshot.










''#### VBS Script ####''
On Error Resume Next
Dim objShell, strTemp
Set objShell = WScript.CreateObject("WScript.Shell")
strTemp = "HKEY_CURRENT_USER\Volatile Environment\ViewClient_MAC_Address"
WScript.Echo "VDI Client MAC Address : " & objShell.RegRead(strTemp) 
''### END ####''







Monday, December 3, 2018

Matching VMware Virtual disks and Windows OS disks using VMware Vsphere Power CLI


When working with server with several disks, Sometimes it is difficult to trace "Windows disk" corresponding to "Virtual disk". Below Steps will help you to match Windows guest disk with VMware Virtual disks.

Step 1:

First step is to open "Vmware Vsphere Power CLI" and execute command "Connect-VIServer VcenterName".In my case, I did it from vcenter server. It will prompt vCenter Credentials. 

In case you are getting Certificate error during "Connect-VIServer" operation,

Follow below link to fix it.




Step 2: 

 Execute below power shell script to list the disk details. While executing, script will prompt you to enter Guest OS credential to execute Get-WmiObject Command.

Script will list the VMDK name corresponding to the "WindowsDiskIndex" as below.

  

"WindowsdiskIndex" is the disk number "Disk 0, Disk 1,..." showing in "Disk management"





Copy below script in notepad and change its extension to .ps1 PowerShell script. 

Modify the "MYVM001" with the Virtual machine name you want to find disk Layout.

#-------------------------------------------PowerShellScript-------------------------

$vmName = "MYVM001"  
####Dont need to modify below lines of codes  
$cred = if ($cred){$cred}else{Get-Credential}  
$win32DiskDrive  = Get-WmiObject -Class Win32_DiskDrive -ComputerName $vmName -Credential $cred  
$vmHardDisks = Get-VM -Name $vmName | Get-HardDisk  
$vmDatacenterView = Get-VM -Name $vmName | Get-Datacenter | Get-View  
$virtualDiskManager = Get-View -Id VirtualDiskManager-virtualDiskManager  
foreach ($disk in $win32DiskDrive)  
{  
  $disk | Add-Member -MemberType NoteProperty -Name AltSerialNumber -Value $null  
  $diskSerialNumber = $disk.SerialNumber  
  if ($disk.Model -notmatch 'VMware Virtual disk SCSI Disk Device')  
  {  
    if ($diskSerialNumber -match '^\S{12}$'){$diskSerialNumber = ($diskSerialNumber | foreach {[byte[]]$bytes = $_.ToCharArray(); $bytes | foreach {$_.ToString('x2')} }  ) -join ''}  
    $disk.AltSerialNumber = $diskSerialNumber  
  }  
}  
$results = @()  
foreach ($vmHardDisk in $vmHardDisks)  
{  
  $vmHardDiskUuid = $virtualDiskManager.queryvirtualdiskuuid($vmHardDisk.Filename, $vmDatacenterView.MoRef) | foreach {$_.replace(' ','').replace('-','')}  
  $windowsDisk = $win32DiskDrive | where {$_.SerialNumber -eq $vmHardDiskUuid}  
  if (-not $windowsDisk){$windowsDisk = $win32DiskDrive | where {$_.AltSerialNumber -eq $vmHardDisk.ScsiCanonicalName.substring(12,24)}}  
  $result = "" | select vmName,windowsDiskIndex,vmHardDiskVmdk,vmHardDiskDatastore,vmHardDiskName,windowsDiskSerialNumber,vmHardDiskUuid,windowsDiskAltSerialNumber,vmHardDiskScsiCanonicalName  
  $result.vmName = $vmName.toupper()  
  $result.windowsDiskIndex = if ($windowsDisk){$windowsDisk.Index}else{"FAILED TO MATCH"}
  $result.vmHardDiskVmdk = $vmHardDisk.filename.split(']')[1].trim()
  $result.vmHardDiskDatastore = $vmHardDisk.filename.split(']')[0].split('[')[1]    
  $result.vmHardDiskName = $vmHardDisk.Name    
  $result.windowsDiskSerialNumber = if ($windowsDisk){$windowsDisk.SerialNumber}else{"FAILED TO MATCH"}  
  $result.vmHardDiskUuid = $vmHardDiskUuid  
  $result.windowsDiskAltSerialNumber = if ($windowsDisk){$windowsDisk.AltSerialNumber}else{"FAILED TO MATCH"}  
  $result.vmHardDiskScsiCanonicalName = $vmHardDisk.ScsiCanonicalName  
  $results += $result  
}  
$results = $results | sort {[int]$_.vmHardDiskName.split(' ')[2]}  
$results | ft -AutoSize  

##-----------------------------------ScriptEnd----------------------------------------------------------

Script result:

You may get an output similar as below. 

vmHardDiskVmdk                      vmHardDiskName    windowsDiskIndex  vmName     

--------------                      -------------- ---------------- ------     
MYVMMACHINE/MYVMMACHINE.vmdk        Hard disk 1          0     MYVMMACHINE...
MYVMMACHINE/MYVMMACHINE_7.vmdk    Hard disk 2          1     MYVMMACHINE...
MYVMMACHINE/MYVMMACHINE_12.vmdk  Hard disk 3          2     MYVMMACHINE...
MYVMMACHINE/MYVMMACHINE_3.vmdk    Hard disk 4          3     MYVMMACHINE...
MYVMMACHINE/MYVMMACHINE_5.vmdk    Hard disk 5          4     MYVMMACHINE...
MYVMMACHINE/MYVMMACHINE_1.vmdk    Hard disk 6          5     MYVMMACHINE...
MYVMMACHINE/MYVMMACHINE_2.vmdk    Hard disk 7          6     MYVMMACHINE...
MYVMMACHINE/MYVMMACHINE_8.vmdk    Hard disk 8          7     MYVMMACHINE...
MYVMMACHINE/MYVMMACHINE_9.vmdk    Hard disk 9          8     MYVMMACHINE...

Matching With Guest OS Disk:

WindowsdiskIndex is the disk number "Disk 0, Disk 1,..." showing in "Disk management"







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

Search Keywords:

trace vmware vm disk to guest disk

trace vmware virtual disk to guest disk