简体   繁体   English

预定的Powershell任务无法与浏览器交互

[英]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. 请注意,我在下面的“启动过程”中使用Powershell社区扩展。 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. 我有一个名为Parse的函数,该函数查询POP帐户,如果找到消息,请采取措施。 The action in question is thus ($helptext is defined and it works properly - this is being parsed correctly.) 因此,所考虑的操作是(定义了$ helptext并且它可以正常工作-正确地对此进行了解析。)

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... 我有一个Powershell脚本,可以调用Firefox并在Firefox上触发一堆SendKey(这是一个很长的故事,但是尝试使用Web调用是有问题的。)它可以很好地交互工作,但是当计划任务运行了一个调用上述脚本的Powershell脚本时Powershell脚本...

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. Firefox运行...但是它不是交互式的(因此,powershell正在运行,脚本正在启动。)换句话说,我从没有看到Firefox窗口。 I'm not sure if that's important or not, but there is always a firefox.exe in my task manager window. 我不确定这是否重要,但是在任务管理器窗口中始终存在firefox.exe。 However, my script always hangs, complaining that it can't find the firefox window. 但是,我的脚本总是挂起,抱怨找不到Firefox窗口。

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); 它通过任务管理器使用与常规交互式脚本相同的凭据(此框上只有1个用户(管理员除外)); 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 ). 我在尝试执行相同的操作时遇到了一些问题, Powershell脚本在另一个会话/上下文中运行(正如我在此处阅读的那样, 操纵桌面元素的Powershell脚本不适用于Scheduler )。

Therefore you cant use the gui. 因此,您不能使用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. 这将打开一个全屏的本地网站,并专注于默认的Web浏览器。 after this, you can use the sendkeys. 之后,您可以使用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. 附带说明一下,如果我要使用AppActivate,我将通过捕获并使用ID来确保它确实是我刚刚开始的过程。

    $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, 但是,这不能解决问题,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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