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
LVL 2
fruitloopyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mlmccCommented:
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
0
redmondbCommented:
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.
0
aikimarkCommented:
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.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Steve KnightIT ConsultancyCommented:
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

0
RobSampsonCommented:
Hi, here's my take on the solution.

Regards,

Rob,

Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
strNumber = Right("XXXX" & CInt(Right(strComputer, 4)) * 2, 4)
strPassword = "Pass" & strNumber
WScript.Echo strPassword

Open in new window

0
fruitloopyAuthor Commented:
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?
0
RobSampsonCommented:
You still have zeros is your latest example. My code produces your result.
0
Steve KnightIT ConsultancyCommented:
Likewise.  Do you mean above should then be PassX1XX

In which case can use replace easily enough:


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" & replace(right("XXXX" & 2*right(strComputerName,4),4),"0","X")
if strPassword="" then 
    Msgbox "Error finding password for " & strComputerName
else
    msgbox "Password is: " & strPassword,,strComputerName
End if

Open in new window

0
redmondbCommented:
fruitloopy.

All X's...
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" & replace(right(2*mid(strValue,len(strValue)-3,4),4),"0","X")
WScript.Echo strValue & " --> " & strPassword

Open in new window

Regards,
Brian.
0
fruitloopyAuthor Commented:
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!
0
RobSampsonCommented:
If I use this code on your example:
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
strNumber = Mid(strComputer, 4)
strPassword = "Pass" & Hex(strNumber)
WScript.Echo strPassword

Open in new window


I get Pass181C18

Rob.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
redmondbCommented:
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.
0
fruitloopyAuthor Commented:
Thanks all
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.