PowerShell 默認不允許執行*.ps1腳本文件。運行ps1文件會得到下面的錯誤:
File C:\Temp\Test.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:19
+ c:\Temp\Test.ps1 <<<<
可以通過Get-ExecutionPolicy,來取得當前策略。
用Set-ExecutionPolicy設置當前策略。
下面的命令可以解決上面的錯誤
PS C:\Windows\system32> Set-ExecutionPolicy RemoteSigned <按回車>
Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution
policy?
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):<按Y>
在PowerShell中的執行
Policy的有效參數:
-- Restricted: 不載入任何配置文件,不運行任何腳本。 "Restricted" 是默認的。
-- AllSigned: 只有被Trusted publisher簽名的腳本或者配置文件才能使用,包括你自己再本地寫的腳本
-- RemoteSigned: 對於從Internet上下載的腳本或者配置文件,只有被Trusted publisher簽名的才能使用。
-- Unrestricted: 可以載入所有配置文件,可以運行所有腳本文件. 如果你運行一個從internet下載並且沒有簽名的腳本,在運行之前,你會被提示需要一定的權限。
-- Bypass: 所有東西都可以使用,並且沒有提示和警告.
-- Undefined: 刪除當前scope被賦予的Execution Policy. 但是Group Policy scope的Execution Policy不會被刪除.