PowerShellをシェルとして利用するときに誰もが一度はひっかかるのがExecutionPolicyです。
今回は、Set-ExecutionPolicy RemoteSignedをしようとしたら、以下の警告が出た場合の対処です。
ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand : セキュリティ エラーです。
ExecutionPolicy の目的
敢えて長い間ふれてきませんでしたが、そもそもに触れましょう。
スクリプトは信頼できることのセーフガード、VBScriptなど過去の経験、そしてJEAに見られるような必要な権限を必要な時にという考え、あるいは「あ、間違えて実行しちゃった!」を防ぐための機能としてExecutionPolicyがあると説明されています。*1
大事なのはこの辺です。
- What you are trying to protect. In PowerShell’s case, this is almost entirely "code execution."
Sources of data, and how that data flows. In PowerShell’s case, these are scripts sent to you through email, scripts downloaded from the internet, your profile, user input, and other similar sources. From there, this data flows through many PowerShell features – the parser, cmdlet invocation, formatting and output, etc
Boundaries between untrusted data and trusted data
- PowerShell doesn’t trust scripts that you download from the internet
- PowerShell doesn’t trust a random script or executable lying in the current location of your hard drive
- PowerShell does trust user input
- PowerShell does trust the administrator of the machine
- PowerShell does trust a running script
https://blogs.msdn.microsoft.com/powershell/2008/09/30/powershells-security-guiding-principles/
https://blogs.msdn.microsoft.com/powershell/2010/02/12/building-on-powershell-execution-policies/
コンセプト自体は理解できるのですが、ちょっと心が折れそうになることが多いのではないでしょうか。
中にはデフォルトから変更しないことをすすめる記事もあります。これはこれで有益です。特にPowerShell.exe -ExeuctionPolicy RemoteSigned -Command "なにか処理やps1"は私自身よく利用しています。
他にもバイパスする手段は数多くあります。
https://blog.netspi.com/15-ways-to-bypass-the-powershell-execution-policy/
「バイパスされるようなものなんて」といいたくなる気持ちが湧いた方もいらっしゃるでしょうが、Windows Server 2012R2ではデフォルトがRemoteSignedなあたりに考えの変化も見て取れます。*2
Office365やExchange Onlineなどは、相変わらずのようですがMicrosoft社内でも見解はそれぞれなのでしょう。
https://powershell.office.com/scenarios/setting-execution-policies-on-Windows-powershell
どうするといいのか
ExecutionPolicyとは、というのは結局のところ利用シーン次第です。
グラニでは、RemoteSignedに変更しています。これは、Disposable な環境で一度きりの環境構築、以降は構成変更があれば捨てて新しく立てる。 という仕組み、コンセプトが根付いているからです。このケースにおいて、Restricted + -ExecutionPolicyなどでやる意味が乏しいです。
PowerShellスクリプト開発をされる方にとっては、当然のようにRemoteSignedにしたくなるでしょう。*3しかし、スクリプトも実行しないようなPCまで強制する必要はありません。Restrictedでいいでしょう。多くの方にとって、それぞれの利用シーンがあります。
Set-ExecutionPolicy
さて、今回の記事は、Set-ExecutionPolicyを実行すると、ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand と出た場合の対処です。
Set-ExecutionPolicy : Windows PowerShell updated your execution policy successfully, but the setting is overridden by
a policy defined at a more specific scope. Due to the override, your shell will retain its current effective
execution policy of RemoteSigned. Type "Get-ExecutionPolicy -List" to view your execution policy settings. For more
information please see "Get-Help Set-ExecutionPolicy".
At line:1 char:1
+ Set-ExecutionPolicy Unrestricted
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (:) [Set-ExecutionPolicy], SecurityException
+ FullyQualifiedErrorId : ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
Set-ExecutionPolicy : Windows PowerShell により実行ポリシーは正常に更新されましたが、設定は範囲がより明確に定義されたポ
リシーで上書きされました。この上書きにより、シェルで現在有効な実行ポリシー Restricted が保持されます。実行ポリシーの設
定を表示するには、「Get-ExecutionPolicy -List」と入力してください。詳細については、"Get-Help Set-ExecutionPolicy" を参
照してください。
発生場所 行:1 文字:1
+ Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (:) [Set-ExecutionPolicy], SecurityException
+ FullyQualifiedErrorId : ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
結論からいうと、このエラーが出る場合は設定しようとしたスコープよりも下のスコープで制限している ことが原因です。
見てみましょう。
PowerShell の実行権限スコープ
Get-ExecutionPolicy -Listをすることで一覧がみれます。MachinePolicyとUserPolicyは、GPOに関連するので触りません。

この場合、既にLocalMachineでRemoteSignedになっています。
権限スコープは、Process < CurrentUser < LocalMachineの順に広くなります。
| 権限スコープ | 範囲 | 設定箇所 |
|---|---|---|
| Process | 現在のPowerShell セッションのみ。別のPowerShell セッションには影響を与えません。 | 環境変数PSExecutionPolicyPreference |
| CurrentUser | 現在のユーザーのPowerShell実行のみ。別のユーザーには影響を与えません | レジストリHEKY_CURRENT_USER\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell |
| LocalMachine | マシン全体のスコープ。どのユーザーで実行しても、同一の権限になる。 | レジストリHKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell |
例えば、PowerShell.exe -ExecutionPolicy RemoteSignedなどは、Processスコープの変更です。

ここまで理解できれば話は簡単です。
狭いスコープから変更していく
エラーが示しているのは、変更しようとしたスコープよりも低いスコープで制限されていることを意味します。
たとえば、CurrentUserスコープがRestrictedにします。

この状態で、LocalMachineスコープをRemoteSignedにしようとするとエラーがでます。

対処は簡単です。CurrentUserを先にRemoteSignedなどにしてから、LocalMachineを変えましょう。
ProcessスコープがRestrictedになっていた場合でも、同様にProcessスコープをRemoteSignedなどにすれば回避できます。


怒られませんね。
まとめ
DisposableならExecutionPolicyも気にせず済むので、オススメです。