Re: remotely query group membership of machine via script/wmi

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Torgeir Bakken \(MVP\) (Torgeir.Bakken-spam_at_hydro.com)
Date: 11/29/04

  • Next message: Peter Falz: "Re: remotely query group membership of machine via script/wmi"
    Date: Mon, 29 Nov 2004 22:32:17 +0100
    
    

    sumGirl wrote:

    > 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<----------------------

    -- 
    torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
    Administration scripting examples and an ONLINE version of
    the 1328 page Scripting Guide:
    http://www.microsoft.com/technet/scriptcenter/default.mspx
    

  • Next message: Peter Falz: "Re: remotely query group membership of machine via script/wmi"

    Relevant Pages

    • Re: Im new to scripting a I working on a project that I need some help with.
      ... After that the script ... >> that the user account is not valid. ... > it to the Administrators group: ... If the user does not have it, maybe a RunAs solution ...
      (microsoft.public.scripting.vbscript)
    • Re: How do I create a locked down user account with only access to Ping?
      ... > I want to create a User Account that does something similar to the ... Ideally they wouldn't have a home directory. ... shell script that only allows a user to execute ping. ...
      (comp.unix.solaris)
    • Re: [SLE] emailing myself when a ping command fails via crontab????
      ... but I have a perl script to monitor a webserver. ... First the script tries to ping it's DNS servers, ...
      (SuSE)
    • RE: ICMP (Ping)
      ... script to locate vulnerabilities(example point and case: ... Is it good to allow UDP ping packets? ... Guys again, I am not saying that you disable pings and walk away, job ... 300K and you guys are right, it port scans like a freaking demon. ...
      (Security-Basics)
    • Re: Memory consumption PowerShell
      ... The mechanism of the Identity Management system (it is Novell Identity ... And we could initiate to run a script oout side the IDN 3.0. ... is the case we want to do, if a new user account was created. ... memory. ...
      (microsoft.public.windows.server.scripting)