使用SendKeys运行powershell脚本并获取Access的计划任务被拒绝

时间:2017-07-06 09:26:21

标签: powershell scheduled-tasks

我有一个运行带有脚本的电源shell控制台窗口的计划任务(脚本总是从具有该计划任务的计算机运行,因此没有远程任务调用)包含:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('~');

问题是当我记录(远程或本地)它运行顺利时(电源外壳控制台打开,程序运行,按键被按下并且程序完成),但是当我注销脚本时,在power shell控制台窗口中抛出异常:

Exception calling "SendWait" with "1" argument(s): "Access is denied"
At C:\Testing\Powershell\PageFeature.ps1:4 char:2
+  [System.Windows.Forms.SendKeys]::SendWait('~');
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : Win32Exception

我将任务设置为使用我的凭据运行,即使我在注销时仍然在控制台窗口中收到消息。

这可能是根本问题?

1 个答案:

答案 0 :(得分:1)

也许只是尝试:

$obj = new-object -ComObject WScript.Shell
$obj.SendKeys("~")