Avatar of fruitloopy
fruitloopy asked on

VBScript to generate a password based on fixed parameters

I'm sure this is a quick and easy answer:

I need some vbscript to autmatically read the PC name from the registry and generate a password based on the following parameters...

Example PC Name = "P001440089"

Password equals the last 4 digits of the PC name multiplied by 2
Any leading zeroes should be replaced with a capital X
Final password generated would equal "Pass" & the result above

Example result should be "PassX178"

At the moment I use the following to gain the PC Name:

Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
StrComputer & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName"
strValueName = "ComputerName"
objReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

Open in new window


Thanks
VB ScriptVisual Basic Classic

Avatar of undefined
Last Comment
fruitloopy

8/22/2022 - Mon
Mike McCracken

Assuming that gets the name into strValue

I don't know vbScript but you should be able to convert

strvalue = Right(strValue,4)
strValue = "Pass" & Right("XXXX" & ToText(val(strValue) * 2),4)

mlmcc
redmondb

Hi, fruitloopy.

The code below assumes that the ComputerName will be in the expected format. Do you want error-handling in case it isn't?

Option Explicit
Const HKEY_LOCAL_MACHINE = &H80000002
dim strComputer
dim strKeyPath
dim strValue
dim strValueName
dim strPassword
dim objReg

strComputer = "."
strKeyPath = "SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName"
strValueName = "ComputerName"

Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\default:StdRegProv")
objReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

strPassword = "Pass" & right("XXXX" & 2*mid(strValue,len(strValue)-3,4),4)
WScript.Echo strValue & " --> " & strPassWord

Open in new window

Regards,
Brian.
aikimark

I think it might be simpler to glom the computer name from the environment variable
dim strComputer
strComputer = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%COMPUTERNAME%")

Open in new window


Then use the code, already posted above, to derive the password from the computer name.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Steve Knight

This is what I had started to type in before going away onto something else.  Might aswell post it, similar but different...  the inputbox lets you amend the real computername for easier checking.

Steve

on error resume next
strComputerName=CreateObject("WScript.Shell").Environment("PROCESS")("COMPUTERNAME")

'Remove this next line after checking oK
strComputerName=inputbox("Please check and amend computer name to test with",,strComputerName)
if strComputerName="" then wscript.quit

strPassword = "Pass" & right("XXXX" & 2*right(strComputerName,4),4)
if strPassword="" then 
    Msgbox "Error finding password for " & strComputerName
else
    msgbox "Password is: " & strPassword,,strComputerName
End if

Open in new window

SOLUTION
RobSampson

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
fruitloopy

Thanks for all your efforts. And they all work in their own way.

Their is an additional problem which I have just discovered, ALL zeros have to be replaced with an X!

To give an example:

Computer name P001580050
Password = PassX100

Is there a way to make all zeros become an X?
RobSampson

You still have zeros is your latest example. My code produces your result.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
fruitloopy

Bloody management!

They've changed their minds (again) and now said we have to do the following:

Take the numbers after "P00", so for PC P001580050 we would use the numbers 1580050 and convert them into a HEX value.

On an excel spreadsheet this is the result:

Computer name = P001580056
Password = Pass181C12

Sorry about this!
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
redmondb

fruitloopy,

It's probably not your fault that management keep changing their minds, however you can manage your question. Rather than having a number of Experts chasing a trivial but ever-moving target, why not just pick one and go with them?

Cheers,
Brian.
ASKER
fruitloopy

Thanks all
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck