質問
PowerShell スクリプトとして単純なバッチ ファイルを作成しましたが、実行するとエラーが発生します。
私のパスの scripts ディレクトリにあります。
Cannot be loaded because the execution of scripts is disabled on this system.
please see "get-help about-signing".
ヘルプを見ましたが、あまり役に立ちませんでした。
解決
これは PowerShell のデフォルトのセキュリティ レベルである可能性があり、(IIRC) は署名されたスクリプトのみを実行します。
これを入力してみてください:
set-executionpolicy remotesigned
これにより、ローカル (つまり、ローカル ドライブ上) の署名されていないスクリプトの実行を許可するように PowerShell に指示されます。
次に、スクリプトを再度実行してみます。
他のヒント
走らなければなりません Set-ExecutionPolicy
:
Set-ExecutionPolicy Restricted <-- Will not allow any powershell scripts to run. Only individual commands may be run.
Set-ExecutionPolicy AllSigned <-- Will allow signed powershell scripts to run.
Set-ExecutionPolicy RemoteSigned <-- Allows unsigned local script and signed remote powershell scripts to run.
Set-ExecutionPolicy Unrestricted <-- Will allow unsigned powershell scripts to run. Warns before running downloaded scripts.
Set-ExecutionPolicy Bypass <-- Nothing is blocked and there are no warnings or prompts.
使用:
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
現在のセッションで PowerShell を実行できるようにするには、常に上記のコマンドを使用します。
次のように PowerShell を呼び出すことで、このエラーを回避できました。
powershell -executionpolicy bypass -File .\MYSCRIPT.ps1
つまり、私が追加したのは、 -executionpolicy bypass
スクリプトを呼び出した方法に。
これは Windows 7 Service Pack 1 で動作しました。私は PowerShell を初めて使用するため、私が気づいていない注意点があるかもしれません。
[2017-06-26 編集] Windows 10 や Windows 2012 R2 などの他のシステムでもこのテクニックを問題なく使用し続けています。
今私が使っているのはこちらです。これにより、スクリプトをクリックして誤って実行することがなくなります。スケジューラで実行するときは、引数を 1 つ追加します。"scheduler" を選択すると、プロンプトがバイパスされます。
これにより、最後にウィンドウが一時停止され、PowerShell の出力が確認できるようになります。
if NOT "%1" == "scheduler" (
@echo looks like you started the script by clicking on it.
@echo press space to continue or control C to exit.
pause
)
C:
cd \Scripts
powershell -executionpolicy bypass -File .\rundps.ps1
set psexitcode=%errorlevel%
if NOT "%1" == "scheduler" (
@echo Powershell finished. Press space to exit.
pause
)
exit /b %psexitcode%
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
次のエラーが発生した場合でも、上記のコマンドは機能しました。
Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.
また、以下を含める必要がある場合があることも知っておいてください。 .\
スクリプト名の前に。例えば:
.\scriptname.ps1
コマンド set-executionpolicy unrestricted
作成したスクリプトはすべて、ログインしたユーザーとして実行できるようになります。ただし、実行ポリシー設定を必ず署名に戻してください。 set-executionpolicy signed
ログアウトする前にコマンドを実行します。