Run Powershell script from software package


Hello,

It might be really handy to run a Powershell script on your clients for doing a broad range of tasks.

In the past, vbscript was used to perform these tasks but considering to use powershell for the same tasks might be interesting

The big advantage of using Powershell is that it’s been built on top of .NET which means that it utilizes the base classes and is capable of interacting with some applications that cannot be manipulated using vbscript. Powershell also supports a wide range of cmdlets that facilitate tasks that might be difficult to perform when using vbscript.

Running a Powershell script using a package + program within SCCM 2007 ( or Configuration Manager 2012) can be done following the steps below:

  1. Create a package which contains the script you want to run eg. myscript.ps1
  2. Create a program for that package that sets the ExecutionPolicy of Powershell to unrestricted. This can be done by using this commandline: “powershell set-ExecutionPolicy Unrestricted -force” (without quotes)

  1. Create another program which runs the script that contains all the actions that need to be done on the client machines. In this example the command line should be: “powershell .\myscript.ps1” (again without the quotes). Be sure to add the “.\”-part because it doesn’t seem to run without that.

  1. Create a last program which sets the Executionpolicy back to restricted (or remotesigned) by using the commandline: “powershell set-ExecutionPolicy Restricted -force”  (once again without the quotes)

Now link the 3 programs together by using the “Run program first” checkbox from the program wizard in SCCM as follows:

  • Program mentioned at 4. has a run program first which points to the program mentioned at 3.
  • Program mentioned at 3. has a run program first which points to the program mentioned at 2.

As a final step create an advertisement (or deployment in ConfigMgr 2012) which uses the program mentioned at 4. and target all devices needed.

This should do the trick.

I can imagine there are other ways of achieving the same, feel free to comment with your opinion about this.

If you find this post useful, please consider buying me a virtual beer with a bitcoin donation: 3QhpQ5z5hbPXXRS8x6R5RagWVrRQ5mDEZ1

Cheers,

B

EDIT: Running a Powershell script should also be possible by using the following commandline as a program: “Powershell.exe -ExecutionPolicy Bypass -file .\myscript.ps1” (without quotes). This is a lot easier then using the 3 programs mentioned above, I’ll try and test this as soon as possible and get the result back at you guys.

EDIT2: the method of using “Powershell.exe -executionpolicy Bypass -file .\myscript.ps1” seems to work.

Advertisements

