May 4, 2012 at 3:31 pm
I'm using Powershell (v2.0) to get the members of local groups on remote servers. I've got that working with the get-wmiobject cmdlet with one strange exception: If a local groupname contains a $, that character seems to be ignored in the name matching. For instance, I made two test groups on a server, test and test$sjs, each containing different users. I then run
get-wmiobject -ComputerName 'PTS-GPSQL01' -query "select * from win32_groupuser where GroupComponent = `"Win32_Group.Domain='PTS-GPSQL01'`,Name='test'`""
and
get-wmiobject -ComputerName 'PTS-GPSQL01' -query "select * from win32_groupuser where GroupComponent = `"Win32_Group.Domain='PTS-GPSQL01'`,Name='test$sjs'`""
But each command returns the members of the test group. I'm using a PS script function I found at http://gallery.technet.microsoft.com/scriptcenter/List-local-group-members-762b48c5. That contains a comment saying dollar signs will screw up the WMI query if they are in the computer name and filters them out of there. Seems they might mess up the group names too. And since SQL 2005 creates groups with names like SQLServer2005MSSQLUser$PTS-GPSQL01$MSSQLSERVER, that kinda makes things difficult.
Has anyone come across a workarounds for this? Or am I missing something?
Shaun
May 4, 2012 at 5:16 pm
Try putting a back-tick in front of the $ sign, to escape it.
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
May 7, 2012 at 11:00 am
That fixed it. Thanks!
Shaun
May 7, 2012 at 11:23 am
Well, I spoke too soon. The backtick works when just issueing a command from the PS command line, but I'm trying to use this within a function, so the group name is in a variable:
$wmi = Get-WmiObject -ComputerName $ComputerName -Query "SELECT * FROM Win32_GroupUser WHERE GroupComponent=`"Win32_Group.Domain='$ComputerName',Name='$GroupName'`""
This doesn't seem to work, even when I pass in a $GroupName of test`$sjs. I tried using two backticks with no luck also.
May 7, 2012 at 11:49 am
Odd...this works fine on my system:
function x($ComputerName, $GroupName)
{
Get-WmiObject `
-ComputerName $ComputerName `
-Query "select * from win32_groupuser where GroupComponent=`"Win32_Group.Domain='$ComputerName'`,Name='$GroupName'`""
}
##############
$MyComputerName = "my-pc-name"
$MyGroupName = "SQLServer2005MSFTEUser`$$MyComputerName`$STD2005"
x $MyComputerName $MyGroupName
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
May 7, 2012 at 1:19 pm
Hmm...Yeah, if I copy and paste that to a file, it works for me too. My problem must lie elsewhere. I'm reading a list of computers and groups from a SQL table into a variable, then looping through them using the get-wmiobject to get the group members. Must be doing something wrong, like passing an object to the command instead of a string or something.
Thanks for your help!
Shaun
May 7, 2012 at 1:21 pm
You're welcome 🙂
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
May 7, 2012 at 1:40 pm
Aha! I've figured it out.. I was passing in the group name prefaced with the computer name (computername\groupname). I removed this is another script when I was getting t AD group members but forgot to remove it when getting local machine group members.
Thanks!
P.S. It appears, doing it this way (assigning the group name to a variable first, does not require you to put a ` before the $.
May 7, 2012 at 1:43 pm
Nice! Happy to assist...I bet that felt good 😉
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
May 7, 2012 at 2:06 pm
Heh..yeah. It felt good to figure it out, but also felt like a big facepalm because I had the same problem 5 days ago and forgot about it. Oh well.. Another learning experience 🙂
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply