I have create VBscript to enumerate user in group
Function GetUserInGroup()
strComputer = "localhost"
Set colGroups = GetObject("WinNT://" & strComputer & "")
colGroups.Filter = Array("group")
For Each objGroup In colGroups
For Each objUser in objGroup.Members
If objUser.name = "yayantritaryana" Then
WScript.stdout.write objGroup.Name + " "
End If
Next
Next
End Function
WScript.stdout.write "Group=" + GetUserInGroup
But when i execute it, the output is'nt what i expected
The output I Wanted is like
Group=Administrator SQLAdmin Sysadmin
Can someone help me ?
A Function has a purpose (eg. delivering a string of space separated group names). For that you assign the desired result to the function's name (other languages use some kind of return statement):
Function GetUserInGroup()
GetUserInGroup = "pi pa po"
End Function
WScript.stdout.write "Group=" & GetUserInGroup()
output:
cscript 29053176.vbs
Group=pi pa po
A function shouldn't have side-effects (like your
WScript.stdout.write objGroup.Name + " "
which prints names to the console before
you output "Group=" in the last line of your script). Instead concatenate the objGroup.Names.
The operator for string concatenation is &.
The (possibly empty) argument list of a function call must be enclosed by (); these param list () are illegal for Sub calls.
Some extra code as food for thought:
Function GetUserInGroup()
For Each s In Split("pi pa po")
GetUserInGroup = GetUserInGroup & s & "*"
' GetUserInGroup = GetUserInGroup() & s & "*"
Next
End Function
WScript.stdout.write "Group=" & GetUserInGroup()