Re: Need WMI script



Error handling in VBScript is obviously not as good as in .NET. However, you
can suspend normal error handling with "On Error Resume Next", then restore
normal error handling with "On Error GoTo 0". You can use the builtin Err
object to check for errors.

I suspend normal error handling for the statements I expect might raise an
error, handle the error if it is raised, then restore normal error handling.
This way, if something unexpected happens, the problem is not ignored and I
get an error message (and line number) to help troubleshoot.

If you lack rights in the other domain, for example, an error might be
raised when you attempt to bind to a group. You could trap this error with
code similar to:
===========
On Error Resume Next
Set objGroup = GetObject("LDAP://"; & strDN)
If (Err.Number <> 0) Then
' Error raised.
Wscript.Echo "Error attempting to bind to group " & strDN
Wscript.Echo "Error Number: " & Err.Number
Wscript.Echo "Description: " & Err.Description
On Error GoTo 0
Else
On Error GoTo 0
enumMembers objGroup, ""
End If
===========

If you are having trouble with groups in other domains, I wonder if using
the Global Catalog will help. Assuming the domains have trusts, the GC
should have partial information on all objects in the forest. This may make
the NameTranslate object a good choice in your case, for converting NT names
in the form MyDomain\TestGroup into Distinguished Names. In any case, if
NameTranslate cannot find the object, an error is raised by the Set method.
For example, the code I posted earlier can be modified to trap this possible
error as follows:
=============
' Constants for the NameTranslate object.

Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

' Specify values, or parse from XML file.
strDomainName = "MyDomain"
strGroupName = "TestGroup"

' Use the NameTranslate object to convert the NT name to the
' Distinguished Name required for the LDAP provider.
Set objTrans = CreateObject("NameTranslate")

' Initialize NameTranslate by locating the Global Catalog.
objTrans.Init ADS_NAME_INITTYPE_GC, ""

' Use the Set method to specify the NT format of the object name.
' Trap possible error.
On Error Resume Next
objTrans.Set ADS_NAME_TYPE_NT4, strDomainName & "\" & strGroupName
If (Err.Number = 0) Then
' No Error.
On Error GoTo 0
' Use the Get method to retrieve the RPC 1779 Distinguished Name.
strGroup = objTrans.Get(ADS_NAME_TYPE_1779)

' Bind to the group object.
Set objGroup = GetObject("LDAP://"; & strGroupDN)

enumMembers objGroup, ""
Else
Wscript.Echo "Cannot find group " & strDomainName & "\" & strGroupName
Wscript.Echo "Error Number: " & Err.Number
Wscript.Echo "Description: " & Err.Description
On Error GoTo 0
End If

I hope this helps.

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

"Sriman" <Sriman@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:2A7AD010-F5E2-4493-96A6-FE49686C895D@xxxxxxxxxxxxxxxx
Hi Mueller,

How to handle the errors in vbscript. right now i have written "on error
resume next". but i want to store the groupnames which has descripancy,
like
i don't have right to execute the few domains , at that time application
throwing errors. i was handled the error in .net perfectly and able to
separate the not worked groups.

is there any way to handle the errors like in .net?

Regards,
Sri

"Sriman" wrote:

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








.


Loading