9 Responses to Run Powershell script from software package

  1. Yvonne says:

    Thank you! this really helped me out. I used the -ExecutionPolicy Bypass as well and it worked to copy a config file from a network server to workstation clients by executing the .ps1 through SCCM 2012 R2.
    Thanks for the great advice!

  2. Tony says:

    What is the “.\” actually doing in respect of the the PS file? Why do you have to have it?

  3. krumble99 says:

    I am having a big problem with this and I was wondering if you guys could help me. I can’t get it to execute the script but keep getting an object not found error. The script is there, if I right click on it really quick and tell it to execute in the powershell it works. But I can’t get it to execute. I have tried it as a software package and it works stand alone but I need it to execute from within a task sequence and as soon as I try that it fails with object not found. Part of the smsts.log file
    Set a global environment variable _SMSTSCurrentActionName=Run Command Line upgrade tivoli TSManager 8/4/2015 9:57:49 AM 2212 (0x08A4)
    Set a global environment variable _SMSTSNextInstructionPointer=2 TSManager 8/4/2015 9:57:49 AM 2212 (0x08A4)
    Set a local default variable SMSTSDisableWow64Redirection TSManager 8/4/2015 9:57:49 AM 2212 (0x08A4)
    Set a local default variable _SMSTSRunCommandLineAsUser TSManager 8/4/2015 9:57:49 AM 2212 (0x08A4)
    Set a global environment variable _SMSTSLogPath=C:\Windows\SysWOW64\CCM\Logs\SMSTSLog TSManager 8/4/2015 9:57:49 AM 2212 (0x08A4)
    Expand a string: smsswd.exe /run:BOS00315 powershell -executionpolicy bypass -fiile .\bos00315\upgrade-tivoli.ps1 TSManager 8/4/2015 9:57:49 AM 2212 (0x08A4)
    Expand a string: TSManager 8/4/2015 9:57:49 AM 2212 (0x08A4)
    Start executing the command line: smsswd.exe /run:BOS00315 powershell -executionpolicy bypass -fiile .\bos00315\upgrade-tivoli.ps1 TSManager 8/4/2015 9:57:49 AM 2212 (0x08A4)
    !——————————————————————————————–! TSManager 8/4/2015 9:57:49 AM 2212 (0x08A4)
    Expand a string: WinPEandFullOS TSManager 8/4/2015 9:57:49 AM 2212 (0x08A4)
    Executing command line: smsswd.exe /run:BOS00315 powershell -executionpolicy bypass -fiile .\bos00315\upgrade-tivoli.ps1 TSManager 8/4/2015 9:57:49 AM 2212 (0x08A4)
    =======================[ smsswd.exe ] ======================= InstallSoftware 8/4/2015 9:57:49 AM 3836 (0x0EFC)
    PackageID = ‘BOS00315’ InstallSoftware 8/4/2015 9:57:49 AM 3836 (0x0EFC)
    BaseVar = ”, ContinueOnError=” InstallSoftware 8/4/2015 9:57:49 AM 3836 (0x0EFC)
    SwdAction = ‘0001’ InstallSoftware 8/4/2015 9:57:49 AM 3836 (0x0EFC)
    No http based locations are available. SMB based locations will be attempted now. InstallSoftware 8/4/2015 9:57:50 AM 3836 (0x0EFC)
    Failed to connect to “\\pcsfilesrv01.boscov.com\SMSPKGE$\BOS00315”. Invalid username or password. InstallSoftware 8/4/2015 9:58:06 AM 3836 (0x0EFC)
    Failed to connect to the share \\pcsfilesrv01.boscov.com\SMSPKGE$\BOS00315\ with default account InstallSoftware 8/4/2015 9:58:06 AM 3836 (0x0EFC)
    Successfully connected to “\\pcsfilesrv01.boscov.com\SMSPKGE$\BOS00315” InstallSoftware 8/4/2015 9:58:06 AM 3836 (0x0EFC)
    Resolved source to ‘C:\_SMSTaskSequence\Packages\BOS00315’ InstallSoftware 8/4/2015 10:01:50 AM 3836 (0x0EFC)
    Working dir ‘C:\_SMSTaskSequence\Packages\BOS00315’ InstallSoftware 8/4/2015 10:01:50 AM 3836 (0x0EFC)
    Executing command line: Run command line InstallSoftware 8/4/2015 10:01:50 AM 3836 (0x0EFC)
    The term ‘-fiile’ is not recognized as the name of a cmdlet, function, script f InstallSoftware 8/4/2015 10:02:19 AM 3836 (0x0EFC)
    ile, or operable program. Check the spelling of the name, or if a path was incl InstallSoftware 8/4/2015 10:02:19 AM 3836 (0x0EFC)
    uded, verify that the path is correct and try again. InstallSoftware 8/4/2015 10:02:19 AM 3836 (0x0EFC)
    At line:1 char:7 InstallSoftware 8/4/2015 10:02:19 AM 3836 (0x0EFC)
    + -fiile <<<< .\bos00315\upgrade-tivoli.ps1 InstallSoftware 8/4/2015 10:02:19 AM 3836 (0x0EFC)
    + CategoryInfo : ObjectNotFound: (-fiile:String) [], CommandNotFo InstallSoftware 8/4/2015 10:02:19 AM 3836 (0x0EFC)
    undException InstallSoftware 8/4/2015 10:02:19 AM 3836 (0x0EFC)
    + FullyQualifiedErrorId : CommandNotFoundException InstallSoftware 8/4/2015 10:02:19 AM 3836 (0x0EFC)
    InstallSoftware 8/4/2015 10:02:19 AM 3836 (0x0EFC)
    Process completed with exit code 1 InstallSoftware 8/4/2015 10:02:19 AM 3836 (0x0EFC)
    Command line returned 1 InstallSoftware 8/4/2015 10:02:19 AM 3836 (0x0EFC)
    Process completed with exit code 1 TSManager 8/4/2015 10:02:20 AM 2212 (0x08A4)
    !——————————————————————————————–! TSManager 8/4/2015 10:02:20 AM 2212 (0x08A4)
    Failed to run the action: Run Command Line upgrade tivoli.
    Incorrect function. (Error: 00000001; Source: Windows) TSManager 8/4/2015 10:02:20 AM 2212 (0x08A4)

    In this version I was specifying the \packageid\script.ps1, I have also tried just -file .\script.ps1, just -file script.ps1, -file (full path)\script.ps1, -command script.ps1, -command {.\script.ps1} and always the same error objectnotfound. What fundamental concept am I missing here that I can't get something this simple to run?

  4. krumble99 says:

    Thank you do your assistance, as soon as I had posted the log to your site, which has a larger font, and looked it over I saw right away the -fiile instead of -file. Couldn’t see it on my screen but saw it there. Fixed that problem pretty easy

  5. Bert Pinoy says:

    I’m glad you could sort that one out!

  6. Juan Ballard says:

    I have also had success using:
    “powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File .\%PSScript%.ps1”

    We are big on running without interrupting the users so I added a few commands to hide any notifications. WORKS GREAT!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s