Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell bug “execution of scripts is disabled on this system.”

Tags:

powershell

I have a power shell script that runs to stop services, 'stop / terminate process' , delete 2 files and then restart.

I can run this script perfect on my Windows 10 64 Bit Host Machine - with ZERO issues. I try to run it in my Virtual Machines and I get the error

cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170

SO just for giggles I went to see my group policies and they are not configured on either machine.

Administrative Templates > Windows Components > Windows PowerShell Not Configured.

So why the issue on the virtual machine and not in my host ?

EDIT Ran Get-ExecutionPolicy and also Get-ExecutionPolicy-List on VM Restricted

MachinePolicy       Undefined    UserPolicy       Undefined       Process       Undefined   CurrentUser       Undefined  LocalMachine       Undefined 

Ran it on my Host

        Scope ExecutionPolicy         ----- --------------- MachinePolicy       Undefined    UserPolicy       Undefined       Process       Undefined   CurrentUser       Undefined  LocalMachine    Unrestricted 

Group Policy Screen Shot

I do not know how my local machine was changed - software installation ??

like image 354
Ken Avatar asked Feb 19 '19 23:02

Ken


People also ask

How do I enable PowerShell scripts in PowerShell?

Press “Windows + I” to open settings and click on “Update & Security”. On the left sidebar, click “For developers”, then scroll down to the “PowerShell” subheading. Tick “change execution policy to allow local PowerShell scripts to run without signing.

Why it is showing running scripts is disabled on this system?

In Default windows desktops, it is Restricted, not allowing any scripts (signed or unsigned) only interactive sessions. So best is you set using RemoteSigned (Default on Windows Server) letting only signed scripts from remote and unsigned in local to run, but Unrestriced is insecure lettting all scripts to run.


2 Answers

I am going to go out on a limb here and just rehash a portion of About Execution Policies.

The default execution policy for Windows client OSes is Restricted. This means that a script will not run automatically. If your VM has a Windows client OS and you have never changed the execution policy, then your issue is expected. If the one Windows 10 machine works without issues, then someone changed the execution policy.

On the problematic VMs, you will need to determine the scope (or account) that is running your script. Then you will need to set the execution policy accordingly.

If you are testing running a script while logged into the server as yourself, then you can just open a PowerShell console and run the following:

Set-ExecutionPolicy RemoteSigned 

Then run the script in that same console.

The following command will list the execution policy for all scopes on that machine:

Get-ExecutionPolicy -List 

You should compare the command above on the working system and the non-working system. Your issue likely be the execution policy setting for the particular scope that is running the script. If you read the link in my post, it should help you determine what you need to change specifically.

The following will allow all local scripts to execute on the VM, irrespective of whether they're signed or not:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine 
like image 78
AdminOfThings Avatar answered Sep 22 '22 12:09

AdminOfThings


Open your PowerShell and enter the following command

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine 
like image 20
Saroj Bhattarai Avatar answered Sep 22 '22 12:09

Saroj Bhattarai