It’s always a good practice to keep a VDI estate in a good managed manner. Unused VDIs are not so easy to locate with the provided out-of-box View Administrator capabilities. Such VDIs can often appear when people leave and if not a good working leaving process exists.
In some of the environments I was tasked to clean the unused VDIs and decided to automate the process.
The script will report events from all pools with a VCenter source. If data for Manual pools with physical desktops is required, just change the respective WHERE clause.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
If ((Get-PSSnapIn -Name vmware.view.broker -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapIn -Name vmware.view.broker } # Adjust start date if required $startdate=(Get-Date).AddDays(-180) function GetEvents { $events = $null $events = @{} $events = Get-EventReport -viewName user_events -startDate $startdate | Where { ($_.eventtype -eq "AGENT_CONNECTED" -or $_.eventtype -eq "AGENT_RECONNECTED") } return $events } function FindLatestLogin { $allevents = GetEvents Write-Host "Successfully retrieved all events." Write-Host "Generating CSV report file..." $allvdi = Get-Pool | Where {$_.vcServerName -ne $null} | Get-DesktopVM $outputArray = $null $outputArray =@() foreach ($tvdi in $allvdi) { $efound = $false $tdate = $startdate if (!$tvdi.user_displayname) { $assigned = "Not Assigned" } else { $assigned = $tvdi.user_displayname } foreach ($event in $allevents) { # Extract VDI name from an event $VDI = $event.moduleandeventtext $pos = $VDI.IndexOf("machine ") If ($VDI.Substring($pos+8) -eq $tvdi.Name) { $efound = $true $edate = Get-Date($event.time) # If more than 1 event for specific VDI exists we want the newest one If ($edate -gt $tdate ) { $tdate = $edate } } } $vdiOO = new-object PSObject $vdiOO | Add-Member -MemberType NoteProperty -Name "VDI" -Value $tvdi.Name $vdiOO | Add-Member -MemberType NoteProperty -Name "Pool" -Value $tvdi.pool_id $vdiOO | Add-Member -MemberType NoteProperty -Name "AssignedUser" -Value $assigned If ($efound) { $vdiOO | Add-Member -MemberType NoteProperty -Name "LoginTime" -Value $tdate } Else { $vdiOO | Add-Member -MemberType NoteProperty -Name "LoginTime" -Value "no logins for the last 180 days" } $outputArray += $vdiOO } $filename = "c:\temp\VDILastLoginTimes.csv" $outputArray | Export-csv $filename -NoTypeInformation Write-Host "Completed." } FindLatestLogin |