Re: Need WMI script



Thank you mueller. cscript worked fine to me.
Below is my code. my requirement is to display the members in the group
down to the user. input(domain\grpname) comes from the xml file. In .net i am
able to retrieve the user list down to the user. but here in vbscript i am
able to list down the users of few groups in my domain. it will not allow me
to access the other domain users. I don't know the reason. no error is
coming. Please check my code and suggest me the best way.

Option Explicit

Dim adoCommand, adoConnection, strBase, strFilter, strAttributes, xmlDoc, x,
WshNetwork, strComputer
Dim strDomainName, strNodeText, strXmlBuilder
Dim fso, res

Dim strQuery, adoRecordset, strDN, strGroupName

' Setup ADO objects.

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputer = UCase(WshNetwork.ComputerName)

'Set fso = CreateObject("Scripting.FileSystemObject")
'Set res = fso.CreateTextFile(strComputer & ".xml", True)
'strXmlBuilder = strXmlBuilder & "<?xml version=\"1.0\" standalone=\"yes\"?>"
'strXmlBuilder = strXmlBuilder & "\n" + "<WINNTGrps>"

'res.WriteLine "setmachine= & SerialNumber
'res.Close


set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("groupstoberesolved.xml")

for each x in xmlDoc.documentElement.childNodes

strNodeText = replace(x.text,"\","/")

if x.nodename <> "#comment" then
Wscript.Echo "Node Name:" & x.nodename & vbcrlf & "Node Text:" &
strNodeText & vbcrlf & "Domain Name:" & _
mid(x.text,1, InStr(strNodeText,"/")-1) _
& vbcrlf & "GroupName:" & mid(x.text,InStr(strNodeText,"/") + 1,
len(strNodeText))

strDomainName = mid(x.text,1, InStr(strNodeText,"/")-1)
strGroupName = mid(x.text,InStr(strNodeText,"/") + 1, len(strNodeText))

strBase = "<LDAP://"; & strDomainName & ">"

strFilter = "(&(|(objectClass=user)(objectclass=group))" &
"(sAMAccountName=" & strGroupName & "))"
strAttributes = "distinguishedName"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
'Wscript.Echo strQuery
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

' Run the query.
Set adoRecordset = adoCommand.Execute


' Enumerate the resulting recordset.
Do Until adoRecordset.EOF

' Retrieve values and display.
strDN = adoRecordset.Fields("distinguishedName").Value

enumMembers getGroup(strDN,strGroupName), ""
adoRecordset.MoveNext
Loop
end if
next

' Clean up.

adoRecordset.Close
adoConnection.Close


Function getGroup(strQueryFrom,strGroupName)
Dim objConn, objRecSet, strQueryString
Const adsOpenStatic = 3

Set objConn = wscript.CreateObject("ADODB.Connection")
objConn.Provider = "ADsDSOObject"
objConn.Open
wscript.echo strQueryFrom
strQueryString = "SELECT * FROM 'LDAP://"; & strQueryFrom & "' " & "
WHERE samAccountName = '" & strGroupName & "'"

Set objRecSet = wscript.CreateObject("ADODB.Recordset")

objRecSet.Open strQueryString, objConn, adsOpenStatic

If objRecSet.recordCount = 1 Then
dim objFlds
for each objFlds In objRecSet.Fields
' wscript.echo objFlds.Name
next
Set getGroup = GetObject(objRecSet("AdsPath"))

Else
wscript.echo "Record Cnt: " & objRecSet.recordCount & vbcrlf & "Query:
" & strQueryString ' ucase(strGroupName) & " was not found in the domain. ("
& objRootDSE.get("defaultNamingContext") & ")"
wscript.quit
End If
End Function


Sub enumMembers(byRef objGroup, strInheritedFrom)
Dim objMember

For Each objMember In objGroup.Members
If lcase(objMember.class) = "group" Then
enumMembers objMember, objMember.samAccountName
Else
If objMember.displayname <> "" Then
If strInheritedFrom = "" Then
wscript.echo objMember.displayname
Else
wscript.echo objMember.displayname & " (From NESTED GROUP: " &
strInheritedFrom & ")"
End If
Else
If strInheritedFrom = "" Then
wscript.echo objMember.samAccountName
Else
wscript.echo objMember.samAccountName & " (From NESTED GROUP: " &
strInheritedFrom & ")"
End If
End If
End If

Next
End Sub

"Richard Mueller [MVP]" wrote:

If you run the script with the wscript host, Wscript.Echo commands result in
a message box with an OK button. However, if you use the cscript host,
Wscript.Echo echos to the console. If the VBScript program is called
Example.vbs, you can use a command similar to below at a command prompt:

cscript Example.vbs

I often use the //nologo optional parameter to suppress the WSH logo
information, especially if I want to redirect the output to a text file. For
example:

cscript //nologo Example.vbs > output.txt

The above assumes you are in the directory where the file Example.vbs is
saved. Otherwise, you have to specify the full path and file name in the
command.

I see that on most systems the default host is wscript, so if you just run
the vbs file, wscript.exe is used and the Wscript.Echo statements result in
message boxes. This may be what you experience. For example, that most
likely happens if you enter:

example.vbs
or
example

The default host can be changed to cscript. My reference gives the following
command to set the default to cscript:

cscript //H:cscript //S

The //S option saves this setting for the user. The command:

cscript //I //nologo //H:cscript //S

saves 3 options for the particular user (on this computer), the //I option
specifies interactive mode (which I think is the default anywayt), //nologo
suppresses the WSH logo information, //H:cscript assigns cscript.exe as the
default host, and //S saves the settings for the user.

I don't alter the settings, since I've gotten into the habit of always using
cscript. When I tried a VBScript program just now with no host specified, I
saw how annoying the default wscript.exe can be.

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--

"Sriman" <Sriman@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:379C3080-99DB-43AC-B8F7-955A878BE47C@xxxxxxxxxxxxxxxx
Yes, If i use the Wscript.Echo its giving alert with ok button. I am
looking
for the code which will displays the output to the console.

"Richard Mueller [MVP]" wrote:


"Sriman" <Sriman@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:8B3A9920-4D17-41FE-8069-8F3224F6F169@xxxxxxxxxxxxxxxx
Hi Mueller,

Thanks for your reply.
I have question. Is there any method which will print the out put(other
than
into file) other than Wscript.Echo ..like write, print etc., My
requirement
is to display the status of the group and down to the user details to
the
admin and write those details to file. using filesystem we can write
into
file.But how to display the details?

Regards,
Sri

I don't know of any way to print from a VBScript program. I generally use
Wscript.Echo commands to echo information to the console. I can redirect
the
output to a text file, using the ">" redirection character. You can also
use
the FileSystemObject to write information to a text file.

You can also use the MsgBox function to display information to users.

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--






.