1
0

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.

Remote Desktopで接続した先のWindows上でPower Automateを使った場合にGUI操作が失敗する問題を解決

Last updated at Posted at 2022-08-27

長いタイトル・・・。

この記事について

Remote Desktopで接続した先のWindowsでPower AutomateでGUI操作自動化を行う場合、フローの実行中にRemote Desktopを切断するとGUI操作に失敗する。これは、リモート環境のWindowsでフローを定期実行している場合に特に問題となる。
この記事ではその問題を解決する方法を示す。

解決策

次のようなPowershellスクリプトを用意する。

discon.ps1
$session = query session | select-string "Active"
$str = $session.ToString() -replace "  *"," "
$array = $str.Split(" ")
$session_id = $array[2]

tscon $session_id /dest:console

管理者権限でこのスクリプトを実行するとリモートデスクトップから切断されるが、切断されてもPower Automateは問題なくGUI操作を行うことができる。

使いやすくする

毎回管理者権限でスクリプトを実行するのも面倒。また、ショートカットを作成して管理者権限で起動するよう設定しても、毎回UACが警告してくる。そこでボタン1つで切断できるようにする。

  • 管理者権限でdiscon.ps1を起動するバッチファイルを用意
discon_task.bat
@powershell -Command "Start-Process powershell.exe -ArgumentList "C:\Users\ryohei\tool\discon.ps1","-WindowStyle","Hidden" -Verb runas
  • タスクスケジューラにdiscon_task.batを登録
    タスク名はdisconとしておく。
    全般 > セキュリティオプション > 最上位の特権で実行するにチェック。
    操作 > プログラム/スクリプト にdiscon_task.batを指定。

  • タスクを呼び出すバッチファイルを用意

discon.bat
schtasks.exe /run /tn discon

これでUACの確認画面なしで切断できる。discon.batのショートカットを作成し、好きなアイコンを設定してタスクバーにピン留めしておくとよい。

参考文献

AutoMate : やってはいけないこと
Disconnecting from Remote Desktop while executing automated tests
管理者権限ダイアログを出さずにpowershellスクリプトを管理者権限で実行

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?