Re: On error resume next on encoded VB script




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


.



Relevant Pages

  • On error resume next on encoded VB script
    ... I have a vb script that changes the local admin password of a ... My script tries to change ... since only one of the usernames will actually work. ... objUser.SetPassword strPSWD ...
    (microsoft.public.vb.general.discussion)
  • Re: Changing the local admin password base on the computers OU
    ... The intent is to put thsi script in a GPO that runs everytime the ... allowing us to cahnge local admin passwords pretty ... Your script appears to check for group membership. ... object and use the Parent method to retrieve the ADsPath of the parent ...
    (microsoft.public.scripting.vbscript)
  • Re: On error resume next on encoded VB script
    ... I have a vb script that changes the local admin password of a ... My script tries to change ... since only one of the usernames will actually work. ... objUser.SetPassword strPSWD ...
    (microsoft.public.vb.general.discussion)
  • RE: Changing local admin PW using vb logon script - can it be encrypted?
    ... > True enough, but to quote a tall, hairy dewd I've worked with ... > will only buy you 5 minutes while they search for the script ... I missed the tall hairy "dewd" reference, and I agree that security must ... local admin ought to have more protection than that. ...
    (Focus-Microsoft)
  • RE: Changing local admin PW using vb logon script - can it be encrypted?
    ... renaming the admin account is relatively weak "security by ... Subject: RE: Changing local admin PW using vb logon script - can it be ... > balance between functionality and protection". ...
    (Focus-Microsoft)