2011-12-23 79 views
3

我們的團隊地理位置分散,許多虛擬機將通過遠程桌面與他們連接。我想查找誰正在訪問遠程桌面會話以及使用多長時間。如何查找mstsc的使用時間以及由誰使用?

我試圖用powershell來做到這一點。我寫了一個腳本,用戶將使用powershell調用mstsc。它將記錄誰已經登錄和何時登錄。但我想找到什麼時候有人從mstsc註銷或斷開mstsc。有什麼方法可以使用PowerShell在日誌文件中捕獲這些信息。在關閉可用於它的mstsc時是否觸發任何事件?

回答

5

I編寫了一個基於Cassia的PowerShell模塊,PSTerminalServices(http://psterminalservices.codeplex.com)。 這裏的一個示例命令輸出:

PS> Get-TSSession | fl * 

IPAddress   : 
State    : Active 
ApplicationName : 
Local    : False 
RemoteEndPoint  : 
InitialProgram  : 
WorkingDirectory : 
ClientProtocolType : Console 
ClientProductId : 0 
ClientHardwareId : 0 
ClientDirectory : 
ClientDisplay  : Cassia.Impl.ClientDisplay 
ClientBuildNumber : 0 
Server    : Cassia.Impl.TerminalServer 
ClientIPAddress : 
WindowStationName : Console 
DomainName   : homelab 
UserAccount  : homelab\shay 
ClientName   : 
ConnectionState : Active 
ConnectTime  : 12/15/2011 2:47:02 PM 
CurrentTime  : 12/23/2011 4:35:21 PM 
DisconnectTime  : 
LastInputTime  : 
LoginTime   : 12/15/2011 3:11:58 PM 
IdleTime   : 00:00:00 
SessionId   : 1 
UserName   : shay 
+0

我經常使用這個模塊(基本上與OP需要它相同的原因,找出我的虛擬機的使用模式),我可以強烈推薦它。 – Winfred 2011-12-23 17:06:20

+0

@Shay Levy:我收到這個錯誤。使用「0」參數調用「Open」的異常:「RPC服務器不可用」.Port 445已打開但仍在運行。 – Samselvaprabu 2012-01-23 13:33:05

+0

@Winfred:我可以知道你如何使用它?我的意思是,你會每天晚上用ip地址列表執行,並查明今天是否使用了這些機器?或者你如何確定使用模式?如果你希望我可以把它作爲一個單獨的問題。 – Samselvaprabu 2012-01-23 13:36:29

3

您可以使用Cassia獲取rdp會話信息(可定期記錄到日誌文件中)。

下面是如何在PowerShell中使用決明子一個簡單的例子:

[reflection.assembly]::loadfile("d:\cassia.dll") 
$manager = new-object Cassia.TerminalServicesManager 
$server = $manager.GetRemoteServer("<name of your server>") 
$server.open() 
$server.getsessions() 

它會返回這樣的事情(爲每個會話):

ClientDisplay  : Cassia.Impl.ClientDisplay 
ClientBuildNumber : 0 
Server   : Cassia.Impl.TerminalServer 
ClientIPAddress : 
WindowStationName : 
DomainName  : CONTOSO 
UserAccount  : CONTOSO\admin 
ClientName  : 
ConnectionState : Disconnected 
ConnectTime  : 22/12/2011 19:02:00 
CurrentTime  : 23/12/2011 9:00:42 
DisconnectTime : 22/12/2011 22:22:35 
LastInputTime  : 22/12/2011 22:22:35 
LoginTime   : 22/12/2011 10:40:21 
IdleTime   : 10:38:06.4220944 
SessionId   : 33 
UserName   : admin 
+0

我試着執行並得到以下錯誤。我也搜索了該鏈接,但沒有有用的信息。使用「0」參數調用「打開」的異常:「RPC服務器不可用」 在D:\ My_Scripts \ Mstsc-monitor.ps1:4 char:13 + $ server.Open <<<<() + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId:DotNetMethodException – Samselvaprabu 2011-12-23 09:34:48

+0

您是否用服務器的主機名替換<服務器名稱>? – 2011-12-23 09:45:23

+0

我在服務器的地方使用了IP地址。 – Samselvaprabu 2011-12-23 09:52:36

1

我每運行15分鐘這個函數一次,它依賴於模塊PSTerminalServices。基本上它的作用是,它拉取上一次RDP進來的人,然後將其存儲在XML中,如果存在,則覆蓋舊值,如果當前沒有人登錄,則返回XML中的最新值。

Function Get-LastLogonTime 
{ 
<# 

.SYNOPSIS 

Get-LastLogonTime returns the last date that someone logged on to a computer. 

.DESCRIPTION 

Get-LastLogonTime returns the last date that someone logged to a computer. 
If admin rights are missing on the server it will return False. 

.EXAMPLE 

Get-LastLogonTime "nameofcomputer" 

.NOTES 

gets last access time from the user folder 

.LINK 

http://winfred.com 
#> 
Param(
[Parameter(Position=0, Mandatory=$true)]$ComputerName 
) 
    $StoredRDPSessions = Import-Clixml "RDPSessions.xml" 

    $myobj = "" | select ComputerName, LastAccessedDate, UserName 
    $myobj.ComputerName = $ComputerName 
    $LastConnectedUser = Get-TSSession -ComputerName $ComputerName | where ` 
    { 
     ($_.WindowStationName -ne "Services") -and ` 
     ($_.State -ne "Listening") -and ` 
     ($_.WindowStationName -ne "Console") 
    } | sort-object -property LastAccessTime -Descending 
    if($LastConnectedUser -is [array]) 
    { 
     $myobj.LastAccessedDate = $LastConnectedUser[0].ConnectTime 
     $myobj.UserName = $LastConnectedUser[0].UserName 
    }elseif($LastConnectedUser){ 
     $myobj.LastAccessedDate = $LastConnectedUser.ConnectTime 
     $myobj.UserName = $LastConnectedUser.UserName 
    }else{ 
     $myobj.LastAccessedDate = $Null 
     $myobj.UserName = "Unknown" 
    } 
    if(($myobj.LastAccessedDate) -and ($myobj.UserName)) 
    { 
     $StoredRDPSession = $StoredRDPSessions | where {$_.ComputerName -eq $ComputerName} 
     if($StoredRDPSession) 
     { 
      if($myobj.LastAccessedDate -gt $StoredRDPSession.LastAccessedDate) 
      { 
       write-verbose "Newer LastAccessedDate, updating XML" 
       $StoredRDPSession.LastAccessedDate = $myobj.LastAccessedDate 
       $StoredRDPSession.UserName = $myobj.UserName 
       $StoredRDPSessions | Export-Clixml "RDPSessions.xml" 
      } 
     }else{ 
      write-verbose "No Entry found Adding to XML" 
      $NewStoredRDPSessions = @() 
      $StoredRDPSessions | % {$NewStoredRDPSessions += $_} 
      $NewStoredRDPSessions += $myobj 
      $NewStoredRDPSessions | Export-Clixml "RDPSessions.xml" 
     } 
    } 

    if((!($myobj.LastAccessedDate)) -and $StoredRDPSessions) 
    { 
     write-verbose "no current session, pulling from stored XML" 
     $StoredRDPSession = $StoredRDPSessions | where {$_.ComputerName -eq $ComputerName} 
     if($StoredRDPSession) 
     { 
      $myobj.LastAccessedDate = $StoredRDPSession.LastAccessedDate 
      $myobj.UserName = $StoredRDPSession.UserName 
     }else{ 
      write-verbose "Sadness, nothing stored in XML either." 
     } 
    } 
    write-verbose "Get-LastLogonTime $ComputerName - $($myobj.LastAccessedDate) - $($myobj.UserName)" 
    Return $myobj 
} 
相關問題