Udostępnij za pośrednictwem


WSF whatif parameter

Writing custom scripts for MDT and/or MDT-integrated ConfigMgr task sequences should leverage the common scripting framework in MDT (WSF script referencing ZTIUtility.vbs) to take advantage of the provided features, such as:

  • centralized logging (e.g, C:\MININT\SMSOSD\OSDLOGS)
  • shared environment variables
  • helper functions for common tasks

When using this scripting framework it can be complicated to test a script and/or entire task sequence without having it actually perform what it is intended to do.  The /debug:true command-line parameter will enable any oLogging.CreateEntry commmands of type LogTypeVerbose which can be used to provide more detailed logging output, but the script will still execute the desired commands.  PowerShell provides a standard -whatif parameter that describes what would happen if you executed the command without actually executing the command, so I developed the following add-ons for WSF scripts and task sequences to provide a similar functionality.

This is done by creation of a new command line parameter, /whatif:# .  This parameter accepts an integer value ( # ) which is used during script execution as the return code from any command that would be executed.  For example:

cscript %DeployRoot%\Scripts\Custom\INSTALL-ReportViewer.wsf /debug:true /whatif:0

In this example the INSTALL-ReportViewer.wsf script will be run with debug enabled (so all LogTypeVerbose entries will be written), whatif processing will occur, and the sample return code will be 0 (success).

The WSF script must be modified such that any line that performs any modification to the system is configured to handle the whatif parameter.  Consider the following sample lines from the original INSTALL-ReportViewer.wsf:

sFile = oUtility.ScriptDir & "\source\reportviewer.exe"
iRetVal = oShell.Run("""" & sFile & """ /q:u /c:""install /qb""", 0, True)

This needs to be modified as such to handle the new whatif parameter:

sFile = oUtility.ScriptDir & "\source\reportviewer.exe"
sCmd = """" & sFile & """ /q:u /c:""install /qb"""
If Not oEnvironment.Exists("whatif") Then 
    oLogging.CreateEntry "INSTALL ReportViewer: about to run the command: ", & sCmd, LogTypeInfo
    iRetVal = oShell.Run(sCmd,0,true)
Else
    oLogging.CreateEntry "INSTALL Report Viewer: **DEBUG** WhatIf command: " & sCmd, LogTypeVerbose
    iRetVal = oEnvironment.Item("whatif")
End If

It results in more lines of code in the script, but allows for greater functionality when running the script.

To add this functionality into a task sequence for tasks that do not execute a WSF script (e.g., Add Role or Feature), the following condition can be set to skip the task when whatif is enabled:

If none of the conditions are true
    Task sequence variable whatif exists

(This logic is for the MDT task sequence engine, the ConfigMgr task sequence engine logic may be slightly different.)

If the whatif environment variable is set before or during execution of the task sequence, then a task with the above condition will be skipped.

The whatif parameter, along with /debug:true, allows for detailed testing of the script and task sequence without "arming" either with a destructive payload.  Since no changes will be made to the system the script or task sequence can be quickly run repeatedly to test functionality during development.  The real benefit of this is gained by adding detailed LogTypeVerbose log entries to the script so that the log file can be analyzed to trace the script.