I have an AD group with members. Now, I want to know on what computers each member is logged on. To do that, I created the following script:
$SCCMServer = "sccmserver"
$SCCMSiteCode = "TST"
$WMIParam = @{ComputerName = $SCCMServer
NameSpace = "rootsmssite_$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 -Class "SMS_CombinedDeviceResources Where (Name like 'W10-%' and CurrentLogonUser = '$($UserMemberOfGroup.UniqueUserName)')"
Write-Host "User $($UserMemberOfGroup.UniqueUserName) is logged on to $($Computer.Name)."
}
It fails with a generic error on the query $Computer = Get-WmiObject @WMIParam -Class “SMS_CombinedDeviceResources Where (Name like ‘W10-%’ and CurrentLogonUser = ‘$($UserMemberOfGroup.UniqueUserName)’)”
If I modify the script to:
$SCCMServer = "sccmserver"
$SCCMSiteCode = "TST"
$WMIParam = @{ComputerName = $SCCMServer
NameSpace = "rootsmssite_$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 -Class "SMS_CombinedDeviceResources Where Name like 'W10-%'" | Where-Object {$_."CurrentLogonUser" -eq $($UserMemberOfGroup.UniqueUserName)}
Write-Host "User $($UserMemberOfGroup.UniqueUserName) is logged on to $($Computer.Name)."
}
Then no errors but it is extremely slow. Not a real solution.
So, my question is: what is wrong with my first script? Feedback is appreciated.
With kind regards,
TheStingPilot