Jump to content


FazzaGBR

How Do You Run A Powershell Script In The Task Sequence

Recommended Posts

I have a powershell script that removes the default Windows 8.1 Apps that I wish to run as part of the task sequence on my SCCM 2012 R2 server.

 

Within the Task Sequence editor, when I click on Add > General > Run PowerShell Script I was expecting to just be able to enter the powershell script I wanted to run, instead it's asking me for a Package and the Script name.

 

So I'm guessing from this I need to create a Package first but I've not been able to find any examples on what the Package settings should be - has anyone successfully run a PS Script within the task sequence in SCCM 2012 R2, if so can you tell me what settings you put in the Package please?

Thanks.

Share this post


Link to post
Share on other sites

simply create a package with no program, the package should contain your powershell script, distribute the package to your distribution points.

 

once done, you can create a Run PowerShell Script step in your task sequence, like so

 

the Package should refer to the package you created above

the script name is the actual powershell script, eg

 

path\to\myscript.ps1

 

and the parameters are whatever parameters the powershell script expects to be provided such as variables

 

%somevariable%

 

the Powershell execution policy should be set to Bypass (unless you've signed the script yourself)

 

below is a sample powershell script being run in a task sequence

 

image_thumb_790B67B9.png

  • Like 3

Share this post


Link to post
Share on other sites

Has anyone been able to use the 'Run Command Line' task sequence task and while specifying "RUN AS" credentials and get the script to run?

 

I have been trying to get PowerShell scripts to run from the 'Run Command Line' task while specifying a "RUN AS" account for a while now with no success. Here is the syntax I use in the task sequence step: Powershell.exe -Executionpolicy Bypass -File .\filename.ps1

 

I have tried with twice now with do different scripts that work with the 'Run PowerShell Script' task, but when I use the 'Run Command Line' task the first line always throws an error.

 

For example this line: $tsenv=New-Object -COMObject Microsoft.SMS.TSEnvironment throws this error: New-Object : The term 'New-Object' is not recognized as the name of a cmdlet, function

, script file, or operable InstallSoftware.

I tried another script using this method and the first line calls Get-Content which returns the error message for the cmdlet.

Are the know issues with running Powershell scripts using the 'Run Command Line' task and specifying a "RUN AS" account? The reason I am trying to do this in this way because I need to specify credentials, the 'Run PowerShell Script' task only runs as LocalSystem which does not have enough rights do all stuff in the script. Would anyone know what is going on here or what is causing this behavior? The script code works, it has been tested under the 'Run Powershell script' task. The script only fails using the'Run Power Script' task due to permissions. I would appreciate any help, I just cant wrap my mind around what is happening here.

 

-Tony

post-24181-0-66004700-1434594465_thumb.jpg

post-24181-0-96393500-1434595024_thumb.jpg

Share this post


Link to post
Share on other sites

Reading through the link you provided, On a 64 bit computer, I have to force PowerShell to launch as 64 bit bet to use Microsoft.SMS.TSEnvironment COM Object? To do that I have to launch PowerShell as follows: "%windir%\sysnative\windowspowershell\v1.0\powershell.exe -NoProfile -File MyFile.ps1? Does that sound right? Do you have experience personally in that space?

Share this post


Link to post
Share on other sites

Hi Anyweb,

I tried using your steps, but i can't distribute the empty package to the DP, as it is greyed out, presumably because it is an empty package.

Is there a way around this?

I only want to run two lines of powershell, is the package method the best way and how can i make it work?

Task sequence fails with 0x80070002 error, because it can't find the package.

Share this post


Link to post
Share on other sites

take a look at this post, it's exactly what you want to do, run a PowerShell script...

 

https://www.windows-noob.com/forums/topic/14170-how-can-i-display-custom-messages-in-a-task-sequence-using-powershell-in-system-center-configuration-manager-current-branch/

 

follow the steps exactly until you have it working, once you have it working, adapt it to run YOUR PowerShell script.

 

cheers

niall

Share this post


Link to post
Share on other sites

On ‎7‎/‎30‎/‎2014 at 12:36 PM, anyweb said:

simply create a package with no program, the package should contain your powershell script, distribute the package to your distribution points.

 

once done, you can create a Run PowerShell Script step in your task sequence, like so

 

the Package should refer to the package you created above

the script name is the actual powershell script, eg

 

path\to\myscript.ps1

 

and the parameters are whatever parameters the powershell script expects to be provided such as variables

 

%somevariable%

 

the Powershell execution policy should be set to Bypass (unless you've signed the script yourself)

 

below is a sample powershell script being run in a task sequence

 

image_thumb_790B67B9.png

if i have created package with standard program & provide general details in program then ... will it be worked or failed? 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.