Ben Hunter

Architect - Microsoft Enterprise Services - Redmond

BDD 2007 - Tips and Tricks - How to write a custom script

BDD 2007 - Tips and Tricks - How to write a custom script

  • Comments 5
  • Likes

 

BDD provides a scripting framework, by leveraging this framework you can write a custom script that can utilize the functionality that BDD provides. This functionality includes a logging, accessing and changing environment variables and a number of utility functions.

Before describing how to create a script it is best to review how the scripts included with BDD are constructed.

The standard BDD script is a WSF file, this allows references to be made to functions that are contained in other scripts. BDD scripts leverage this functionality by referencing a script called “ZTIUtility.vbs”.

Understanding ZTIUtility.vbs

ZTIUtility.vbs is used to initialize the BDD environment and setup classes, these can then be used by scripts that reference it.

The script defines a number of standard objects:

  • oFSO – File System Object
  • oShell – Wscript Shell object
  • oEnv - Process Environment object
  • oNetwork - WScript Network object

Note: You do not need to declare these objects in your script.

Four classes are defined which perform a number of standard tasks:

  • Environment – This configures environment variables gather via WMI and BDD rule processing and allows you to reference these directly I from you script
  • Logging – Provides the logging functionality used by all BDD scripts, creating a single log file for each script and a consolidated log file of all scripts
  • Utility – Provides general utility functionality
  • Database – performs functions such as connecting to databases and reading information from databases (See this blog for further information)

These classes can be referenced in your scripts, here are some examples for each of the classes:

Environment class

This class is referenced in scripts through the object “oEnvironment”.

For example you change the computer name to “Blah” using the following command:

            oEnvironment.Item("ComputerName") = “Blah”

Or if you needed to determine if this is an x86 or x64 then you could query the architecture using the following command:

            oEnvironment.Item("Architecture")

Logging class

This class is referenced in scripts through the object “oLogging”.

When creating a informational log entry use the following command:

            oLogging.CreateEntry "Informational message", LogTypeInfo

When creating a error log entry use the following command:

            oLogging.CreateEntry "An error occured“,LogTypeError

Utility class

The class is referenced in scripts through the object “oUtility”.

To determine the name of the the current script, use the following command:

            oUtility.ScriptName

To find the location of a file and BDD use the following command will look in a number of locations for it.

            iRetVal = oUtility.FindFile("CustomSettings.ini", sIniFile)

Database class

The class is referenced in scripts through the object “oDatabase”. There is generally no need to use the database class directly. Database lookups can be performed using rule processing. See my rule processing blog for more information.

This is a high level view of the tasks that ZTIUtility.vbs can perform. It is worthwhile taking a good look through the script yourself as there are many hidden gems.

Now that we have reviewed the framework lets look at how to write a custom script. I have created a script template below which is based on the structure used within the standard BDD scripts. The template is simply a wrapper that references the ZTIUtility.vbs script and  launches the function "ZTIProcess". To create your own script you simply place your code within the ZTIProcess function.

Here is a quick overview of the steps required to add your script to a BDD build:

  1. Create your script based on the template
  2. Place the script in the folder \Distribution\Scripts
  3. Create an Application in the BDD Workbench that references this script
  4. Add the Application to the build task sequence

It is also important to remember the following key rules when creating your script:

  1. Always declare variables
  2. Only create objects where required - BDD creates most objects you will need
  3. Check if ZTIUtitlity provides the functionality you require before writing a function
  4. Script names should always start with a "Z" as BDD will replicate only scripts beginning with "Z" to other deployment points

Hopefully these tips will get you started, writing your own scripts that  leverage the true flexibility of BDD.

Template

Use the following template to create your own custom scripts.

<job id="Z-Sample">
<script language="VBScript" src="ZTIUtility.vbs" mce_src="ZTIUtility.vbs"/>
<script language="VBScript">

' //***************************************************************************
' // ***** Script Header *****
' //
' // Solution: Solution Accelerator for Business Desktop Deployment
' // File: Z-Sample.wsf
' //
' // Purpose: Template
' //
' // Usage: cscript Z-Sample.wsf [/debug:true]
' //
' // Customer Build Version: 1.0.0
' // Customer Script Version: 1.0.0
' // Customer History:
' //
' // ***** End Header *****
' //***************************************************************************

'//----------------------------------------------------------------------------
'//
'// Global constant and variable declarations
'//
'//----------------------------------------------------------------------------

Option Explicit

Dim iRetVal

'//----------------------------------------------------------------------------
'// End declarations
'//----------------------------------------------------------------------------

'//----------------------------------------------------------------------------
'// Main routine
'//----------------------------------------------------------------------------

On Error Resume Next
iRetVal = ZTIProcess
ProcessResults iRetVal
On Error Goto 0

'//---------------------------------------------------------------------------
'//
'// Function: ZTIProcess()
'//
'// Input: None
'//
'// Return: Success - 0
'// Failure - non-zero
'//
'// Purpose: Perform main ZTI processing
'//
'//---------------------------------------------------------------------------
Function ZTIProcess()

     iRetVal = Success

     ZTIProcess = iRetval

     '!!!!!!!!!!!   INSERT YOUR CODE HERE   !!!!!!!!!!!!

End Function

</script>
</job>

Disclaimer: The information on this site is provided "AS IS" with no warranties, confers no rights, and is not supported by the authors or Microsoft Corporation. Use of included script samples are subject to the terms specified in the Terms of Use.

Comments
  • When creating your own script to use with BDD it is a good idea to leverage the framework that BDD provides.

  • Great Stuff - Is there any way I can find all of the oEnvironment Items that are available. I am trying to find the ALLUSERSPROFILE environment variable etc.

  • Hi Josonic,

    If you have a look in the \MININT\SMSOSD\OSDLOGS folder you will find a file called variables.dat. This contains all of the environment variables and there values.

    It is in an XML format so I find it easiest to rename it variables.xml and open it with IE.

    Thanks,

    Ben