'Powershellscript stops after disconnection to VM

I would like to run a powershell script permanently on a virtual machine. In doing so, mouse movements and keystrokes. Attached are two examples:

[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point(829 , 556)
[W.U32]::mouse_event(6,0,0,0,0);
Sleep -seconds 5
$wshellkeys.SendKeys("$global:Benutzer");

Unfortunately the code is paused when I disconnect the RemoteDesktop connection to the VM. As long as the connection is established, it runs without problems. I have also tried to convert it into an exe and vbs. Without success. Do you have any tips on how I can work around this problem? Would a ghost screen be a solution?

No, unfortunately I have no influence on the settings of the VM (Enterprise managed). And I always terminate the connection by clicking on the cross.

Many thanks for your tips



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source