Eu sei que este post é meio antigo. O @Matthew Wetmore tem uma ótima solução para remover ALL PSSessions de um servidor remoto. Mas, @SuHwak tinha uma pergunta sobre como parar apenas sessões que um usuário específico gerou.
Para esse fim, escrevi uma função para ajudar.
function Get-PSSessionsForUser
{
param(
[string]$ServerName,
[string]$UserName
)
begin {
if(($UserName -eq $null) -or ($UserName -eq ""))
{ $UserName = [Environment]::UserName }
if(($ServerName -eq $null) -or ($ServerName -eq ""))
{ $ServerName = [Environment]::MachineName }
}
process {
Get-CimInstance -ClassName Win32_Process -ComputerName $ServerName | Where-Object {
$_.Name -imatch "wsmprovhost.exe"
} | Where-Object {
$UserName -eq (Invoke-CimMethod -InputObject $_ -MethodName GetOwner).User
}
}
}
E, para usá-lo ...
#Get, but do not terminate sessions for the current user, on the local computer.
Get-PSSessionsForUser
#Terminate all sessions for for the current user, on the local computer.
(Get-PSSessionsForUser) | Invoke-CimMethod -MethodName Terminate
<####################################>
#Get, but do not terminate sessions for a specific user, on the local computer.
Get-PSSessionsForUser -UserName "custom_username"
#Terminate all sessions for a specific user, on the local computer.
(Get-PSSessionsForUser -UserName "custom_username") | Invoke-CimMethod -MethodName Terminate
<####################################>
#Get, but do not terminate sessions for the current user, on a remote server.
Get-PSSessionsForUser -ServerName "remote_server"
#Terminate all sessions for the current user, on a remote server.
(Get-PSSessionsForUser -ServerName "remote_server") | Invoke-CimMethod -MethodName Terminate
<####################################>
#Get, but do not terminate sessions for a specific user, on a remote server.
Get-PSSessionsForUser -UserName "custom_username" -ServerName "remote_server"
#Terminate all sessions for a specific user, on a remote server.
(Get-PSSessionsForUser -UserName "custom_username" -ServerName "remote_server") | Invoke-CimMethod -MethodName Terminate