Skip to content

Instantly share code, notes, and snippets.

@mathse
Created September 17, 2015 11:05
Show Gist options
  • Save mathse/3e0e9a261f25c7db3dee to your computer and use it in GitHub Desktop.
Save mathse/3e0e9a261f25c7db3dee to your computer and use it in GitHub Desktop.
remove unused desktop ownership in dedicated pools on VMWare Horizon View
$daysNotUsed = 7
$ignoredPools = "ignoredPools" # regex are allowed here
Add-PSSnapIn Vmware.View.Broker
Add-PSSnapIn Vmware.VimAutomation.Core
Get-DesktopVM | ?{ $_.pool_id -notmatch $ignoredPools } | ? {$_.user_displayname} | %{
$hostname = $_.hostname
$machineId = $_.machine_id
$username = $_.user_displayname.replace((Get-ADDomain).DNSRoot,(Get-ADDomain).NetBIOSName)
Get-EventReport -viewname user_events | ? {$_.userdisplayname -eq "$username" } | ? {$_.machineid -eq "$machineId"} | select-object -last 1 | %{
if($_.eventtype -eq "AGENT_ENDED") {
$days = (new-timespan -start $_.time -end (GET-DATE)).Days
"$hostname`t$machineId`t$username`tlogged of " + $days + " days ago"
if($days -gt $daysNotUsed) {
" ... unassigning"
remove-UserOwnership -machine_id $machineId
}
}
}
}
@nmanzi
Copy link

nmanzi commented Nov 2, 2018

Thanks! I'd put the Get-EventReport before the foreach loop to make this much quicker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment