none
MDT Powershell Application

    Question

  • Hello All, I am looking for some help with MDT and Powershell files. I have wrote a couple of PowerShell scripts that I use to install applications with. I ended up creating a CMD script that take the PS file name. I did this because the CMD script changes the execution policy to unrestricted, checks 4 folder paths for the script (for redundancy), runs the PS script, and then changes the execution policy back. For example, I have an application "APP-1" and the quiet install command is \\SERVER1\MDT\Applications\RunPowerShell.cmd App-1.ps1 . When I do it like this, everything works great. I have tried numerous times to set the working directory to \\SERVER1\MDT\Applications\ and the quiet install command to RunPowerShell.cmd App-1.ps1 and it always fails. I have also tried run PS scripts that I store in the Applications folder by setting the working directory and setting my quiet install command to Powershell -file App-1.ps1 . Any suggestions on calling a powershell script from the Applications directory by setting the working directory?

    I also have 3 servers, I set up one and then replicate to the other two. When I call the script like \\SERVER1\MDT\Applications\RunPowerShell.cmd App-1.ps1 it only works when I am on SERVER1. Can I set in a variable so SERVER1 is the server name of the MDT Instance installing the applications? Can I configure it another way so MDT just knows to pull script from it's own application folder?

    Thursday, April 05, 2012 3:07 PM

Answers

  • Just an addition to Michael's post;

    You can also place your scripts inside the Deploymentshare\Scripts folder, or a newly made subdirectory. You would then not need to use any logic at all within the CMD file to determine which server it runs from, since it's actually located in all deploymentshares, as part of the scripts.

    Say you were to use DS\Scripts\Custom\RunPowershell.cmd as file location for the script, you would only need to execute %scriptroot%\custom\runpowershell.cmd App-1.ps1 from the run command line/mdt application.

    Kind regards,

    Stephan Schwarz.


    If one of these posts answered your question or issue, please click on "Mark as answer".

    My Blog | Twitter: @Schwarz_Stephan | MCTS, MCITP, MCC.
    How to configure Windows RE/OEM Recovery Partition with MDT

    • Proposed as answer by Michael Klinteberg Friday, April 06, 2012 4:19 PM
    • Marked as answer by Russel_ Friday, April 06, 2012 7:17 PM
    Friday, April 06, 2012 7:46 AM

All replies

  • Your CMD file, how does the execute powershell line look like? Is the app-1.ps1 file in the same directory as your RunPowerShell.cmd?

    Here are two simple example files that do work

    Filename RunPowerShell.cmd
    Powershell -Command Set-ExecutionPolicy Unrestricted
    Powershell.exe -file "%~dp0%1"
    Powershell -Command Set-ExecutionPolicy AllSigned

    Filename App1.ps1
    Dir

    Execute this with
    [fulle_Path_if_needed\]RunPowerShell.cmd App1.ps1

    Thursday, April 05, 2012 11:19 PM
  • Just an addition to Michael's post;

    You can also place your scripts inside the Deploymentshare\Scripts folder, or a newly made subdirectory. You would then not need to use any logic at all within the CMD file to determine which server it runs from, since it's actually located in all deploymentshares, as part of the scripts.

    Say you were to use DS\Scripts\Custom\RunPowershell.cmd as file location for the script, you would only need to execute %scriptroot%\custom\runpowershell.cmd App-1.ps1 from the run command line/mdt application.

    Kind regards,

    Stephan Schwarz.


    If one of these posts answered your question or issue, please click on "Mark as answer".

    My Blog | Twitter: @Schwarz_Stephan | MCTS, MCITP, MCC.
    How to configure Windows RE/OEM Recovery Partition with MDT

    • Proposed as answer by Michael Klinteberg Friday, April 06, 2012 4:19 PM
    • Marked as answer by Russel_ Friday, April 06, 2012 7:17 PM
    Friday, April 06, 2012 7:46 AM
  • My CMD looks like this:

    @echo off
    REM Created by Russel
    REM 19-Mar-2012
    REM Version 1.1
    REM %1 is the file name passed in when calling this batch
    Color 0A
    Title Powershell Install %1
    REM Change Powershell Execution policy so script can be run
    powershell -command "& {Set-ExecutionPolicy Unrestricted -Force}"
    REM Set possible script locations
    Set loc1=\\Image1\MDT\Applications\
    Set loc2=\\Image2\MDT\Applications\
    Set loc3=\\Image3\MDT\Applications\
    Set loc4=\\SomeServer1\Russel\PS\
    Set d=NULL
    REM Check to see where the script can be found
    IF EXIST %loc4%%1% (
    		SET d=%loc4%%1)
    IF EXIST %loc3%%1% (
    		SET d=%loc3%%1)
    IF EXIST %loc2%%1% (
    		SET d=%loc2%%1)
    IF EXIST %loc1%%1% (
    		SET d=%loc1%%1)
    REM If the script can be found then execute it, otherwise write an error file
    IF EXIST %d% (
    	powershell -file %d%
    	) ELSE (
    	Echo %1, %COMPUTERNAME%, %USERNAME%, %DATE%, %TIME%,%d% >> "%ALLUSERSPROFILE%\Desktop\Powershell_Error.txt"
    	)
    	
    REM Change Execution Policy back to restricted
    powershell -command "& {Set-ExecutionPolicy Restricted -Force}"
    This batch script works fine. I came up with this as a solution to my problem with running PowerShell scripts to install application with MDT. I needed this solution because the execution policy is set to restricted in our image. This also allows me a little redundancy when looking for the script. Thank you so much for the feedback Michael. I got a little lost in the code you put up, it looks like an infinite loop with the CMD calling the PS1 and the PS1 calling the CMD again?

    • Edited by Russel_ Friday, April 06, 2012 12:06 PM Name Change
    Friday, April 06, 2012 12:04 PM
  • Stephan, thank you, I think that is what I was looking for. I will give it a shot and let you know how it works.
    Friday, April 06, 2012 12:07 PM
  • Using the %scriptroot% variable is what I was looking for. Thank you
    Friday, April 06, 2012 7:18 PM