我有一個Powershell腳本,它獲取遠程桌面用戶會話的列表,並將它們通過SessionID放入散列。Powershell散列包含鍵不正確返回錯誤
# Create a hashtable which contains all of the remote desktop sessions by SessionId
$userSessionBySessionID = @{}
ForEach($userSession in Get-RDUserSession -CollectionName $collectionName)
{
$userSessionBySessionID.Add($userSession.SessionId, $userSession)
}
然後我就可以轉儲$ userSessionByID在PowerShell ISE中
Name Value
---- -----
9 Microsoft.RemoteDesktopServices.Management.RDUserSession
8 Microsoft.RemoteDesktopServices.Management.RDUserSession
7 Microsoft.RemoteDesktopServices.Management.RDUserSession
6 Microsoft.RemoteDesktopServices.Management.RDUserSession
5 Microsoft.RemoteDesktopServices.Management.RDUserSession
4 Microsoft.RemoteDesktopServices.Management.RDUserSession
2 Microsoft.RemoteDesktopServices.Management.RDUserSession
1 Microsoft.RemoteDesktopServices.Management.RDUserSession
令人沮喪的是,$ userSessionBySessionID.ContainsKey(4)返回false。我在這裏錯過了什麼?我也試過$ userSessionBySessionID.ContainsKey(「4」),但是這也返回false。
sessionid是什麼類型? '$ userSession.SessionId.Gettype()'返回什麼? –
我認爲問題可能是$ userSession.SessionId.GetType()返回UInt32,而Get-Process返回SessionId是Int32的對象。 – bpeikes
這是否意味着'$ userSessionBySessionID.ContainsKey(4 -as [uint32])'有效? – Matt