UiPathでPowerShellを呼出しの時に実行ポリシーを一時的に変更する方法が分からない

現在「Powershellを呼出し」でPowershellスクリプトを呼び出そうとすると

「PowerShell を呼び出し: File Test.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at about Execution Policies - PowerShell | Microsoft Learn. 」というエラーが出ます。
(Test.ps1の部分はPowershellのパス・ファイル名にあたります。)

おそらく PowerShellでこのシステムではスクリプトの実行が無効になっているため、ファイル hoge.ps1 を読み込むことができません。となったときの対応方法 #Windows10 - Qiita で書かれているような、実行ポリシーの変更をしないといけないのだろうな、とは思うのですが、常時実行可能にはしておきたくありません。上記Qiitaの中にあるように「 その時のプロセスだけ有効にして終わったら元に戻るようにスコープをProcess で設定」としたいと思っています。

今、
①実行ポリシーについて何も指定しない:
→冒頭のエラーが返却
②Powershellスクリプトの冒頭に「Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process」を記載:
【Powershell】キー入力の監視プログラムの作り方【キーロガー】 - Cyber Security Lab の中にスクリプト内に記載があるのか?というものを見かけたので。
→冒頭のエラーが返却

という状況です。一時的な実行ポリシー変更かつ「Powershellを呼出し」アクテビティを使うことはできるでしょうか?できる場合どのようにすればよいでしょうか?

本件、解決に近づいたような遠のいたような。。です。
1)Forceで強制する。2) コマンド間を「&」でつなぐ。 ということをしてみると、実行ポリシーを変更したうえで、動きそうな状態にはなりました。

“Set-ExecutionPolicy -Force -ExecutionPolicy RemoteSigned -Scope Process &” & Test.ps1 & " " & 【何か引数】

ところが、以下のエラーが出ました。

System.Management.Automation.CmdletInvocationException: The pwsh executable cannot be found at “C:\Users\local_admin.nuget\packages\system.management.automation\7.1.0\runtimes\win\lib\net5.0\pwsh.exe”.
Note that ‘Start-Job’ is not supported by design in scenarios where PowerShell is being hosted in other applications. Instead, usage of the ‘ThreadJob’ module is recommended in such scenarios. —> System.Management.Automation.PSNotSupportedException: The pwsh executable cannot be found at “C:\Users\local_admin.nuget\packages\system.management.automation\7.1.0\runtimes\win\lib\net5.0\pwsh.exe”.
Note that ‘Start-Job’ is not supported by design in scenarios where PowerShell is being hosted in other applications. Instead, usage of the ‘ThreadJob’ module is recommended in such scenarios. at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)

そして、他の 書き込みなどをみるに、PowershellスクリプトをUiPathで使うことは問題が残っているようです。

本来は以下の質問を参考に、VBScriptをPowershellスクリプトに変えようと思ったのですが。。。やめておきます。

こんにちは

直接の回答ではありませんが...
特段の戻り値が必要でなければ、一連のコマンドをバッチ化してStartProcessアクティビティあるいはProcessクラス等を用いて実行したほうが良いかもしれません。
(InvokePowershellアクティビティは少々使いにくいので...)

有難うございます。「バッチ化」とは、「~.bat」ファイルにまとめる、という意味でしょうか?

はい、複数行必要なら.batにして、一つのコマンドで済むならpowershell.exeを呼び出せばよいかと思います。

1 Like