如何避免PowerShell自动进入等待模式

时间:2018-02-16 17:45:18

标签: powershell

我有一个powershell脚本可以自动上传报告。在Internet Explorer中打开文件上载对话框后,我的代码尝试获取它的ID,然后发送密钥并发送文件地址和sendkeys输入。但不幸的是,一旦弹出文件上传对话框,我的脚本就不会继续进行并等待那里。如果我点击关闭(在文件上传对话框中),则脚本继续。有什么方法可以告诉powershell不要等待什么? 这是我的代码: -

$dummy = get-process iexplore | where {$_.mainWindowTitle -eq "ArkAngelWeb - Hitachi Systems Security Inc. - Internet Explorer"} | select -expand id
Sleep 1
$temp = $ie.Document.documentElement.getElementsByClassName("v-button-caption") | Where-Object {$_.innerHTML -eq "Select file"}
$temp.click()
Sleep 2
$swshell=New-Object -ComObject wscript.shell
$fileUploadDialog = get-process iexplore | where {$_.mainWindowTitle -eq "ArkAngelWeb - Hitachi Systems Security Inc. - Internet Explorer" -and $_.Id -ne 
$dummy.Id} | select -expand id 
$swshell.AppActivate($fileUploadDialog.Id)
$swshell.SendKeys("H");
$swshell.SendKeys("{TAB}")
$swshell.SendKeys("C:\Users\ratns1\Desktop\TEST_FILE.txt")
$swshell.SendKeys("~")

由于

1 个答案:

答案 0 :(得分:2)

这是SendKeys,时间,UI控件等问题。

您正在弹出一个模型对话框,这就是造成等待的原因。 按照设计,您的脚本将在您解除之前不会继续。这是对话框的设计。

虽然PoSH,但是你的用例是捕获22,而不是PoSH本身。

看看Wasp模块,从你的盘子上卸下所有发送密钥和窗口查找内容。

  

有时,自动化流程的唯一方法是发送   按键或鼠标点击UI元素。一个好的免费PowerShell   扩展名称为“WASP”,可在此处获取:

http://wasp.codeplex.com/ 
     

安装模块后(不要忘记取消阻止ZIP文件   在解压缩之前,通过右键单击,属性,取消阻止),WASP   module提供以下cmdlet:

Get-WindowPosition
Remove-Window
Select-ChildWindow
Select-Control
Select-Window
Send-Click
Send-Keys
Set-WindowsActive
Set-WindwowPosition
  

以下是使用Windows计算器的简单自动化示例:

Import-Module WASP 

# launch Calculator
$process = Start-Process -FilePath calc -PassThru
$id = $process.Id
Start-Sleep -Seconds 2
$window = Select-Window | Where-Object { $_.ProcessID -eq $id }

# send keys
$window | Send-Keys 123
$window | Send-Keys '{+}'
$window | Send-Keys 999
$window | Send-Keys =

# send CTRL+c
$window | Send-Keys '^c'