Created
September 17, 2015 11:05
-
-
Save mathse/3e0e9a261f25c7db3dee to your computer and use it in GitHub Desktop.
remove unused desktop ownership in dedicated pools on VMWare Horizon View
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
$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 | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! I'd put the Get-EventReport before the foreach loop to make this much quicker.