close

最近工作需求要溫習一下Angular,npm裝完後執行ng指令出現 因為這個系統上已停用指令碼執行,所以無法載入...
開始以為是版本問題降了npm版本也無效,也不是最高權限問題,查了才知道是PowerShell 執行政策(Set-ExecutionPolicy)設定問題

powershell.JPG

Set-ExecutionPolicy設定值
官方說明,破英文大概翻譯一下

AllSigned
Requires that all scripts and configuration files are signed by a trusted publisher, including scripts written on the local computer.
所有腳本和配置文件包含本機撰寫的腳本僅接受受信任的發布者簽證

Bypass
Nothing is blocked and there are no warnings or prompts.
腳本和配置文件通行無阻

Default
Sets the default execution policy. Restricted for Windows clients or RemoteSigned for Windows servers.
一般Windows預設為Restricted,Windows伺服器預設為RemoteSigned

RemoteSigned
Requires that all scripts and configuration files downloaded from the Internet are signed by a trusted publisher. The default execution policy for Windows server computers.
從網路下載的腳本和配置文件僅接受受信任的發布者簽證,本機所撰寫可執行

Restricted
Doesn't load configuration files or run scripts. The default execution policy Windows client computers.
腳本和配置文件都不能執行

Undefined
No execution policy is set for the scope. Removes an assigned execution policy from a scope that is not set by a Group Policy. If the execution policy in all scopes is Undefined, the effective execution policy is Restricted.
沒有設置policy。全部沒設置的話會套用Restricted

Unrestricted
Beginning in PowerShell 6.0, this is the default execution policy for non-Windows computers and can't be changed. Loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the internet, you're prompted for permission before it runs.
PowerShell 6.0之後非windows系統使用PowerShell都是Unrestricted且不可變更,載入所有腳本和配置文件,網路下載的使用最高權限即可

Get-ExecutionPolicy
查看一下目前是甚麼設定
Get-ExecutionPolicy.JPG

-List可以看更詳細
Get-ExecutionPolicy-list.JPG

其實第一次設置會全部都是Undefine,所以預設會是Restricted,全部不能執行

Set-ExecutionPolicy
把policy修改一下吧
Set-ExecutionPolicy.JPG

會選擇RemoteSigned是因為我只是本機要跑這些指令,如果用Bypass可能會有些安全性風險

再來執行一次Angular指令ng巴!
ng command.JPG
成功執行! 繼續來複習Angular囉

官方資料:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7.1

文章標籤
PowerShell
全站熱搜
創作者介紹
創作者 jack1213 的頭像
jack1213

尋找魔豆的傑克

jack1213 發表在 痞客邦 留言(0) 人氣()