我有一个 AD 小组,有成员。现在,我想知道每个成员登录在哪些计算机上。为此,我创建了以下脚本:
$SCCMServer = "sccmserver"
$SCCMSiteCode = "TST"
$WMIParam = @{ComputerName = $SCCMServer
NameSpace = "root\sms\site_$SCCMSiteCode"}
$UsersMemberOfGroup = (Get-WMIObject @WMIParam -Query "select SMS_R_User.UniqueUserName from SMS_R_User where SMS_R_User.UserGroupName LIKE '%A_SQL_SERVER_MANAGEMENT_STUDIO'" | Select-Object -Property UniqueUserName)
ForEach ($UserMemberOfGroup in $UsersMemberOfGroup)
{
$Computer = Get-WmiObject @WMIParam -Query "Select Name, CurrentLogonUser From SMS_CombinedDeviceResources Where (Name like 'W10-%' and CurrentLogonUser = '$($UserMemberOfGroup.UniqueUserName)')"
Write-Host "User $($UserMemberOfGroup.UniqueUserName) is logged on to $($Computer.Name)."
}
查询失败并出现一般错误 $Computer = Get-WmiObject @WMIParam -Class "SMS_CombinedDeviceResourcesWhere (Name like 'W10-%' and CurrentLogonUser = '$($UserMemberOfGroup.UniqueUserName)')"
如果我将脚本修改为:
$SCCMServer = "sccmserver"
$SCCMSiteCode = "TST"
$WMIParam = @{ComputerName = $SCCMServer
NameSpace = "root\sms\site_$SCCMSiteCode"}
$UsersMemberOfGroup = (Get-WMIObject @WMIParam -Query "select SMS_R_User.UniqueUserName from SMS_R_User where SMS_R_User.UserGroupName LIKE '%A_SQL_SERVER_MANAGEMENT_STUDIO'" | Select-Object -Property UniqueUserName)
ForEach ($UserMemberOfGroup in $UsersMemberOfGroup)
{
$Computer = Get-WmiObject @WMIParam -Query "Select Name, CurrentLogonUser From SMS_CombinedDeviceResources Where Name like 'W10-%'" | Where-Object {$_."CurrentLogonUser" -eq $($UserMemberOfGroup.UniqueUserName)}
Write-Host "User $($UserMemberOfGroup.UniqueUserName) is logged on to $($Computer.Name)."
}
然后没有错误,但速度非常慢。这不是真正的解决方案。
所以,我的问题是:我的第一个脚本有什么问题?感谢反馈。
谨致问候, 毒刺飞行员
我已经找到解决办法了。正确的行是:
$Query = "Select Name, CurrentLogonUser From SMS_CombinedDeviceResources Where (Name like 'vm-%' and CurrentLogonUser = '$(($UniqueUserName.UniqueUserName).Replace('\','\\'))')"
UniqueUserName 的格式为 DOMAIN\USER。所以我不得不将 '\' 更改为 '\' 因为它应该被转义字符转义。