Re: On error resume next on encoded VB script
- From: "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 7 Feb 2008 16:26:58 -0600
"Bob Butler" <noway@xxxxxxxxxxx> wrote in message
news:eJD0oRdaIHA.3400@xxxxxxxxxxxxxxxxxxxxxxx
"Joe V" <jnv0529@xxxxxxxxx> wrote in message
news:aeddbc5a-f033-49e6-b34a-47b3cad69226@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hello,
I have a vb script that changes the local admin password of a
machine. My company has offices in U.S. and Canada so we have two
different usernames for the local admin account, one in English one in
French depending on where you are located. My script tries to change
the password for both. I have it set to resume if there is an error
since only one of the usernames will actually work. The program works
fine as a vbscript but after using the Windows Script Encoder it fails
if the first username that it tries to change doesn't exist. No I
can't have just one username, we have to have one in English and
French.
Here is the code (the only thing I changed was the actuall password
and usernames:
------------------------------------------------------------------------------------------
Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputer = WshNetwork.ComputerName
strPSWD = "Password"
'If username does not exist ignore error
On Error Resume Next
'Change password for French local admin account
Set objUser = GetObject("WinNT://" & strComputer & "/Frenchname,user")
If Err.Number = 0 Then
objUser.SetPassword strPSWD
objUser.SetInfo
Else
Err.Clear
'Change password for English local admin account
Set objUser = GetObject("WinNT://" & strComputer & "/
Englishname,user")
If Err.Number = 0 Then
objUser.SetPassword strPSWD
objUser.SetInfo
End If
End If
I think the added Err.Clear statement probably fixes the program, but I
would code it a bit differently. I would use:
===========
Set WshNetwork = CreateObject("WScript.Network")
strComputer = WshNetwork.ComputerName
strPSWD = "Password"
' Attempt to bind to French local admin account.
On Error Resume Next
Set objUser = GetObject("WinNT://" & strComputer & "/Frenchname,user")
If (Err.Number <> 0) Then
' Error raised, clear error condition.
Err.Clear
' Attempt to bind to English local admin account.
If objUser = GetObject("WinNT://" & strComputer & "/EnglishName,user")
If (Err.Number <> 0) Then
' Error raised.
Wscript.Echo "Unable to bind to local admin account"
Wscript.Quit
End If
End If
On Error GoTo 0
objUser.SetPassword strPSWD
===========
The "On Error GoTo 0" statement restores normal error handling, so you know
if something else goes wrong, like maybe you lack permission to change the
password. It also clears any error condition. Note that the SetPassword
method works immediately so you do not need to invoke the SetInfo method.
I believe the code will work the same as a VBScript or VB program. I don't
believe you can use early binding for local objects in the SAM account
database, so this about the best you can code. Note also that I use
"CreateObject" in place of "Wscript.CreateObject" when binding to the
wshNetwork object.
Finally, since you are changing the password, and may have coded in VB
rather than VBScript to "Hide" the password, note that the password can
still be viewed in the *.exe file. You may want to run the VBScript program
remotely yourself. Normal users cannot normally change Administrator
passwords, but if you are a member of the "Domain Admins" group, this group
should be a member of the local Administrators groups so you can. You could
hard code a value for strComputer and run the script remotely. This would be
more secure. If you have many workstations you could even code one script to
loop through a list of computers and change them all at once (assuming a
network).
--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--
.
- Follow-Ups:
- Re: On error resume next on encoded VB script
- From: Joe V
- Re: On error resume next on encoded VB script
- References:
- On error resume next on encoded VB script
- From: Joe V
- Re: On error resume next on encoded VB script
- From: Bob Butler
- On error resume next on encoded VB script
- Prev by Date: Re: showing code in vb2005
- Next by Date: Re: On error resume next on encoded VB script
- Previous by thread: Re: On error resume next on encoded VB script
- Next by thread: Re: On error resume next on encoded VB script
- Index(es):
Relevant Pages
|