Newbie, Disconnect account from Teminal Server

by FrancoisL at 2012-12-15 16:01:26

I made a noob attemp to create a script:

I am very greatfull for anyone with any comment on my script, i hope one day to be in your league !!!
-Run the script against a list of computer (listOfComputer.txt)
- get all the Account active or disconnected
- how could i list all computername with disconnected account ??
- how to actually do a reset of that disconnected user (that forgot to log off) ??

1) How to i control , to send that computername not working to export into a file(Computer_Not_Working.txt)
when i get theses type of issues: if the computer name or access to computer or if i get this
Exception calling “Open” with “0” argument(s): “The RPC server is unavailable"

Because the script stop with the first computername not working or cannot when RPC server is unavailable (i would like the scritp to send that computer name into a file and continue with the next computer in my listOfComputer.txt.

2) Got stuck trying to get the result of of a server into a variable of the list of active and disconnected account from Teminal Server, then continue and get the next result.

get-TSSession -ComputerName $currline -username * | ft username,state,idletime,logintime,remoteendpoint -AutoSize

My small attemps is trying to do this:

## module to open “Browse” box to select input file (listOfComputer.txt)
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
$ofd = New-Object System.Windows.Forms.OpenFileDialog
$ofd.Title = “*** Please provide an TXT extension INPUT file ! ***”
$ofd.InitialDirectory = “d:\scripts”
if($ofd.ShowDialog() -eq “OK”) { $ofd.FileName }
$source = $ofd.FileName

$results = @(”")

## Get all the teminal server connection on each servers
foreach ($currline in Get-Content $source) {Â Â
Write-Host “ServerName: !” $currline -ForegroundColor Red
get-TSSession -ComputerName $currline -username * | ft username,state,idletime,logintime,remoteendpoint -AutoSize

## Cannot find how to get each result per server into a file or variable to generate a first report ? Showing who is always forgetting to log off.
## Could i also sent this into a variable to later try and find a way to do a RESET of the account on thoses servers with Disconnected accounts ?

# $aaa = get-TSSession -ComputerName $currline -username * | ft username,state,idletime,logintime,remoteendpoint -AutoSize
# $aaa = $aaa | Format-List accessToString
  $results = $results + $aaa

Here is the results i get withing the PowerGUI Script Editor:

ServerName: ! computerXXXX

UserName State IdleTime LoginTime RemoteEndPoint
-------- ----- -------- --------- -------------
Disconnected 22.23:08:03.9060000
Listening 00:00:00
Connected 00:00:00
abeer Active 01:02:02.8750000 2012/12/09 16:26:44…
jonDoe Disconnected 1.22:26:19.8440000 2012/12/10 21:58:23

ServerName: ! WINNT123
Exception calling “Open” with “0” argument(s): “The RPC server is unavailable”
à C:\Documents and Settings\jonDoe\My Documents\WindowsPowerShell\Modules\PSTerminalServices\PSTerminalServices.psm1: ligne:189 caractère:24
+ $TSRemoteServer.Open( <<<< )
by mikefrobbins at 2012-12-16 17:56:31
What operating system are your terminal servers running? Is PowerShell remoting enabled on them? And what version of PowerShell (v2 or v3) is installed on the machine you plan to run this script from and on the terminal servers?

There are a couple of PowerShell scripts on my blog that I wrote last year for querying user session information from Windows 2003 terminal servers:
by FrancoisL at 2012-12-19 05:00:38
Using PowerShell (V2) on my Windows XP machine, and i cannot use the:
get-TSSession -ComputerName $currline -State Disconnected | Stop-TSSession –Force (so i sent a issue on the Casia site, see below).

Please show small exemple or building block to do something like this:
how i would control or test any servers from my list that do not respond because ( they are down or do not have terminal Server running), and place these servers in a new list for Report_No_TS_Running.txt file.

Would like :
1) Run script againts all servers from my list_of_Servers.txt and generate:
# List All TS connection
# get-TSSession -ComputerName $currline | ft username,id,state,idletime,logintime -AutoSize

2) Run script to disconnet all session with users that are Disconnected without it stoping (if issues send out to list Report_No_TS_Running.txt file)
# List Disconnected TS connection
# get-TSSession -ComputerName $currline -State Disconnected | Stop-TSSession –Force

3) Run script to validate thate there are no more Disconnected session:
# get-TSSession -ComputerName $currline -State Disconnected | ft username,id,state,idletime,logintime -AutoSize

Since i cannot use : Stop-TSSession –Force
What steps will reproduce the problem?
I am trying to find all disconnected Terminal server session to generate a report. Then i want to disconect them with:

My report before:
ServerName: ! SYST460
UserName id State IdleTime LoginTime
-------- – ----- -------- ---------
Disconnected 00:00:00
admin456 Disconnected 5.18:23:32.0590000 2012/12/10 03:35:40
admin123 Disconnected 4.14:23:32.4250000 2012/12/12 09:39:09

get-TSSession -ComputerName $currline -State Disconnected | Stop-TSSession –Force

But i get:

ServerName: ! SYST460 = windows 2008 R2
Exception calling “Logoff” with “1” argument(s): “Access is denied”
à C:\Documents and Settings\myaccount\My Documents\WindowsPowerShell\Modules\PSTerminalServices\PSTerminalServices.psm1: ligne:631 caractère:21
+ $session.Logoff( <<<< $Synchronous)

Here with another situation:
This server was down but the script stops because i get this:

ServerName: ! SYST934
Exception calling “Open” with “0” argument(s): “The RPC server is unavailable”
à C:\Documents and Settings\myaccount\My Documents\WindowsPowerShell\Modules\PSTerminalServices\PSTerminalServices.psm1: ligne:189 caractère:24
+ $TSRemoteServer.Open( <<<< )

What version of Cassia are you using (e.g. Yes

On what Windows version is your code running?
I run it from my PC = windowx xp