none
PowerShell wrapper script for applications and exit code RRS feed

  • Question

  • I'm using PowerShell scripts for application installations etc. in MDT. My problem is that I have not figured out how to catch possible errors.

    Example code below. If something goes wrong how do I return that error code to MDT? When I run PowerShell script this way in MDT it always returns success even if something goes wrong.

    Application install command is:

    powershell.exe -ExecutionPolicy ByPass -NoProfile -Command "& .\MyPowerShellScript.ps1"

    $tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment 
    $logPath = $tsenv.Value("LogPath") 
    $logFile = "$logPath\$($myInvocation.MyCommand).log"
     
    
    Start-Transcript $logFile
    Write-Output "Logging to $logFile"
    
    $DataFolder = "C:\Deploy\Data"
    
    Copy-Item ".\Source\" -Destination $DataFolder -Recurse
    
    # Stop logging 
    Stop-Transcript


    • Edited by DamonWH Friday, June 16, 2017 5:34 PM
    Friday, June 16, 2017 5:33 PM

Answers

  • MDT *ALLREADY* has a wrapper for powershell scripts, it's been built in for quite some time.

    Within your Task Sequence, add a new step: Add --> General --> Run PowerShell Script.

    This will call ZTIPowerShell.wsf, which in turn will create a PowerShell Host to capture output from Write-host, and provide all the OSD variables in a new PowerShell provider called tsenv: which works like env:

    https://scriptimus.wordpress.com/2012/09/17/ltizti-powershell-accessing-task-sequence-variables/


    Keith Garner - Principal Consultant [owner] - http://DeploymentLive.com

    Tuesday, June 20, 2017 4:38 AM
    Moderator

All replies

  • Because I already use this - https://deploymentbunny.com/2016/07/20/osd-bios-upgrade-during-os-deployment-in-mdtconfigmgr-v3/

    To do BIOS upgrades, I just borrowed the part of the script that generates the log file for other PowerShell scripts I've made for use with MDT.

    Mikael Nystrom has a lot of MDT related PowerShell things you can find here: https://deploymentbunny.com/tag/powershell/


    If this post is helpful please vote it as Helpful or click Mark for answer.

    Monday, June 19, 2017 2:18 PM
  • MDT *ALLREADY* has a wrapper for powershell scripts, it's been built in for quite some time.

    Within your Task Sequence, add a new step: Add --> General --> Run PowerShell Script.

    This will call ZTIPowerShell.wsf, which in turn will create a PowerShell Host to capture output from Write-host, and provide all the OSD variables in a new PowerShell provider called tsenv: which works like env:

    https://scriptimus.wordpress.com/2012/09/17/ltizti-powershell-accessing-task-sequence-variables/


    Keith Garner - Principal Consultant [owner] - http://DeploymentLive.com

    Tuesday, June 20, 2017 4:38 AM
    Moderator
  • Yes, I know that but I like to add all scripts as applications in MDT. This way they are easier to manage and use imo. 
    Wednesday, June 21, 2017 7:44 AM