将光标移到/点击文章中的句子上,可以查看译文。      显示繁体中文内容    显示简体中文内容

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

I am trying to run the a. cmd file that calls a PowerShell script from the command prompt, and i am getting the below error :

Management_Install.ps1 cannot be loaded because the execution of scripts is disabled on this system.

I have ran set-executionpolicy unrestricted and when i run get-executionpolicy from PowerShell i get unrestricted back.

//Output from Powershell

PS C:UsersAdministrator> get-executionpolicy

Unrestricted

//Output from DOS

C:ProjectsMicrosoft.Practices.ESBSourceSamplesManagement PortalInstallScr

ipts> powershell. Management_Install.ps1 1

WARNING : Running x86 PowerShell...

File C:ProjectsMicrosoft. Practices.ESBSourceSamplesManagement PortalInstallScriptsManagement_Install.ps1 cannot be loaded because the execution of scripts is disabled on this system.please see"get-help about_signing"for more details.

At line:1 char:25

  • .Management_Install.ps1 << <<1

    • CategoryInfo : NotSpecified : (:) [], PSSecurityException

    • FullyQualifiedErrorId : RuntimeException

C:ProjectsMicrosoft.Practices.ESBSourceSamplesManagement PortalInstallScripts> pause

Press any key to continue...

The system is Windows Server 2008 R2.

What am i doing wrong?

时间:

If you're using Windows 2008 R2 then there is an x64 and x86 version of PowerShell both of which have to have their execution policies set.did you set the execution policy in both hosts?

You can set the execution policy by typing this into your powershell window :

Set-ExecutionPolicy RemoteSigned

For more information see here.

I had a similar issue and noted that the default cmd on Windows 2012 was running the x64 one.

For Windows 7, Windows 8, Windows Server 2008 R2 or Windows Server 2012, run the following commands as Administrator :

x86
Open C:Windowssystem32cmd.exe
Run the commandpowershell Set-ExecutionPolicy RemoteSigned

x64
Open C:WindowsSysWOW64cmd.exe
Run the commandpowershell Set-ExecutionPolicy RemoteSigned

You should read here on the various Execution Policies are what they allow :
MSDN - WINDOWS POWERSHELL EXECUTION POLICIES

In Windows 7 :

Go to Start Menu and search for" Windows PowerShell ISE".

Right click the x86 version and choose"Run as administrator".

In the top part, paste Set-ExecutionPolicy RemoteSigned ; run the script.Choose"Yes".

Repeat these steps for the 64-bit version of Powershell ISE too (the non x86 version).

I'm just clarifying the steps that @Chad Miller hinted at.thanks Chad!

RemoteSigned : all scripts you created yourself will be run, all scripts downloaded from the internet will need to be signed by a trusted publisher.

OK, change the policy by simply typing :


Set-ExecutionPolicy RemoteSigned

If you are in an environment where you are not an administrator, you can set the Execution Policy just for you, and it will not require administrator.


Set-ExecutionPolicy -Scope"CurrentUser" -ExecutionPolicy"RemoteSigned"

or


Set-ExecutionPolicy -Scope"CurrentUser" -ExecutionPolicy"Unrestricted"

You can read all about it in the help entry.


Help Get-ExecutionPolicy -Full
Help Set-ExecutionPolicy -Full

Most of the existing answers explain the how, but very few explain the why.and before you go around executing code from strangers on the Internet, especially code that disables security measures, you should understand exactly what you're doing.so here's a little more detail on this problem.

From the TechNet about Execution Policies Page :

Windows PowerShell execution policies let you determine the conditions under which Windows PowerShell loads configuration files and runs scripts.

The benefits of which, as enumerated by PowerShell Basics - Execution Policy and Code Signing, are :

  • Control of Execution - Control the level of trust for executing scripts.
  • Command Highjack - Prevent injection of commands in my path.
  • Identity - is the script created and signed by a developer i trust and/or a signed with a certificate from a Certificate Authority i trust.
  • Integrity - Scripts cannot be modified by malware or malicious user.

To check your current execution policy, you can run Get-ExecutionPolicy.but you're probably here because you want to change it.

To do so you'll run the Set-ExecutionPolicy cmdlet.

You'll have two major decisions to make when updating the execution policy.

Execution Policy Type :

  • Restricted- no Script either local, remote or downloaded can be executed on the system.
  • AllSigned - all script that are ran require to be digitally signed.
  • RemoteSigned - all remote scripts (UNC) or downloaded need to be signed.
  • Unrestricted - no signature for any type of script is required.

Scope of new Change

  • LocalMachine- the execution policy affects all users of the computer.
  • CurrentUser - the execution policy affects only the current user.
  • Process - the execution policy affects only the current Windows PowerShell process.

for example : if you wanted to change the policy to RemoteSigned for just the CurrentUser, you'd run the following command :


Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Note : in order to change the Execution policy, you must be running PowerShell as Adminstrator.if you are in regular mode and try to change the execution policy, you'll get the following error :

Access to the registry key 'HKEY_LOCAL_MACHINESOFTWAREMicrosoftPowerShell1ShellIdsMicrosoft.PowerShell 'is denied.to change the execution policy for the default (LocalMachine) scope, start Windows PowerShell with the"Run as administrator"option.

If you want to tighten up the internal restrictions on your own scripts that have not been downloaded from the Internet (or at least don't contain the UNC metadata), you can force the policy to only run signed sripts.to sign your own scripts, you can follow the instructions on Scott Hanselman's article on Signing PowerShell Scripts.

Note : most people are likely to get this error whenever they open Powershell because the first thing PS tries to do when it launches is execute your user profile script that sets up your environment however you like it.

The file is typically located in :


%UserProfile%My DocumentsWindowsPowerShellMicrosoft.PowerShellISE_profile.ps1

You can find the exact location by running the powershell variable

 
$profile

 

If there's nothing that you care about in the profile, and don't want to fuss with your security settings, you can just delete it and powershell won't find anything that it cannot execute.

If you're here because of running it with Ruby or Chef and using `` system execution, execute as follows :


`powershell.exe -ExecutionPolicy Unrestricted -command [Environment]::GetFolderPath('mydocuments')`

That command is for getting"MyDocuments"Folder.

-ExecutionPolicy Unrestricted does the trick.

I hope it's helpful for someone else.

...