我正在使用 Shay 的(太棒了!)PSTerminalServices Powershell 模块来查找整个 TS 场中断开连接的会话。我在我的 Windows 7 x64 工作站(使用 PS ISE x86)上开发了脚本(在 Stackoverflow 的帮助下!)。我已经将代码部署到我测试它的新 Windows 2012 进程服务器(再次在 ISE x86 中)......并且它引发了异常。
代码(适用于 Windows 7)是:
#variables
$SubjectLine = "Disconnected Session Discovered on Terminal Server"
$Sender = "noreply@example.com"
$Recipient = "test@example.com"
$Attachment = ""
$Exceptions = ""
$BodyPart1 = "You are being sent this email because you have a disconnected session on a Terminal Server. The session is as follows: `r`n`r`n"
$BodyPart3 = "Please log back on to this terminal server, close all data entry systems and log off properly (via the Start menu). Please do not click the the top-right X as this only closes the remote window. Following this procedure will help prevent data/index corruption and other work-stopping issues. `r`n`r`n Thank you"
#terminalservers.txt contains a list of terminal servers to check
$tsservers = Get-Content ".\terminalservers.txt"
foreach ($i in $tsservers){
Get-TSSession -ComputerName $i | ? { $_.state -eq "Disconnected" } | % {
$username = $_ | select -ExpandProperty UserName
$server = $_ | select -ExpandProperty Server
#>>>>THESE next two lines work on Windows 7 but not on Server 2012...?<<<<
$connect = $_ | select -ExpandProperty ConnectTime
$disconnect = $_ | select -ExpandProperty DisconnectTime
$query = "SELECT * from ds_user where ds_sAMAccountName='$username'"
$user = Get-WmiObject -Query $query -Namespace "root\Directory\LDAP"
$user.DS_mail
#build email body
Body = $BodyPart1 + "Username: " + $username + "`r`n" + "Terminal Server Name: " + $server.ServerName + "`r`n" + "Connect Time: " + $connect.DateTime + "`r`n" + "Disconnect Time: " + $disconnect.DateTime + "`r`n`r`n`r`n" + $BodyPart3
#send email to user
SendMail $SubjectLine $Body $Sender $Recipient
}
}
我无法在 Windows 2012 中检索的两个属性是:ConnectTime 和 DisconnectTime。在 Server 2012 上运行时,我收到错误消息:
select :无法处理参数,因为参数“obj”的值为空。将参数“obj”的值更改为非空值。在 C:\somefolder\terminal_server_session_reporter.ps1:39 char:26 + $connect = $_ | 选择 -ExpandProperty ConnectTime + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Select- Object], PSArgument NullException + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.SelectedObjectCommand
select :无法处理参数,因为参数“obj”的值为空。将参数“obj”的值更改为非空值。在 C:\somefolder\terminal_server_session_reporter.ps1:39 char:26 + $connect = $_ | 选择 -ExpandProperty ConnectTime + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Select- Object], PSArgument NullException + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.SelectedObjectCommand
只是这两个属性会导致问题。如果我将它们注释掉,则代码在 Server 2012 上运行良好。我已经确认我正在运行相同版本的 PSTerminalServices 模块以及 .NET Framework 4 和 cassia.dll。两台机器都运行相同版本的 Powershell 3。我可以在省略这两个属性的情况下部署此代码……但它们实际上很有帮助,如果可能的话,我想包括它们。
有没有人对为什么这在 Server 2012 上不起作用或如何修复它有任何建议?
哦,我还想提一下,以下代码确实适用于 Server 2012:
Get-TSSession -computername terminalserver1 | Select ConnectedTime, DisconnectedTime
...所以看起来这些属性在以这种方式访问时是可用的...我很困惑。
编辑 1:我刚刚确认 Windows Server 2008 R2 上存在相同的问题(所有模块、框架等的相同版本都是最新的)。