目前我们正在尝试在Windows Server 2008 R2域中的Powershell中创建一个脚本,该脚本会注销任何有两个活动会话的用户,我们要完成的是从第二个会话中注销用户进行登录
这是脚本
$ScriptUsers = "x", "y"
$MaxSessions = 1
$Sessions = 0;
$LogoffCommand = $env:systemroot + "\system32\logoff.exe"
$TSServers = Get-ADComputer -Filter *
foreach ($Server in $TSServers.ServerName)
{
$CurrentUser = $env:username.Trim()
if ($ScriptUsers -inotcontains $CurrentUser)
{
return
}
$WmiUserNameExpression = "*,Name=`"" + $CurrentUser + "`""
$Sessions = $Sessions + get-wmiobject Win32_LoggedOnUser | `
select Antecedent | `
where { $_.Antecedent -ilike $WmiUserNameExpression } | `
measure-object
if ($Sessions.Count -gt $MaxSessions)
{
&$LogoffCommand
$Sessions = $Sessions-1;
}
}
然而,它似乎仅在本地计算机中而不是在域中进行会话,我们如何解决此问题以便监视域中的每个会话并注销任何其他并发会话?另外,我们如何将用户在域PC上的会话数作为参数传递?