Quantcast
Channel: OS|DC
Viewing all articles
Browse latest Browse all 64

MDT 2013: Create a simulation environment for your Powershell Scripts

$
0
0

roadside

Ever heard that MDT support Vbscript and Powershell ? Nice one you might think, then you try developing your own script with all those fantastic buildin function… Long story short: after scratching your head during nights and days, you realize that it’s a bit harder than expected.

One of the most difficult part when building MDT scripts, is to be able to catch the state of the MDT properties to use them in your own scripts.

While a solution exist for Vbscript (more on that later) there is actually no equivalent for Powershell. But, as you might have guessed,  until the next few lines.

 

What is a powershell simulation environment?

During a deployment MDT expose two new PSDrive filled with lists of all available MDT properties. MDT also offer a powershell module to access the drives and to modify the properties. Unfortunately the drives are only fulfilled during a deployment.  After some research, Andrew Barnes, thanks to him, was able to give a workaround to the community. But while his solution worked nicely, it was not the most “comfortable” compared to what already exist on the vbscript front.

Vbscripts in MDT have the same weakness when it comes to MDT properties, but to overcome the issue Michael Niehaus introduced the “simulation environment” which basically is an excerpt from MDT scripts that are in charge of computing and exposing the properties with the big advantage of being able to run locally! No more need to launch MDT nor a task sequence, no more need of wds, vm, infra… got it?!!… Other advantage: you can now comfortably develop you own MDT script with your preferred IDE an access MDT properties like if you were in the middle of a deployment.

The aim of this new solution is to offer the same ease of use under powershell!

 

How to install

(Disclaimer: the next four step are a total rip off from technet who were originally written by Yohan ! so please, don’t sue me for that)

Create a folder named C:\MDT.

From the Scripts folder of your deployment share, copy the following files to C:\MDT:

  • ZTIDataAccess.vbs
  • ZTIGather.wsf
  • ZTIGather.xml
  • ZTIUtility.vbs

From the Control folder of your deployment share, copy the CustomSettings.ini file to C:\MDT.

In the C:\MDT folder, create a subfolder named X64.

From the Tools\X64 folder of your deployment share, copy the Microsoft.BDD.Utility.dll file to C:\MDT\X64.

From the Tools\X64 folder of your deployment share, copy the following files to C:\MDT:

  • 00000409 folder
  • CcmCore.dll
  • CcmUtilLib.dll
  • Smsboot.exe
  • SmsCore.dll
  • TsCore.dll
  • TSEnv.exe
  • TsManager.exe
  • TsmBootstrap.exe
  • TsMessaging.dll
  • TsProgressUI.exe
  • TsResNlc.dll
  • xprslib.dll

From the Tools\Modules folder of your deployment share, copy the following subfolders to C:\Windows\System32\WindowsPowerShell\v1.0\Modules

  • Microsoft.BDD.TaskSequenceModule
  • ZTIUtility

In the C:\MDT folder, create a file named GatherPS.cmd and fill it with the following instruction:

cls
set CuDIR=%CD%
if exist C:\MININT rd C:\MININT /s /q
cscript ZTIGather.wsf
Xcopy C:\MININT\SMSOSD\OSDLOGS\VARIABLES.DAT %CuDIR% /Y
TsmBootstrap.exe /env:SAStart

In the C:\MDT folder, create a file named TS.XML and fill it with the following lines:

<?xml version="1.0"?>
<sequence version="3.00" name="Custom Task Sequence" description="Sample Custom Task Sequence">
  <step type="BDD_InstallApplication" name="Launching PS with MDT Env." description="" disable="false" continueOnError="false" runIn="WinPEandFullOS" successCodeList="0 3010">
    <defaultVarList>
      <variable name="ApplicationGUID" property="ApplicationGUID"></variable>
      <variable name="ApplicationSuccessCodes" property="ApplicationSuccessCodes">0 3010</variable>
    </defaultVarList>
    <action>"C:\Windows\System32\WindowsPowerShell\v1.0\Modules\Microsoft.BDD.TaskSequenceModule\Microsoft.BDD.TaskSequencePSHost40.exe""%CuDIR%\NewPSConsole.ps1""C:\MININT\SMSOSD\OSDLOGS"</action>
  </step>
</sequence>

In C:\Users\<User>\Documents\WindowsPowerShell, edit the files

  • Microsoft.PowerShell_profile.ps1
  • Microsoft.PowerShellISE_profile.ps1

and add to them the command line Import-Module ZTIutility

Finally, in the C:\MDT folder, create a file named NewPSConsole.ps1 and fill it with this command:

start-process powershell.exe -Wait

Your C:\MDT folder should now looks like this :

2014-07-29_10h57_00

 

How to use

Now, let the fun begin, once you’ve opened an elevated command prompt, launch GatherPS.cmd. A few second later, an MDT window will pop up followed by a slightly different Powershell Console that have those new capability:

2014-07-29_12h50_52

As you can see the MDT module is loaded automatically, but that’s not all, TSEnv and TSEnvList the two new MDT Drives are also present.

If we try to enumerate one of those with Get-Childitem:

2014-07-29_12h58_45

BOOM!!! Properties are available.

 

What’s next?

You are now able to work under the same condition as those which apply during a deployment …except for one point: the DeployRoot propertie doesn’t exist. You’ll have to use hardcoded path during your script development.

Apart from that, the world is your!! And if you are not very fond of the powershell console for writing code, why not using Powershell ISE or PowerGUI instead?

Yes, that’s also possible, edit NewConsole.ps1 and replace its content by one of the following lines:

For Powershell ISE:

start-process "C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe"–Wait

For PowerGUI:

start-process "C:\Program Files (x86)\PowerGUI\ScriptEditor.exe"–Wait  

2014-07-29_14h10_56

Hope this will encourage every one to go powershell with MDT.

I really want to thanks Andrew Barnes and Floriant Valente for their very inspiring blog post. Both works lead me to this new solution.


Viewing all articles
Browse latest Browse all 64

Trending Articles