
It’s back to VBScript today. I received an email from a reader last week. He had an issue with a deployment script. We thought it would be a good idea to share the solution. For me, a chance to review some of my previous posts. Tony wrote:
I’m trying to run a simple function in my TS to put the Make and Model (from WMI) into the OEM information key of the registry, so it shows up in the System Properties screen. By itself, the script works normally:
On Error Resume Next
Dim RegShell
Dim objLocator
Dim wmi
Set RegShell = CreateObject("Wscript.Shell")
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set wmi = objLocator.ConnectServer(CompName, "root\cimv2",,,,,128)
For Each Instance in wmi.ExecQuery("Select * From Win32_ComputerSystem",,48)
Make = Instance.Manufacturer
Model = Instance.Model
Next
Model = Trim(Model)
RegShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation\Manufacturer", Make, "REG_SZ"
RegShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation\Model", Model, "REG_SZ"
' Line below used for testing only.
' MsgBox "Computer Make-Model has been written to the Windows System Properties page."
Wscript.Quit(0)
If I try to integrate this into the MDT Script Template, I constantly get an error, in the attached picture. I basically put the same code in the spot where it says ‘Insert Your Code Here!!!!’.
I’ve attached my template copy as well. I’d really like to use the script template for things like this, and I’d also like to figure out the logging aspect as well. First though, I have to figure out why it’s not running. Thanks for any help you might be able to give!
/Tony
So what went wrong? Well Tony’s created a complete (stand alone) script. That would work great in MDT but he’s wrapped it in the deployment template. He’s also created extra work for himself because using the MDT template is supposed to save you time and effort. I’ve blogged extensively on this but perhaps a recap is due.
Ok, where do I begin? Well, I’ve explained where to get and how to use the MDT scripting template here: MDT Scripting: Creating custom scripts Getting Started
After that, there’s just a few things to remember. The script makes a call to another VBScript called ZTIUtility.vbs(Located in the scripts folder of the deployment share). This acts like an engine and takes care of the heavy lifting for you. It contains a huge list of functions and declarations so you don’t have to write them yourself. So when using the MDT Template:
- Do not create objects for WMI, Windows Scripting host or to read the registry
- Do use the available functions/subroutines to:
Write a Variable,
Read a Variable,
Write to Log files,
Copy, Move or delete files,
Read or Write Registry Keys,
Delete Registry Keys,
- Do not insert a whole script on the line that says
'!!!!!!!!!!! INSERT YOUR CODE HERE !!!!!!!!!!!!
- Do Declare all variables
- Do read my posts to save yourself hours in scripting
- Do open the file ZTIUtility.vbs and browse the subroutines to learn what it’s doing.
- Do not edit the ZTIUtility.vbs –Ever!
- Do open and read the ZTIGather.xml
In the end, I was able to help Tony with these few lines:
oLogging.CreateEntry "Write Registry Keys Script", LogTypeInfo
Dim sRegKey, sMakeKey, sMakeRegValue, sModelKey, sModelRegValue
sRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation"
sMakeKey = sRegKey & "\Manufacturer"
sMakeRegValue=oUtility.regWrite(sMakeKey, oEnvironment.Item("Make"))
sModelKey = sRegKey & "\Model"
sModelRegValue=oUtility.regWrite(sModelKey, oEnvironment.Item("Model"))
The code was simply pasted where it says ‘!!!!!!!!!!! INSERT YOUR CODE HERE !!!!!!!!!!!!
If the points above dont cover everything needed to script in MDT. You can also read the references below:
You can download a copy of the template from here.
Ben Hunter – BDD 2007 – Tips and Tricks – How to write a custom script.
Xtreme Deployment.