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
Avatar of Mike McCracken
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
Avatar of redmondb
redmondb
Flag of Afghanistan image

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.
Avatar of aikimark
aikimark
Flag of United States of America image

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.
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of RobSampson
RobSampson
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of fruitloopy
fruitloopy

ASKER

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?
Avatar of RobSampson
RobSampson
Flag of Australia image

You still have zeros is your latest example. My code produces your result.
SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of redmondb
redmondb
Flag of Afghanistan image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of fruitloopy
fruitloopy

ASKER

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
Avatar of RobSampson
RobSampson
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of redmondb
redmondb
Flag of Afghanistan image

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.
Avatar of fruitloopy
fruitloopy

ASKER

Thanks all
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo