剛開始使用Powershell,導入管理模塊或者其他操作的時候會出現因爲在此係統中禁止執行腳本的報錯,報錯內容如下:
Windows PowerShell
版權所有 (C) 2009 Microsoft Corporation。保留所有權利。
PS C:\Windows\system32> D:\Workshop\Powershell\Sprinkler\src\Scripts\SPRINKLE.ps1
無法加載文件 D:\Workshop\Powershell\Sprinkler\src\Scripts\SPRINKLE.ps1,因爲在此係統中禁止執行腳本。有關詳細信息,請參
閱 "get-help about_signing"。
所在位置 行:1 字符: 58
+ D:\Workshop\Powershell\Sprinkler\src\Scripts\SPRINKLE.ps1 <<<<
+ CategoryInfo : NotSpecified: (:) [], PSSecurityException
+ FullyQualifiedErrorId : RuntimeException
PS C:\Windows\system32> get-ExecutionPolicy
Restricted
果然,是被限制。還是在"get-help about_signing"裏面可以查到設置命令"set-executionpolicy"
再在幫助中找到"set-executionpolicy"的說明。。似乎是比較建議設置爲,至少是被標記爲可信任的
-- RemoteSigned: Requires that all scripts and configuration files downloaded from the Internet be signed by a trusted publisher.
在PowerShell執行以下:
PS C:\Windows\system32> set-ExecutionPolicy RemoteSigned
執行策略更改
執行策略可以防止您執行不信任的腳本。更改執行策略可能會使您面臨 about_Execution_Policies
幫助主題中所述的安全風險。是否要更改執行策略?
[Y] 是(Y) [N] 否(N) [S] 掛起(S) [?] 幫助 (默認值爲“Y”): y
PS C:\Windows\system32>