Hi Guys!
I’m working on a script to automate the resetting of RDS profiles when they get stuck / errors occur.
Currently, i have the script finding users, deleting their local rds profiles and clearing them down, which is great.
Before i run the script, i want to make sure that i disconnect the current users session from whatever RDS server they are logged in to, (we have an RDS farm of 3 session hosts)
I would like the script to automate this part of it too. Helpdesk enters the username, script searches the array of RDS session hosts, grabs the session ID and logs them off, then continues down the script. Problem is, i can’t figure out this part of it, anyone have any experience with this? Is it possible? I’ve included what i have so far and also what i’ve tried.
## Created by Chris Pounds 2017 ## ## Script to find a Users folder in C drive of target computer, created to clear RDS Profiles ## ## if you want to target multiple machines, place them in this string list ## $sessionhost = @("***","***","***") $filestore = "***" $user = Read-Host "Please enter username" foreach ($sessionhosts in $sessionhost) { Invoke-Command -Computer $sessionhosts -ArgumentList $sessionhosts,$user -ScriptBlock { param($sessionhosts,$user) Remove-Item -Path \\$sessionhosts\C$\Users\$user* } Write-Host "Profiles deleted" } Invoke-Command -Computer $filestore -ArgumentList $filestore,$user -ScriptBlock { param($filestore,$user) Remove-Item -Path \\$filestore\C$\RDS` Roaming` Profiles\$user* -Recurse } Write-Host "All instances of $user have been removed"
The code i tried to use to find users sessions is
foreach($sessionhosts in $sessionhost) {$session = ((quser /server:$sessionhost | ? { $_ -match $user }) -split ’ +')[2]
logoff $session /server:$server
}
Write-Host $user “Logged off from server”
which works if the session hosts aren’t listed in an array like i currently have. The errors i received when i ran the script are.
quser : Error 0x000006BA enumerating sessionnames At N:\PSScripts\RemoveLocalProfileWithArray.ps1:15 char:14 + $session = ((quser /server:$sessionhost | ? { $_ -match $user }) -spl ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (Error 0x000006B...ng sessionnames:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError Error [1722]:The RPC server is unavailable. logoff : If you give a server name, you need to specify a session ID or a session name. At N:\PSScripts\RemoveLocalProfileWithArray.ps1:17 char:1 + logoff $session /server:$server + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (If you give a s...a session name.:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError quser : Error 0x000006BA enumerating sessionnames At N:\PSScripts\RemoveLocalProfileWithArray.ps1:15 char:14 + $session = ((quser /server:$sessionhost | ? { $_ -match $user }) -spl ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (Error 0x000006B...ng sessionnames:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError Error [1722]:The RPC server is unavailable. logoff : If you give a server name, you need to specify a session ID or a session name. At N:\PSScripts\RemoveLocalProfileWithArray.ps1:17 char:1 + logoff $session /server:$server + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (If you give a s...a session name.:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError quser : Error 0x000006BA enumerating sessionnames At N:\PSScripts\RemoveLocalProfileWithArray.ps1:15 char:14 + $session = ((quser /server:$sessionhost | ? { $_ -match $user }) -spl ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (Error 0x000006B...ng sessionnames:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError Error [1722]:The RPC server is unavailable. logoff : If you give a server name, you need to specify a session ID or a session name. At N:\PSScripts\RemoveLocalProfileWithArray.ps1:17 char:1 + logoff $session /server:$server + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (If you give a s...a session name.:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError
Sorry about the long post! Any help / tips would be smashing