Problem with ExecutionPolicy at Startup

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
derhoeppi
Posts: 34
Last visit: Mon Nov 28, 2022 8:48 am

Problem with ExecutionPolicy at Startup

Post by derhoeppi »

Hi,
in Powershell Studio i exported my script with the deploy function. After them i transfered the exported script to a test system where i run it. At the begin i get an error:

Code: Select all

Set-ExecutionPolicy : Windows PowerShell updated your execution policy successfully, but the setting is overridden by
a policy defined at a more specific scope.  Due to the override, your shell will retain its current effective
execution policy of Unrestricted. Type "Get-ExecutionPolicy -List" to view your execution policy settings. For more
information please see "Get-Help Set-ExecutionPolicy".
At line:1 char:46
+ ...  -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'C ...
+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (:) [Set-ExecutionPolicy], SecurityException
    + FullyQualifiedErrorId : ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
The execution policy on my test system is unrestricted. Has anyone an idea where i can configure the execution policy option for deployment in Powershell studio. Powershell Studio does not change the execution policy on the target system. It's controlled by group policies.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Problem with ExecutionPolicy at Startup

Post by jvierra »

You cannot override GP. We do not set execution policy per process. It is set globally.

Search for numerous blog articles explain what execution policy is and how it is set. The secure recommendation is "RemoteSigned" globally.
User avatar
derhoeppi
Posts: 34
Last visit: Mon Nov 28, 2022 8:48 am

Re: Problem with ExecutionPolicy at Startup

Post by derhoeppi »

I' dont want to override my own security policy. If i open the deployed script and i'm looking at line 1 char 46 - i cannot find anything to change the execution policy. At line 1 is a comment.
The default policy is "RemoteSigned" but for testing i use "Unrestricted" in an isolated environment.

I don't know where or who generate this error.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Problem with ExecutionPolicy at Startup

Post by jvierra »

You cannot override a policy set by GP. There is no way to do that.

You can run this:
powershell -ExecutionPolicy Bypass -File script.ps1
User avatar
derhoeppi
Posts: 34
Last visit: Mon Nov 28, 2022 8:48 am

Re: Problem with ExecutionPolicy at Startup

Post by derhoeppi »

Yes i know that i can change the policy to execute a script. I tested again and recognized that the problems source is not my Powershell Studio script. A little three liner shows the same error. So i have to search for this error in my policies.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Problem with ExecutionPolicy at Startup

Post by jvierra »

Changing the policy in a script is not how this works. If a script runs then the policy allows it. Any change to the policy will not affect the current script. The command is a system configuration command. When it works it is permanent.

If a policy is set for the system then the user can only set a more restrictive policy but will get an error or warning when trying to set a less restrictive policy.

"Bypass/Process" is a one time policy shot that affects a script before it is run which is why it works on the command line. It will also work for any new scripts called from the current PowerShell process. If you can run an unsigned script that is on the local file system then there is no need to "Bypass" except if you want to call a remote unsigned script from that PS session. If GP has set "RemoteSigned" then the remote script will still run because you have permission to Bypass. All admins can "Bypass".

There should never be a need to set this in a script if the systems are configured for the correct security required by the organization which would normally be "RemoteSigned" or "AllSigned".

A standard user can set Bypass with no prompt or error by using "Force"

Set-ExecutionPolicy bypass -Scope Process -Force

Check the PowerShell Team blog for more information on how policy works. Note that these policies are not hard security restrictions since they can be bypassed. They are a first defense against rogue scripts. Even a fully restricted policy still allows a text file to be loaded into memory and executed from memory.
This topic is 5 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked