Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting a Unique Local Admin Password via VBA

Status
Not open for further replies.

damienenglish

Technical User
Mar 21, 2012
27
GB
Hi All

I have been asked to edit our existing VBS which is used to rename all local 'Admin' accounts and passwords on our PC's and Laptops. This is the script we currently have:

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

strComputer = "."
Set objUser = GetObject("WinNT://" & strComputer & "/ITAdmin,user")


objUser.SetPassword "password"
objUser.SetInfo

I am happy to carry on using this script but I want to add a little more security to the password. What I want, is for the script to pickup the 'PC Name' and add that in front of the password.

For Example: The PC is called Host1, therefore the password would become 'Host1password'. Also, I would like to make the 'PC Name' part of the password a mixture of upper and lower-case.

Is this at all possible? Any suggestions/comments are much appreciated.

Thanks in advance for your help.
 
>What I want, is for the script to pickup the 'PC Name'

Your script is already doing that:

strComputer = WshNetwork.ComputerName

Trouble is that you then immediately throw this away:

strComputer = "."

>Is this at all possible?

Sure. Something like the following would meet the requirements you have given:

Code:
[blue]Set WshNetwork = CreateObject("WScript.Network")
strComputer = WshNetwork.ComputerName
MsgBox BlatIt(strComputer) & "password"

Function BlatIt(strtext)
    ' generate a seed based on input text and use to initialise random number generator
    For lp = 1 To Len(strtext)
        total = total + Asc(LCase(Mid(strtext, lp, 1)))
    Next
    Randomize total
    
    For lp = 1 To Len(strtext)
        Select Case Int(Rnd(1) * 2) 
            Case 0
                replacement = UCase(Mid(strtext, lp, 1))
            Case 1
                replacement = LCase(Mid(strtext, lp, 1))
        End Select        
        BlatIt = BlatIt & replacement ' build output string
    Next
End Function[/blue]
 
Hi StrongM!!

Thanks so much for your response - I will give it a go.

Regards
 
I have one question!!

With the selection of upper and lower case characters, will this randomly select the character type, or is this putting it in a particular order?

For example: I want the password to be generated as - HoSt1password - The selection of upper and lowercase characters is to apply to the computer part of the password only. Also, I want it to start with Uppercase, then lowercase, then upper case, then lowercase, etc etc.

Does this make sense?

Example: A computer is called COMPUTERMAIN - The Password should then be generated as: CoMpUtErMaInpassword

Regards
 
>will this randomly select the character type

Yes. See below.

>is this putting it in a particular order?

Well, yes, sort of. Again see below.

>apply to the computer part of the password only

Yes. If you'd tried it, you'd have seen this.

>Does this make sense?

Sure.

You were not specific about any particular order in your original post, so my suggestion mixes upper and lower case in an order that is effectively specified/controlled by the computername itself (and will always be in that order for a given computername). If you want a fixed, independant order, then the following simplification to the BlatIt function tion should do the trick:

Code:
[blue]Function BlatIt(strtext)
    For lp = 1 To Len(strtext)
        Select Case lp Mod 2
            Case 1
                replacement = UCase(Mid(strtext, lp, 1))
            Case 0
                replacement = LCase(Mid(strtext, lp, 1))
        End Select
        BlatIt = BlatIt & replacement ' build output string
    Next
End Function[/blue]

 
Hi StrongM

Unfortunately that doesn't work.

The message box is bringing up the correct password layout but its not actually changing the password for the local user account called 'ITAdmin'.

This is what I have got:

Set WshNetwork = CreateObject("WScript.Network")
strComputer = WshNetwork.ComputerName
Set objUser = GetObject("WinNT://" & strComputer & "/ITAdmin,user")
MsgBox BlatIt(strComputer) & "password"

Function BlatIt(strtext)
For lp = 1 To Len(strtext)
Select Case lp Mod 2
Case 1
replacement = UCase(Mid(strtext, lp, 1))
Case 0
replacement = LCase(Mid(strtext, lp, 1))
End Select
BlatIt = BlatIt & replacement ' build output string
Next
End Function
 
My code doesn't do the whole job for you, you know. It was an example of how to ensure you got the computername and how to apply upper and lowercase to it and tack on the password. It didn't then actually modify the password, since you already appeared to know how to do that bit.

I beginning to be a bit concerned that you are messing about with (important) passwords using code you may not understand, having simply glued bits and pieces together from various sources.

However, given that caveat, you'd simply need to change

[tt]MsgBox BlatIt(strComputer) & "password"[/tt]

in your most recently posted code to

[tt]objUser.SetPassword BlatIt(strComputer) & "password"
objUser.SetInfo[/tt]


 
Hi StongM

Many Thanks - that now works.

Sorry, I should have said in the beginning that I am a novice on VBS and still learning the commands.

Many Thanks for all your help.
 
>Many Thanks - that now works

:)

>why is your variable named 1p?

Ah. It isn't 1p, it is lp (shorthand for loop). Goes way, way back to my computer science days at university.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top