VBScript to parse hostname and match it

I have a list of hostnames and keys, which are comma separated. I need a script to run that grabs the hostname (from the host itslef), and tries to match it to this list, then it echo's the key to the end of a command line I want to execute.

computer_101, asdfqeryuiuhdfanzmnvnzbznzb
computer_102, poiuaeryuiuhdfanzmnvczvasmk
etc...

The command to execute would be:
agent.exe -i asdfqeryuiuhdfanzmnvnzbznzb

That would import the key for computer_101. The script will be packaged with an installer, The installer will run the vbs at the end of it's routine, so I need the vbs to run one final command before it exits. That will automate the import of the keys for each host, so each host pick's out the right key to use from the list (inside the VBSCRIPT itself) because we are pushing out only one exe to all hosts. Let me know if that doesn't make sense, this is btw, the method we need to use (vbs).

Long story short, match host, keep list in vbs, exec final command.
-rich
LVL 38
Rich RumbleSecurity SamuraiAsked:
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.

Bill PrewCommented:
So, do you want to keep the list of computers and keys in a TXT file that the VBS reads when it runs, or do you want them hardcoded right into the VBS script, so no extra TXT file is needed?

What do you want to happen when the hostname is not found in the table?

~bp
0
Rich RumbleSecurity SamuraiAuthor Commented:
Good question, just a list in the VBS itself, I don't want an external file at this time. Also if it doesn't find "itself" in the list, just end the vbscript, I'll be able to detect host's that don't sign-in with a blank key.
-rich
0
RobSampsonCommented:
Hi rich,

I reckon this code is all you would need.  Just add more computers and keys to the dctKeys dictionary object, and it should work.

Regards,

Rob.

Set dctKeys = CreateObject("Scripting.Dictionary")
dctKeys.CompareMode = vbTextCompare
dctKeys.Add "computer_101", "asdfqeryuiuhdfanzmnvnzbznzb"
dctKeys.Add "computer_102", "poiuaeryuiuhdfanzmnvczvasmk"

strCommand = "agent.exe -i %KEY%"

Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")

strComputer = objNetwork.ComputerName
If dctKeys.Exists(strComputer) = True Then
	objShell.Run Replace(strCommand, "%KEY%", dctKeys(strComputer)), 0, True
End If

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Rich RumbleSecurity SamuraiAuthor Commented:
Thanks Rob I'll be testing over the weekend, so I'll let you know, but it seems simple enough to me, looks like just what I'll need.
-rich
0
Rich RumbleSecurity SamuraiAuthor Commented:
One last tweak, I need to have the agent to execute in the correct path, the install is 32-bit so on 64-bit machines it's in %ProgramFiles(x86)% and on 32-bit installs it's in %ProgramFiles%.
I need
%ProgFilePath%\agent\agent.exe -i ...
to execute against the proper directory, then I'm done!
-rich
0
RobSampsonCommented:
OK, how about we do a simple test for the C:\Program Files (x86) folder?

Rob.

Set dctKeys = CreateObject("Scripting.Dictionary")
dctKeys.CompareMode = vbTextCompare
dctKeys.Add "computer_101", "asdfqeryuiuhdfanzmnvnzbznzb"
dctKeys.Add "computer_102", "poiuaeryuiuhdfanzmnvczvasmk"

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists("C:\Program Files (x86)") = True Then
	strCommand = "C:\Program Files (x86)\agent\agent.exe -i %KEY%"
Else
	strCommand = "C:\Program Files\agent\agent.exe -i %KEY%"
End If

Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")

strComputer = objNetwork.ComputerName
If dctKeys.Exists(strComputer) = True Then
	objShell.Run Replace(strCommand, "%KEY%", dctKeys(strComputer)), 0, True
End If

Open in new window

0
RobSampsonCommented:
Just thinking, you may need to add double quotes
Set dctKeys = CreateObject("Scripting.Dictionary")
dctKeys.CompareMode = vbTextCompare
dctKeys.Add "computer_101", "asdfqeryuiuhdfanzmnvnzbznzb"
dctKeys.Add "computer_102", "poiuaeryuiuhdfanzmnvczvasmk"

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists("C:\Program Files (x86)") = True Then
	strCommand = """C:\Program Files (x86)\agent\agent.exe"" -i %KEY%"
Else
	strCommand = """C:\Program Files\agent\agent.exe"" -i %KEY%"
End If

Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")

strComputer = objNetwork.ComputerName
If dctKeys.Exists(strComputer) = True Then
	objShell.Run Replace(strCommand, "%KEY%", dctKeys(strComputer)), 0, True
End If

Open in new window

0
Rich RumbleSecurity SamuraiAuthor Commented:
Thanks, it seems I should of given a better example of a key, since there are all kinds of other characters that are breaking the script.
The keys contain "==" as the main culprit, and possibly other characters that might make the script react weird. (no additional quotes however)

