PowerShell:実行できない場合
2つの要因がある。
管理者権限がないために実行できない。
スクリプトの実行権限がないために実行できない。
管理者権限は PowerShell の起動時に「管理者として起動する」(Run As Administrator)を選べばよい。
以下はスクリプトの実行権限の話。
code:console
PS C:\xxxx\GitHub\powershell-playground\hello> powershell .\hello.ps1
発生場所 行:1 文字:1
+ .\hello.ps1
+ ~~~~~~~~~~~
+ CategoryInfo : セキュリティ エラー: (: ) []、PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
英語の場合
code:console
.\Setup.ps1 : File C:\Users\User\Documents\Setup.ps1 cannot be loaded because running scripts is disabled on this
system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\Setup.ps1
+ ~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
スコープごとに実行ポリシーを指定することができる。
code:console
PS C:\WINDOWS\system32> Get-ExecutionPolicy -List
Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine Undefined
PowerShell ではデフォルトはすべて Undefined
PowerShell Core では LocalMachine は RemoteSigned がデフォルトになっている。
そのプロセスでのみ実行可能にする場合
$ Set-ExecutionPolicy RemoteSigned -Scope Process -Force
参考