Board index » Visual Studio » How do I check for group membership?

How do I check for group membership?

Visual Studio71
I can ping a server to check its alive. I can add the proper group. What I

can't do is check to see if the group is already there!



I have looked at Hilltop Lab but can't seem to figure it out :(



'Define Variables

Dim objFSO

Dim objFile

Dim strLine

Dim arrFields

Dim strName

Dim Mydomain

Dim GlobalGroup

Dim oDomainGroup

Dim oLocalAdmGroup

Dim oNet

Dim sComputer



'CONSTANTS DECLARATION

Const LOGFILE = "C:\Documents and Settings\xxx\My

Documents\yyy\zzz\Logs\Results.Log"

Const ForReading = 1 'Open a file for reading only. You can't write to this

file.

Const ForWriting = 2 'Open a file for writing.

Const ForAppending = 8 'Open a file and write to the end of the file.



Set ofso = CreateObject("Scripting.FileSystemObject")

' initialisation of log file

InitLogFile()



' Open csv file for reading

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("C:\Documents and Settings\xxx\My

Documents\yyy\zzz\Logs\Test.csv", 1)



' Read the file one line at the time and loop through it

Do Until objFile.AtEndOfStream

strLine = objFile.ReadLine

If strLine <>"" Then

arrFields = Split(strLine, ",")

For i=0 To UBound (arrFields)

strName = arrFields(i)



'Pinger serveren først, hvis den ikke er i live, skrives en log besked

If Ping(strName) = True Then



' Add the <servername>_local_admin group to the locale Administrators group

Mydomain = "xxx"

GlobalGroup = strName & "_local_admin"



Set oNet = WScript.CreateObject("WScript.Network")

Set oDomainGroup = GetObject("WinNT://" & MyDomain & "/" & GlobalGroup &

",group")

Set oLocalAdmGroup = GetObject("WinNT://" & strName &

"/Administrators,group")

oLocalAdmGroup.Add(oDomainGroup.AdsPath)



On Error Resume Next

If Err.Number = 0 Then

WriteLogFile 0, GlobalGroup & " has been added to the Administrators

group on "

Else

WriteLogFile 1, "Got Error " & Err.Number & " - " & Err.Description &

_

". When trying to add group " & oDomainGroup & " to " & oLocalAdmGroup

End If

On Error Goto 0



Else

WriteLogFile 0, "The server is unreachable: "

End If

Next

End If

Loop



' The functions for logging



Function InitLogFile()

oFso.CreateTextFile LOGFILE,True

End Function



Function WriteLogFile(iResult, sLogInfo)

Dim sLog,fFile



If iResult = 0 Then

sLog = Date & " - " & Time & " - " & sLogInfo & strName

Elseif iResult = 1 Then

sLog = Date & " - " & Time & " - ERROR - " & sLogInfo & strName

End If



Set fFile = oFso.OpenTextFile(LOGFILE,ForAppending,True)

fFile.WriteLine sLog

fFile.Close

End Function



Function WriteEventLog(iResult, sLogInfo)

Dim oshell, Retval, sLog

Set oShell = createobject("wscript.shell")

sLog = "*** " & ucase(wscript.ScriptName) & " ***" & vbcrlf &_

VbCrLf &_

Chr(9) & "RESULT : " & sLogInfo & vbcrlf &_

VbCrLf &_

Chr(9) & "SCRIPTLOCATION : " & wscript.ScriptFullName & VbCrLf

Retval = oShell.LogEvent(iResult, sLog)

End Function



' Funktionen der pinger serverne

Function Ping(strName)



Dim objPing

Dim objRetStatus



set objPing =

GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _

("select * from Win32_PingStatus where address = '" & strName & "'")



For each objRetStatus in objPing

if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0

then

Ping = False

Else

Ping = True

End If

Next



End Function



' Confirms the script has completed and opens the file

Set objShell = CreateObject("WScript.Shell")

objShell.run ("Explorer" & " " & LogFile)



WScript.Quit


-
 

Re:How do I check for group membership?

Use the IsMember method of the local group object to check of the domain

group is already a member. It takes the same parameter as the Add method,

the AdsPath of the prospective member. For example:



============

Set oDomainGroup = GetObject("WinNT://" & MyDomain & "/" & GlobalGroup &

",group")

Set oLocalAdmGroup = GetObject("WinNT://" & strName &

"/Administrators,group")

If (oLocalAdmGroup.IsMember(oDomainGroup.AdsPath) = False) Then

oLocalAdmGroup.Add(oDomainGroup.AdsPath)

End If

============



That should handle almost all possible errors, but if you think the Add

method could raise an error, you must turn off normal error handling before

invoking the method. For example:



============

Set oDomainGroup = GetObject("WinNT://" & MyDomain & "/" & GlobalGroup &

",group")

Set oLocalAdmGroup = GetObject("WinNT://" & strName &

"/Administrators,group")

If (oLocalAdmGroup.IsMember(oDomainGroup.AdsPath) = False) Then

' Turn off normal error handling.

On Error Resume Next

oLocalAdmGroup.Add(oDomainGroup.AdsPath)

If (Err.Number = 0) Then

WriteLogFile 0, GlobalGroup & " has been added to the Administrators

group on "

Else

WriteLogFile 1, "Got Error " & Err.Number & " - " & Err.Description

& _

". When trying to add group " & oDomainGroup & " to " &

oLocalAdmGroup

End If

' Restore normal error handling. This also clears any error condition.

On Error GoTo 0

End If



--

Richard Mueller

Microsoft MVP Scripting and ADSI

Hilltop Lab - www.rlmueller.net">www.rlmueller.net

--



<mjoxp@hotmail.com>wrote in message

Quote
I can ping a server to check its alive. I can add the proper group. What I

can't do is check to see if the group is already there!



I have looked at Hilltop Lab but can't seem to figure it out :(



'Define Variables

Dim objFSO

Dim objFile

Dim strLine

Dim arrFields

Dim strName

Dim Mydomain

Dim GlobalGroup

Dim oDomainGroup

Dim oLocalAdmGroup

Dim oNet

Dim sComputer



'CONSTANTS DECLARATION

Const LOGFILE = "C:\Documents and Settings\xxx\My

Documents\yyy\zzz\Logs\Results.Log"

Const ForReading = 1 'Open a file for reading only. You can't write to

this file.

Const ForWriting = 2 'Open a file for writing.

Const ForAppending = 8 'Open a file and write to the end of the file.



Set ofso = CreateObject("Scripting.FileSystemObject")

' initialisation of log file

InitLogFile()



' Open csv file for reading

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("C:\Documents and Settings\xxx\My

Documents\yyy\zzz\Logs\Test.csv", 1)



' Read the file one line at the time and loop through it

Do Until objFile.AtEndOfStream

strLine = objFile.ReadLine

If strLine <>"" Then

arrFields = Split(strLine, ",")

For i=0 To UBound (arrFields)

strName = arrFields(i)



'Pinger serveren først, hvis den ikke er i live, skrives en log besked

If Ping(strName) = True Then



' Add the <servername>_local_admin group to the locale Administrators

group

Mydomain = "xxx"

GlobalGroup = strName & "_local_admin"



Set oNet = WScript.CreateObject("WScript.Network")

Set oDomainGroup = GetObject("WinNT://" & MyDomain & "/" & GlobalGroup &

",group")

Set oLocalAdmGroup = GetObject("WinNT://" & strName &

"/Administrators,group")

oLocalAdmGroup.Add(oDomainGroup.AdsPath)



On Error Resume Next

If Err.Number = 0 Then

WriteLogFile 0, GlobalGroup & " has been added to the Administrators

group on "

Else

WriteLogFile 1, "Got Error " & Err.Number & " - " & Err.Description &

_

". When trying to add group " & oDomainGroup & " to " &

oLocalAdmGroup

End If

On Error Goto 0



Else

WriteLogFile 0, "The server is unreachable: "

End If

Next

End If

Loop



' The functions for logging



Function InitLogFile()

oFso.CreateTextFile LOGFILE,True

End Function



Function WriteLogFile(iResult, sLogInfo)

Dim sLog,fFile



If iResult = 0 Then

sLog = Date & " - " & Time & " - " & sLogInfo & strName

Elseif iResult = 1 Then

sLog = Date & " - " & Time & " - ERROR - " & sLogInfo & strName

End If



Set fFile = oFso.OpenTextFile(LOGFILE,ForAppending,True)

fFile.WriteLine sLog

fFile.Close

End Function



Function WriteEventLog(iResult, sLogInfo)

Dim oshell, Retval, sLog

Set oShell = createobject("wscript.shell")

sLog = "*** " & ucase(wscript.ScriptName) & " ***" & vbcrlf &_

VbCrLf &_

Chr(9) & "RESULT : " & sLogInfo & vbcrlf &_

VbCrLf &_

Chr(9) & "SCRIPTLOCATION : " & wscript.ScriptFullName & VbCrLf

Retval = oShell.LogEvent(iResult, sLog)

End Function



' Funktionen der pinger serverne

Function Ping(strName)



Dim objPing

Dim objRetStatus



set objPing =

GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _

("select * from Win32_PingStatus where address = '" & strName & "'")



For each objRetStatus in objPing

if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0

then

Ping = False

Else

Ping = True

End If

Next



End Function



' Confirms the script has completed and opens the file

Set objShell = CreateObject("WScript.Shell")

objShell.run ("Explorer" & " " & LogFile)



WScript.Quit









-