4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Powershell実行ポリシーの私的ベストプラクティス

Posted at

この記事で伝えたいこと

むやみにコンピューター全体へ実行ポリシーを設定すべきではない」とうことです。
一時的な作業でps1スクリプトを実行する、定期ジョブでps1スクリプトを実行することが要件であれば、セキュリティ観点からコンピューター全体のポリシーを緩和するのではなく、実行時のみポリシーを緩和すべきです。

非推奨な使い方
Set-Executionpolicy -ExecutionPolicy Unrestricted -Force

私的ベストプラクティス

1. 一時的にスクリプトを実行する場合

以下の様にScopeを現在起動しているPowerShellのProcessに限定し、一時的なポリシー設定をすべきです。サブプロセスにも適用されるため、ps1スクリプトから呼び出すスクリプトにも適用されます。

一時的にスクリプトを実行する場合
Set-Executionpolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
Scope 説明
Process 現在のPowerShellプロセス内でのみ実行ポリシーが適用されます。
CurrentUser 現在のユーザーアカウントに対して実行ポリシーが適用されます。
LocalMachine コンピュータ全体に対して実行ポリシーが適用されます(デフォルト値)。

2. 定期ジョブでスクリプトを実行する場合

以下のpowershellコマンドにExecutionPolicyオプションを指定して、一時的にポリシーを緩和すべきです。

定期ジョブでスクリプトを実行する場合
powershell -ExecutionPolicy RemoteSigned -File "D:\script\test.ps1"
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?