PowerShell
PowerShell

Reputation: 2061

Get The ServerNames where a User's Session is Disconnected

I wanted to get a script for getting the list of servers fed in through pipeline where a particular user's session is disconnected, i got someting like this, could some one please help me out.

#

#function Get-DisconnectedUsers {

#param(
#[parameter(Mandatory=$true,ValueFromPipeline=$true)]
#[string]$compnames
#)
query session /server:$compnames | where-object{ $_ -notmatch '^ SESSIONNAME' } | foreach-object{ 
$item = "" | Select "Active", "SessionName", "Username", "Id", "State", "Type", "Device"
$item.Active = $_.Substring(0,1) -match '>' 
$item.SessionName = $_.Substring(1,18).Trim() 
$item.Username = $_.Substring(19,20).Trim() 
$item.Id = $_.Substring(39,9).Trim() 
$item.State = $_.Substring(48,8).Trim() 
$item.Type = $_.Substring(56,12).Trim() 
$item.Device = $_.Substring(68).Trim() 

#$sessions | ?{ $_.State -eq 'Disc' }
#} 
#}



#$ErrorActionPreference = "silentlycontinue"
#Get-VM -VMMServer scvmm01.org |select-object vmhost,ComputerNameString | 
    #Get-DisconnectedUsers -compnames $_.vmhost | Select-Object active,username,state, |
    #Select-Object @{Name='ServerName';Expression={$_.vmhost}},@    #{Name='Username';Expression={$username}},@{Name='State';Expression={$state}}

#| where-Object{$_.user -eq "vinith"}

Could some one please help me to get this modified & working.

Upvotes: 0

Views: 1495

Answers (1)

Shay Levy
Shay Levy

Reputation: 126732

Try the PSTerminalServices PowerShell module (http://psterminalservices.codeplex.com/). The following will tell you if the Administrator account has a disconnected session on any of the servers in the file.

Import-Module PSTerminalServices

Get-Content servers.txt | Foreach-Object { 
    Get-TSSession -ComputerName $_ -UserName administrator -State Disconnected
}:

Upvotes: 1

Related Questions