#author("2018-06-27T20:40:30+09:00","","") #navi(../) * PowerShellの実行ポリシーを変更せずにスクリプトを実行する方法 [#f51e4c9d] PowerShellの実行ポリシーを変更しないと、スクリプトを記述したファイルを実行してもエラーとなります。~ この記事では、実行ポリシーの変更をしなくても、一時的に実行ポリシーを変更し、PowerShellスクリプトを実行する方法を記します。~ 実行ポリシーについては、以下の記事を参考にしてください。 -[[PowerShell実行ポリシーを変更しPowerShellスクリプトを起動できるようにする>PowerShell/PowerShellスクリプトを起動できるようにする]]~ 関連記事にある、Invoke-Expressionで動かす方法もあります。 #contents #htmlinsertpcsp(win-top.html,win-sp.html) * 関連記事 [#r9d87d82] -[[PowerShell実行ポリシーを変更しPowerShellスクリプトを起動できるようにする>PowerShell/PowerShellスクリプトを起動できるようにする]] -[[PowerShellの実行ポリシーを変更せずにスクリプトを実行する方法>PowerShell/実行ポリシーを変更せずにスクリプトを実行する方法]] -[[PowerShellスクリプトが起動できない環境でスクリプトを起動する方法・Invoke-Expression>PowerShell/PowerShellスクリプトが起動できない環境でスクリプトを起動する方法]] * 実行ポリシーを設定せず、デフォルトのままでスクリプトを実行するとエラー! [#pc802112] 実行ポリシーがデフォルトのままで、PowerShellスクリプトを実行すると、下図のスクリーンショットのようにエラーとなります。 #ref(01.png) これを、エラーなしでスクリプト実行するようにするには、以下のようにします。 * -ExecutionPolicy Bypass | Remote Signed [#s22b25e2] powershellコマンドのオプション -ExecutionPolicy に Bypass を付けることによって、実行ポリシーを一時的に変え、スクリプトを実行することができます。~ (環境によっては、Remote Signedを使用します。ほとんどの場合は、Bypassでよいと思います。) - 実行するPowerShellスクリプト例~ 一行のみのスクリプトです。 #ref(hello.ps1) Write-Host "Hello world" - -ExecutionPolicy Bypass -command で実行 -- 構文 powershell -ExecutionPolicy Bypass -command スクリプト -- 実行例 powershell -ExecutionPolicy Bypass -command .\hello.ps1 #ref(02.png) #br ちなみに、コマンドプロンプトからも起動できます。~ powershell.exeを呼び出しているので当然ですよね。なので、バッチファイルやVBScriptに書いて起動する方法などがあります。 #ref(03.png) #br 以上、PowerShellの実行ポリシーを変更せず、PowerShellスクリプトを実行する方法でした。 #htmlinsertpcsp(win-btm.html,win-sp.html)