0
votes

The requirement is to execute a certain script on multiple workstations using a tool such as Microsoft SCCM.

This script is required to execute the EXE 'C:\ugs\nx5\UGII\env_print.exe' on every workstation. This is to be done twice using the following parameters :

  • C:\ugs\nx5\UGII\env_print.exe -m
  • C:\ugs\nx5\UGII\env_print.exe -n

The script must be designed such that the output from the above mentioned should be stored at someplace on the workstation, from where SCCM could read the values.

To achieve this requirement, I wrote the following VBscript :

--------------------------------------------------------------------------------------------

On Error Resume Next

Const HKEY_LOCAL_MACHINE = &H80000002

Dim WshShell, fso, file, objRegistry, strKeyPath, strSysDrive, outputFile, strTEMP, file2, oTxtFile, oTxtFile2
Dim ComExec, strSysRoot, strComputer, outputFile2, EXEpath, ComExec2, return, return2, text, text2, CMDPath

strComputer = "."
Set WshShell = Wscript.CreateObject("WScript.Shell")
Set fso  = CreateObject("Scripting.FileSystemObject")

Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

strSysDrive = WshShell.ExpandEnvironmentStrings("%SystemDrive%")
strSysRoot = WshShell.ExpandEnvironmentStrings("%SystemRoot%")

EXEpath = strSysDrive & "\ugs\nx5\UGII\env_print.exe"

CMDPath = strSysRoot & "\system32\cmd.exe"

'-----------------------SET TXT FILE LOCATION-----------------------

outputFile = strSysDrive & "\env_print_m.txt"

outputFile2 = strSysDrive & "\env_print_n.txt"

'-----------------------CREATE TEXT FILES-----------------------

Set oTxtFile = fso.CreateTextFile(outputFile)

Set oTxtFile2 = fso.CreateTextFile(outputFile2)

'-------COMMAND TO EXECUTE AND REDIRECT OUTPUT TO TXT FILE-------

ComExec = CMDPath & " /c " & EXEpath & " -m >> " & outputFile

ComExec2 = CMDPath & " /c " & EXEpath & " -n >> " & outputFile2

'-----------------------EXEUTE COMMANDS-----------------------

return = WshShell.Run(ComExec, 0, true)

return2 = WshShell.Run(ComExec2, 0, true)

'-----------------------READ OUTPUT FROM TXT FILES-----------------------

Set file = fso.OpenTextFile(outputFile, 1)

text = file.ReadAll

file.Close


Set file2 = fso.OpenTextFile(outputFile2, 1)

text2 = file2.ReadAll

file.Close

'-----------------------WRITE OUTPUT VALUES TO REGISTRY STRING VALUES-----------------------

strKeyPath = "SOFTWARE\env_print_Ver"

objRegistry.CreateKey HKEY_LOCAL_MACHINE, strKeyPath

WshShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\env_print_Ver\env_print_m", text, "REG_SZ"

WshShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\env_print_Ver\env_print_n", text2, "REG_SZ"

'-----------------------DELETE TXT FILES-----------------------

fso.DeleteFile outputFile

fso.DeleteFile outputFile2

--------------------------------------------------------------------------------------------

This script executes the EXE with the required parameters and stores the output to 2 different TXT files(env_print_m.txt and env_print_n.txt).

Then reads these string values from the text files and stores them as registry string values at the following locations, so that it could be read by SCCM.

  • HKEY_LOCAL_MACHINE\SOFTWARE\env_print_Ver\env_print_m
  • HKEY_LOCAL_MACHINE\SOFTWARE\env_print_Ver\env_print_n

However, when this script is executed on workstations running Windows XP, the outputs aren't redirected to the TXT files. No errors are displayed either.

I am at my wits end. Please help.

1

1 Answers

0
votes

As your first output file is not named/specified outputFile, change

Set file = fso.OpenTextFile("outputFile", 1)

to

Set file = fso.OpenTextFile(outputFile, 1)

(Same for the second one)

Trying to access a file wrongly named should abort your script with an error message. You switched of this feature by an EVIL global "On Error Resume Next". Get rid of it and see if "outputs aren't redirected' is explained by an error message.

Added wrt comment:

If something like

WshShell.Run ComExec, 0, true

'does not work' you should:

  1. call .Run as a function and check the return value
  2. echo the command (ComExec) and try to execute exactly this command from a console
  3. switch /c to /k and look
  4. sacrify a goat and think about permissions

Oh, I forgot:

  1. manually delete the output files and check if they are created but get no content - then reconder the .exe