PowerShellスクリプトとして単純なバッチファイルを作成しましたが、実行時にエラーが発生します。
それは私のパスのscriptsディレクトリにあります。これは私が得るエラーです:
このシステムではスクリプトの実行が無効になっているため、ロードできません。「get-helpabout-signing」をご覧ください。
ヘルプを調べましたが、役に立たないです。
PowerShellスクリプトとして単純なバッチファイルを作成しましたが、実行時にエラーが発生します。
それは私のパスのscriptsディレクトリにあります。これは私が得るエラーです:
このシステムではスクリプトの実行が無効になっているため、ロードできません。「get-helpabout-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 で機能しました。
[2017-06-26 を編集] Windows 10 や Windows 2012 R2 を含む他のシステムでこの手法を問題なく使用し続けています。
これが私が今使っているものです。これにより、スクリプトをクリックして誤って実行することがなくなります。スケジューラで実行すると、「スケジューラ」という引数が1つ追加され、プロンプトがバイパスされます。
これにより、最後にウィンドウも一時停止されるため、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
ログアウトする前に、必ずコマンドを使用して実行ポリシー設定を署名付きに戻してください。
Windows 10 の場合: myfile.ps1 のセキュリティ プロパティの変更をクリックし、myfile.ps1 のプロパティを右クリックして [アクセスを許可] を変更します。