Re: error checking
- From: "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 4 Feb 2008 23:32:17 -0600
Using "On Error Resume Next" throughout a script masks all errors, so it
becomes very difficult to troubleshoot. You may not even know there are
errors. In your snippet the "Loop" statement is missing. I would bind to the
user object outside the loop, as the same object reference can be used for
each computer (no need to repeat the bind operation). Assuming this script
is designed to be run remotely to make one specified user a member of the
local Administrators group for a list of computer, I would suggest:
=============
Option Explicit
' Declare variables.
Dim strFilename, strUser, objFSO, objTextStream
Dim strComputer, objGroup, objUser
Const FOR_READING = 1
' Open text file of computer names.
strFilename = "c:\scripts\hosts.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFSO.OpenTextFile(strFilename, FOR_READING)
' Bind to specified domain user.
strUser = "testuser"
Set objUser = GetObject("WinNT://domain/" & strUser & ",user")
' Read text file.
Do Until objTextStream.AtEndOfStream
' Trim leading and trailing blanks.
strComputer = Trim(objTextStream.ReadLine)
' Skip blank lines.
If (strComputer <> "") Then
' Bind to local Administrators group on remote computer.
' Trap error if computer does not exist or is off line.
On Error Resume Next
Set objGroup = GetObject("WinNT://" & strComputer &
"/Administrators,group")
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "Fail to bind to group on " & strComputer
Else
On Error GoTo 0
' Check if user is already a member.
If (objGroup.IsMember(objUser.AdsPath) = False) Then
' Add the domain user to the local group.
objGroup.Add(objUser.ADsPath)
End If
End If
End If
Loop
=============
The only error I see worth trapping is if the bind to the local
Administrator group fails. The computer name could be wrong or the machine
could be off line, in which case you can skip. If the user object does not
exist, an error is raised right away. You have to fix that anyway, no need
for the script to continue. Same goes if the file does not exist. Using
"Option Explicit" makes minor typos easy to spot and avoids a lot of
problems. I check for blank lines because it is common for the last line of
a text file to be blank, which raises an error.
--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--
"tony" <tony@xxxxxxx> wrote in message
news:e7LfqT7ZIHA.220@xxxxxxxxxxxxxxxxxxxxxxx
How can i improve this script for error checking
-----------------------------
on error resume next
Set objNet = WScript.CreateObject( "WScript.Network" )
Const FOR_READING = 1
strFilename = "c:\scripts\hosts.txt"
strUser = "testuser"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFSO.OpenTextFile(strFilename, FOR_READING)
Do Until objTextStream.AtEndOfStream
strComputer = objTextStream.ReadLine
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = GetObject("WinNT://domain/user")
objGroup.Add(objUser.ADsPath)
--------------------
"Richard Mueller [MVP]" <rlmueller-nospam@xxxxxxxxxxxxxxxxxxxx> wrote in
message news:OM7GDwmZIHA.208@xxxxxxxxxxxxxxxxxxxxxxx
tony wrote:
what kind of error checking can i do for a script that adds a domain
user to a local group?
I always check if the user is already a member using the IsMember method
of the group object. For example, to add the current user to a local
group:
==============
Set objNetwork = CreateObject("Wscript.Network")
strUser = objNetwork.UserName
strDomain = objNetwork.UserDomain
strComputer = objNetwork.ComputerName
' Bind to domain user object with WinNT provider.
Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
' Bind to local group object.
Set objGroup = GetObject("WinNT://" & strComputer & "/MyGroup,group")
' Check if user already a member.
If (objGroup.IsMember(objUser.AdsPath) = False) Then
' Add the user to the group.
objGroup.Add(objUser.AdsPath)
End If
==========
If you expect the user or the group might not exist, you can use "On
Error" to trap the possible errors, then restore normal error handling:
========
' Bind to domain user object.
On Error Resume Next
Set objUser = GetObject("WinNT://MyDomain/JSmith,user")
If (Err.Number <> 0) Then
Wscript.Echo "User not found"
Wscript.Quit
End If
' Bind to local group object.
Set objGroup = GetObject("WinNT://MyComputer/MyGroup,group")
If (Err.Number <> 0) Then
Wscript.Echo "Group not found"
Wscript.Quit
End If
' Restore normal error handling.
On Error GoTo 0
' Check if user already a member.
If (objGroup.IsMember(objUser.AdsPath) = False) Then
' Add the user to the group.
objGroup.Add(objUser.AdsPath)
End If
--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--
.
- References:
- error checking
- From: tony
- Re: error checking
- From: Richard Mueller [MVP]
- Re: error checking
- From: tony
- error checking
- Prev by Date: Re: error checking
- Next by Date: Re: process kill script
- Previous by thread: Re: error checking
- Next by thread: adding description for local user
- Index(es):
Relevant Pages
|