简体   繁体   中英

Scheduled Powershell task can't interact with browser

Revised description:

using the suggestion below, I went with the most basic code I could create. Note that I'm using the Powershell Community Extensions with the "Start-Process" below. I don't know if that impacts things or not.

I have the below function

function Populate-Notepad
{
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
Start-Process Notepad 
[Microsoft.VisualBasic.Interaction]::AppActivate("Untitled")
[System.Windows.Forms.SendKeys]::SendWait("blahblahblah")
return
}

Works fine interactively.

I have a function called Parse, which queries a POP account, and if a message is found - take an action. The action in question is thus ($helptext is defined and it works properly - this is being parsed correctly.)

Switch ($cmd)
{
"HELP"     {$text = $helptext}
"NOTE"     {populate-notepad}
default    {$msg = $msg + "Invalid command: $command `n"}
}

This does not fire notepad. If I watch task manager, it never appears to populate. My guess is that if this can be solved - so can the below problem.


I have a powershell script that invokes Firefox and fires a bunch of SendKeys at Firefox (it's a long story, but trying to use web invokation was problematic.) It works fine interactively, but when a scheduled task runs a powershell script that calls the above powershell script...

Firefox runs...but it's not interactive (so powershell is running and the script is firing.) In other words, I never see a Firefox window. I'm not sure if that's important or not, but there is always a firefox.exe in my task manager window. However, my script always hangs, complaining that it can't find the firefox window.

Like I said, this works interactively; it's using the same credentials via task manager as the regular interactive script does (there's only 1 user other than admin on this box); it's not hidden, and I've tried it as "run with highest privileges" to no avail.

Any thoughts?

I had a few problems trying to do the very same, The powershell script is running in another session/context (as i read here Powershell scripts manipulating desktop elements don't work with Scheduler ).

Therefore you cant use the gui.

My solution was to use this

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

This opens a local website, full screen and focused in the default web browser. after this, you can use the sendkeys.

As a side-note, if i was to use the AppActivate,i would start securing that it is really the process i just started, by capturing and using the ID instead.

    $iex = Start-Process 'C:\Program Files (x86)\Internet Explorer\iexplore.exe' -passthru 'http://localhost/'
    start-sleep 3
    [Microsoft.VisualBasic.Interaction]::AppActivate($iex.Id)

This will however not solve the problem,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM