This script lists the following informations :
- list all the VDI workstations from your VMware connexion servers that host the ldap database DC=vdi,DC=vmware,DC=int
- get the username assigned to the VDI
- check if the VDI is reachable (ping) and then get the desktop uptime in days using WMI
Script :
import-module activedirectory
$viewcxs_arr = "vm-connexion-server1","vm-connexion-server2"
$strFilter = "pae-VM"
$vdi_arr = @()
$array = @()
$array_status = @()
foreach ($viewcxs in $viewcxs_arr){
$objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://"+$viewcxs+"/DC=vdi,DC=vmware,DC=int")
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.SearchScope = "Subtree"
$objSearcher.PageSize = 10000
$objSearcher.Filter = ("(objectCategory=$strFilter)")
$colResults = $objSearcher.FindAll()
foreach ($i in $colResults) {
$objComputer = $i.GetDirectoryEntry()
$username = $objComputer.member
$vdi = $objComputer.ipHostNumber
if ($username -ne $null) {
$string = $vdi.Value + ";" + ($username.Value).split(",")[0].substring(3)
$array = $array + $string
}
}
}
$array_sorted = $array |sort -Unique
$ping = new-object System.Net.NetworkInformation.Ping
foreach ($a in $array_sorted){
$hostname = $a.split(";")[0]
$sid = $a.split(";")[1]
try {
$objSID = New-Object System.Security.Principal.SecurityIdentifier($sid)
$objUser = $objSID.Translate([System.Security.Principal.NTAccount])
$objUsername = $objUser.Value.Split("\")[1]
}
catch [Exception] {
$objUsername = "UNKNOWN_USER ($sid)"
}
$strQuery = "select * from win32_pingstatus where address = '" + $hostname + "'"
$wmi = Get-WmiObject -Query $strQuery
if ($wmi.statuscode -eq 0) {
$Booted = Get-WmiObject -Class Win32_OperatingSystem -Computer $hostname
$uptime = New-TimeSpan -Start $Booted.ConvertToDateTime($Booted.LastBootUpTime) -End (Get-Date)
$uptime_str = $uptime.Days.ToString()
Write-Host $hostname " (assigned to $objUsername) uptime : " $uptime_str " days"
}
}
Get the virtual desktop uptime
