ts-getsession accepting array of computers
nightly, want generate a list of computers in domain have admin id connected via. rdp , forcibly log them off. i'm using ts-getsession accmplish this, can't make accept list of computers. i'm using try-catch around problem of -ea silentlycontinue not functioning (in ts-getsession issue tracker on msdn code gallery).
import-module psterminalservices
$servers=get-qadcomputer –searchroot ‘corp.com/servers’|select-object -expandproperty name
foreach($server in $servers)
{
try
{
$sessionstate = get-tssession -computername $server -filter {$_.connectionstate -eq 'disconnected' -and $_.username -like '*admin'} -erroraction continue
write-host $sessionstate
}
catch
{
write-host $_.categoryinfo
write-host -foregroundcolor red "error: failed sessions machine (error:" $_.exception.message ")"
#continue #not sure why not continue
}
}
output:
notspecified: (:) [], methodinvocationexception
error: failed sessions machine (error: exception calling "getsessions" "0" argument(s): "the rpc server unavailable" )
notspecified: (:) [], methodinvocationexception
error: failed sessions machine (error: exception calling "getsessions" "0" argument(s): "the rpc server unavailable" )
if take out variable , type in server name:
get-tssession -computername corpts05 -filter {$_.connectionstate -eq 'disconnected' -and $_.username -like '*admin'}
script works champ. how can script take array input?
hi,
i had little play , looks content isn't sent down pipline when use -filter command. i've added own filter.
i've included code below .. might want mod ($tssession.state -eq "disconnected") ($tssession.state -eq "disconnected" - , $_.username -like '*admin')
note use microsofts activedirectory module get-adcomputer
import-module activedirectory import-module psterminalservices trap [exception] { write-host "error trapped in $server" -foregroundcolor red continue} $sessionarray = @() $servers= get-adcomputer -filter * -searchbase "ou=servers,dc=comp,dc=com"| select-object -expandproperty name foreach($server in $servers){ if (test-connection $server -count 1 -quiet){ write-host "getting session state $server" $tssessions = get-tssession -computername $server if ($tssessions.count -gt 0){ foreach ($tssession in $tssessions){ if ($tssession.state -eq "disconnected"){ #$tssession | ft write-host "adding $server sessionarray" $sessionarray = $sessionarray + $tssession } } } } write-host "current items in array:-" $sessionarray.count } $sessionarray | ft
have fun
j
Windows Server > Windows PowerShell
Comments
Post a Comment