08 February 2018

Set PowerShell Executionpolicy with verification One-Liner

Recently, I have been revisiting our task sequence for our base build. One of the tasks it does it to set the PowerShell executionpolicy. Yes, we do have a GPO that does this, but this being the base build that generates the golden image, GPOs are not applied during the build process.

Originally, I had the simple command line task that implemented the following command line: powershell.exe set-executionpolicy RemoteSigned. I know that should work with no problems, but I wanted to have PowerShell verify that was set. To do that, I created this one-liner that checks if the executionpolicy is set to the defined policy specified in the variable $Policy. All you need to do is change the value assigned to the $Policy variable. If it does not match that, then it sets the executionpolicy and checks again. If it is set to the defined policy, then the script returns an exit code of 0, otherwise, it returns an exit code of 1 which will fail the build.

This is the command line for implementing this:

powershell.exe -command "&{$Policy='RemoteSigned';If ((get-executionpolicy) -ne $Policy) {set-executionpolicy $Policy; If ((Get-ExecutionPolicy) -eq $Policy) {Exit 0} else {Exit 1}} else {Exit 0}}"

Here is a screenshot on how to implement the executionpolicy command line.


Reactions:

0 comments:

Post a Comment