IF Statement using array Values?

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
jryan
Posts: 2
Joined: Tue Apr 03, 2007 6:44 am

IF Statement using array Values?

Post by jryan » Wed May 23, 2007 11:48 pm

I need to write a vbscript that will do a command if a local group contains a certain group.

So, if the local "Power users" group on a pc contains the domain "Power Users" group I need to do a command.

I got the following to list the members of a the local "power users" group, but I can't get the IF statment to work. It returns both values "yes" and "no".

Set objGroup = GetObject("WinNT://" & strComputer & "/Power Users,group") For Each objMember in objGroup.Members If objMember.name = "Power Users" Then wscript.echo "Yes" Else wscript.echo "no" End If next

Thanks

User avatar
jryan
Posts: 2
Joined: Tue Apr 03, 2007 6:44 am

IF Statement using array Values?

Post by jryan » Wed May 23, 2007 11:48 pm

I need to write a vbscript that will do a command if a local group contains a certain group.

So, if the local "Power users" group on a pc contains the domain "Power Users" group I need to do a command.

I got the following to list the members of a the local "power users" group, but I can't get the IF statment to work. It returns both values "yes" and "no".

Set objGroup = GetObject("WinNT://" & strComputer & "/Power Users,group") For Each objMember in objGroup.Members If objMember.name = "Power Users" Then wscript.echo "Yes" Else wscript.echo "no" End If next

Thanks

User avatar
donj
Posts: 416
Joined: Thu Nov 16, 2006 9:02 pm

IF Statement using array Values?

Post by donj » Thu May 24, 2007 3:14 am

This is working fine for me, although I changed the group names to reflect ones on my computer. I suggest adding

WScript.Echo objMember.Name

Immediately before your If statement. That way you can see exactly what format the name is in. However, I guarantee the If statement is not returning "yes" AND "no" for a single group member - if you're seeing "yes" and "no" in your output, its because the group has more than one member. Adding the WScript.Echo statement as I suggest will help with that.

If you're ONLY looking for the one group, try this:

bFound = False
Set objGroup = GetObject("WinNT://" & strComputer & "/Power Users,group") For Each objMember in objGroup.Members If objMember.name = "Power Users" Then bFound = True
Exit For
End If next
WScript.Echo bFound

Locked