[powershell] 使用 Windows PowerShell ISE

  • 355
  • 0
  • 2022-10-04

在使用 Windows PowerShell ISE 遇到以下錯誤,簡單來說是因為權限的關係。
預設一般使用者無法執行 powershell script。

在使用 Windows PowerShell ISE 遇到以下錯誤,簡單來說是因為權限的關係。
預設一般使用者無法執行 powershell script。

```
因為這個系統上已停用指令碼執行,所以無法載入 C:\x\00StartDevPCVM.ps1 檔案。如需詳細資訊,請參閱 about_Execution_Policies,網址為 https:/go.microsoft.com/fwlink/?LinkID=135170。
   + CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecordException
   + FullyQualifiedErrorId : UnauthorizedAccess
```

兩個解決方法,
(1)用 administrator 身份開啟 PowerShell 或 PowerShell ISE 來執行
(2)設定執行權限

設定執行權限,也是會遇到權限問題,所以也有兩個方法,
(1)用 administrator 身份開啟 PowerShell 或 PowerShell ISE 來執行「設定執行權限」指令
(2)只設定 CurrentUser 的執行權限

如果是使用方法(1)的話,以下指令可用。

`Set-ExecutionPolicy RemoteSigned`


如果是在使用者的 powershell 執行上面指令會出現


```
PS C:\x> Set-ExecutionPolicy RemoteSigned
Set-ExecutionPolicy : 拒絕存取登錄機碼 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell'。 若要變更預設 (Loca
lMachine) 領域的執行原則,請使用 [以系統管理員身分執行] 選項啟動 Windows PowerShell。若要變更目前使用者的執行原則,請執行 "Set-ExecutionPolicy -Scope CurrentUser"。
位於 線路:1 字元:1
+ Set-ExecutionPolicy RemoteSigned
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   + CategoryInfo          : PermissionDenied: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
   + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
```

那就使用以下指令

`Set-ExecutionPolicy -S CurrentUser RemoteSigned`


## 參考指令
Set-ExecutionPolicy
Get-ExecutionPolicy

Get-ExecutionPolicy -List


* https://docs.microsoft.com/zh-tw/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.2

 

 

 

分享