"Pcname", "Y0NTk5N2MzN2Nm++TA2YzUwMDFh1MTAzYmI5MTUwZTQ5NTQzNjI5YmM=="
Things like that^
What can i do to make == not react in this way?
-rich
0
RobSampsonCommented:
How is it breaking the script?  Is it because when it tries to execute the command, the shell treats them as some kind of operator?

How would you go about manually running the command with those sorts of characters?  Do you enclose them in quotes?

Rob.
0
Rich RumbleSecurity SamuraiAuthor Commented:
No quotes, if I do:
dctKeys.Add "computer_101", "asdfqeryuiuhdfanzmnvnzbznzb=="
dctKeys.Add "computer_102", "poiuaeryuiuhdfanzmnvczvasmk=="

Microsoft VBScript compilation error: Expected 'Then'
Taking them away and it works.
-rich
0
RobSampsonCommented:
Hmmm, what line is the error on? I can run this and it doesn't have VBScript errors....
Set dctKeys = CreateObject("Scripting.Dictionary")
dctKeys.CompareMode = vbTextCompare
dctKeys.Add "computer_101", "asdfqeryuiuhdfanzmnvnzbznzb=="
dctKeys.Add "computer_102", "poiuaeryuiuhdfanzmnvczvasmk"

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists("C:\Program Files (x86)") = True Then
	strCommand = "C:\Program Files (x86)\agent\agent.exe -i %KEY%"
Else
	strCommand = "C:\Program Files\agent\agent.exe -i %KEY%"
End If

Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")

strComputer = objNetwork.ComputerName
If dctKeys.Exists(strComputer) = True Then
	strNewCommand = Replace(strCommand, "%KEY%", dctKeys(strComputer))
	WScript.Echo "Running " & strNewCommand
	objShell.Run strNewCommand, 0, True
End If

Open in new window

0
Rich RumbleSecurity SamuraiAuthor Commented:
That was my fault, I guess... missing a few end quotes... I have a few hundred in the list, and my eyes were bleeding from scrolling :)
Ok this last part is embarrassing, can we determine "program files" path with the variable %programfiles(x86) and %programfiles%, we had a newb ghost some machines and messed up the root drive letters, so "C:" is not the root for many hosts :( Some other way is fine it's just the way I thought would work. We have a mix of 64-bit and 32 as well, and the program installs to the x86 dir on 64-bit. Thanks for hanging in with me (again)!
-rich
0
RobSampsonCommented:
Sure, I understand.  This should do the job.

Regards,

Rob.

Set dctKeys = CreateObject("Scripting.Dictionary")
dctKeys.CompareMode = vbTextCompare
dctKeys.Add "computer_101", "asdfqeryuiuhdfanzmnvnzbznzb=="
dctKeys.Add "computer_102", "poiuaeryuiuhdfanzmnvczvasmk"

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists("C:\Program Files (x86)") = True Then
	strCommand = "%PROGRAMFILES%\agent\agent.exe -i %KEY%"
Else
	strCommand = "%PROGRAMFILES%\agent\agent.exe -i %KEY%"
End If

Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")

strProgramFiles = objShell.ExpandEnvironmentStrings("%PROGRAMFILES(x86)%")
If strProgramFiles = "%PROGRAMFILES(x86)%" Then strProgramFiles = objShell.ExpandEnvironmentStrings("%PROGRAMFILES%")

strComputer = objNetwork.ComputerName
If dctKeys.Exists(strComputer) = True Then
	strNewCommand = Replace(Replace(strCommand, "%PROGRAMFILES%", strProgramFiles), "%KEY%", dctKeys(strComputer))
	WScript.Echo "Running " & strNewCommand
	objShell.Run strNewCommand, 0, True
End If

Open in new window

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
RobSampsonCommented:
Incidentally, to avoid having to code of the all dctKeys.Add statements, I assume you have the computers in Column A and the keys in Column B in Excel?  If so, you could run this macro to generate the Add statements into Column C, and then you can copy and paste that column into your code.

Sub GenerateAddStatements()
    For intRow = 2 To Cells(65536, "A").End(xlUp).Row
        Cells(intRow, "C").Value = "dctKeys.Add """ & Cells(intRow, "A").Value & """, """ & Cells(intRow, "B").Value & """"
    Next
End Sub

Open in new window


Regards,

Rob.
0
Rich RumbleSecurity SamuraiAuthor Commented:
TY AGAIN!
0
RobSampsonCommented:
No problem Rich, thanks for the grade.
0
Rich RumbleSecurity SamuraiAuthor Commented:
ExpandEnvironmentStrings, I had no idea that was a thing, you deserve the grade, every example I found of doing %programfiles% never had that, and didn't work well for me when i tried it, yours was perfect!
-rich
0
RobSampsonCommented:
Glad to help! Also notice that I make two calls to ExpandEnvironmentStrings so that it knows whether the x86 one never resolved to anything.

Rob.
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.