remotely query group membership of machine via script/wmi

S

sumGirl

Hi all and happy holidays. I am unsure whether or not this is
possible, so excuse me in advance if this is a dumb question. I need
to make sure a particular user account is a member of the
administrators group on certain remote machines and I am hoping I can
use a script (via WMI?) to do that. I dont need to add or change user
properties, just query the remote machine to find out this info so I
can act on it later, call the user, etc...

Can someone help me with an example script of how to do this?
 
T

Torgeir Bakken \(MVP\)

sumGirl said:
Hi all and happy holidays. I am unsure whether or not this is
possible, so excuse me in advance if this is a dumb question. I need
to make sure a particular user account is a member of the
administrators group on certain remote machines and I am hoping I can
use a script (via WMI?) to do that. I dont need to add or change user
properties, just query the remote machine to find out this info so I
can act on it later, call the user, etc...

Can someone help me with an example script of how to do this?
Hi

Using ADSI is easier.

Script below assumes domain computers that you have implicit
admin access to.


'--------------------8<----------------------

' computer to connect to
sComputer = "some name or ip address"

' user name to check for
sUser = "something"

' ping the computer to see if it is available
If IsConnectible(sComputer, "", "") Then

On Error Resume Next
' try to connect to the administrator account on computer
Set oGroup = GetObject("WinNT://" & sComputer & "/Administrators")

If Err.Number = 0 Then
bUserExist = False ' init value
' was able to connect to the computer, now enumerate users
For Each oUser in oGroup.Members
If LCase(sUser) = LCase(oUser.Name) Then
bUserExist = True
End If
Next

If Err.Number <> 0 Then
WScript.Echo "Could not enumerate users in group"
Elseif bUserExist Then
WScript.Echo "User exists in group"
Else
WScript.Echo "User does not exists in group"
End If
Else
WScript.Echo "Could not connect to the computer"
End If
Else
WScript.Echo "Could not ping the computer"
End If


Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
'
' Author: Alex Angelopoulos/Torgeir Bakken
' Works an "all" WSH versions
' sHost is a hostname or IP
' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used

Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1
Dim oShell, oFSO, sTempFile, fFile

If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

sTempFile = oFSO.GetSpecialFolder(2).ShortPath & "\" & oFSO.GetTempName

oShell.Run "%comspec% /c ping.exe -n " & iPings & " -w " & iTO _
& " " & sHost & ">" & sTempFile, 0 , True

Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsASCII)

Select Case InStr(fFile.ReadAll, "TTL=")
Case 0 IsConnectible = False
Case Else IsConnectible = True
End Select

fFile.Close
oFSO.DeleteFile(sTempFile)

End Function

'--------------------8<----------------------
 
P

Peter Falz

Hi sumGirl,

sumGirl said:
Hi all and happy holidays. I am unsure whether or not this is
possible, so excuse me in advance if this is a dumb question. I need
to make sure a particular user account is a member of the
administrators group on certain remote machines and I am hoping I can
use a script (via WMI?) to do that. I dont need to add or change user
properties, just query the remote machine to find out this info so I
can act on it later, call the user, etc...
Can someone help me with an example script of how to do this?

with the following WQL-Query, you get a enumaration of all
Groups the user is associated to:

ASSOCIATORS OF
{Win32_UserAccount.Domain="[DOMAIN\WORKGROUP]",Name="[USERNAME]"}
WHERE resultClass = Win32_Group

But i agree with Torgeir, that ADSI will be the right place
to get those informations.

HTH

Ciao
Peter
 
S

sumGirl

Thanks everyone. Its wonderful to have several mousetraps for this! Happy Holidays.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top