Hi All
I wanted to use this script to find out if there are any Active or Inactive RDP sessions as my client is escalating everyday about this...My team members do not bother to log off from servers.
param(
[CmdletBinding()]
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[string[]]$ComputerName =
'localhost'
)
process {
foreach ($Computer in $ComputerName) {
quser /server:$Computer | Select-Object -Skip 1 | ForEach-Object {
$CurrentLine = $_.Trim() -Replace '\s+',' ' -Split '\s'
$HashProps = @{
UserName = $CurrentLine[0]
ComputerName = $Computer
}
# If session is disconnected different fields will be selected
if ($CurrentLine[2] -eq 'Disc') {
$HashProps.SessionName = $null
$HashProps.Id = $CurrentLine[1]
$HashProps.State = $CurrentLine[2]
$HashProps.IdleTime = $CurrentLine[3]
$HashProps.LogonTime = $CurrentLine[4..6] -join ' '
} else {
$HashProps.SessionName = $CurrentLine[1]
$HashProps.Id = $CurrentLine[2]
$HashProps.State = $CurrentLine[3]
$HashProps.IdleTime = $CurrentLine[4]
$HashProps.LogonTime = $CurrentLine[5..7] -join ' '
}
New-Object -TypeName PSCustomObject -Property $HashProps |
Select-Object -Property UserName,ComputerName,SessionName,Id,State,IdleTime,LogonTime
}
}
}
When i run this from my desktop(Win7) it shows me that i am logged in.
But when i give a server list in txt format at the above highlighted line localhost it gives me error (string[]]$ComputerName = c:\serverlist.txt)
Also let me know how to get output in txt or CSV
The term 'test.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.At line:1 char:9+ test.ps1 <<<< >> test + CategoryInfo : ObjectNotFound: (test.ps1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundExceptionSuggestion [3,General]: The command test.ps1 was not found, but does exist in the current location. Windows PowerShell doesn't load commands from the current location by default. If you trust this command, instead type ".\test.ps1". See "get-help about_Command_Precedence" for more details.Error 0x000006BA enumerating sessionnamesError [1722]:The RPC server is unavailable.
Thanks in Advance.
Alex
Alex