Powershell scripts manipulating desktop elements d

2019-08-02 15:39发布

问题:

I have 2 scripts working, when I start them manually.

Moving mouse cursor out of Screen:

[system.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point(1999,100)

If there is a chrome browser open with title HUD, put it to kiosk mode:

[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")             
[Microsoft.VisualBasic.Interaction]::AppActivate("Hud - Google Chrome")
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")     
[System.Windows.Forms.SendKeys]::SendWait("{F11}")

If I put them in a Scheduled Task on Windows 7 they don't work.

They work if I start a batch file calling the script file

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noninteractive -command "&C:\Users\Miniuser\Documents\chromeHUD.ps1"

But not if I start the batch file in a Scheduled Task.

It seems in a Scheduled Task I can't acces my UI elements.

Any ideas?

回答1:

When the script runs as a scheduled task, it runs in a different context/session and there is no GUI that it can "see", nor any way to interact with your existing session.



回答2:

As alroc answers, it does run under another context/session.

I was not able to find any workaround to get pass this... Using pure powershell... However i was able to get the job done with wscript.

in your PS script, open your chrome with this

    (new-object -com wscript.shell).run("http://localhost/",3)

It will open the default browser in full screen. i could use this with sendkeys in the task scheduler!



回答3:

To run a script as a scheduled task that must interact with the desktop You have to create a folder (or two on a 64bit-windows): (32Bit, always) C:\Windows\System32\config\systemprofile\Desktop (64Bit) C:\Windows\SysWOW64\config\systemprofile\Desktop