我正在创建一个gui,并且想要一个弹出窗口让您知道它很忙,但是在完成特定任务后将其关闭。我唯一能找到的是以下内容...
$popup = New-Object -ComObject wscript.shell
$popup.popup("Running Script, Please Wait....",0,"Running...",0x1)
但是问题是,这正在等待响应,然后它将运行脚本。我不是要有人给我写脚本,而是要一些有关在何处找到此信息的准则。
我需要Powershell来弹出一个窗口,然后在运行脚本时将其保留,然后在脚本运行完毕后将其关闭。 最好只是拥有另一个Windows窗体,该窗体运行带有标签的脚本吗?对于一个简单的任务来说,这似乎是过多的工作。但是它是强大的...
有没有类似的东西...
$popup = New-Object -ComObject wscript.shell
$popup.popup("Running Script, Please Wait....",0,"Running...",0x1)
###RUN SCRIPT HERE...
$popup.close()
编辑::: 对于“为什么我要弹出而不是writeprogress或其他什么东西”这个问题……原因是因为我在gui中这样做。不在命令行中。因此,我需要gui基本上告知该人正忙,某些任务可能需要6个小时以上才能完成,并且我不希望他们在当前手头的任务运行时四处点击并做其他事情。>
编辑2 ::: 我将保持开放状态,因为未回答原始问题,但是我使用以下代码创建了解决方法。
$LabelAlert = New-Object system.windows.forms.label
$LabelAlert.Text = "Working, Please wait."
$LabelAlert.location = New-Object System.Drawing.Point(0,180)
$LabelAlert.width = 590
$LabelAlert.height = 25
$LabelAlert.Visible = $false
$LabelAlert.TextAlign = "TopCenter"
$Form.Controls.Add($LabelAlert)
$FormGroupBox = New-Object System.Windows.Forms.GroupBox
$FormGroupBox.Location = New-Object System.Drawing.Size(0,0)
$FormGroupBox.width = 600
$FormGroupBox.height = 375
$Form.Controls.Add($FormGroupBox)
$startAlert = {
$LabelAlert.Visible = $true
$FormGroupBox.Visible = $false
}
$stopAlert = {
$LabelAlert.Visible = $false
$FormGroupBox.Visible = $true
}
每个表单部分均已移动到组框内。分组框的大小与我的窗口相同。
每运行一次耗时的脚本
&$startAlert
....script commands go here...
&$stopAlert
答案 0 :(得分:1)
您可以使用Start-Job
在后台作业中运行弹出窗口,这将使脚本在出现后继续运行:
$Job = Start-Job -ScriptBlock {
$popup = New-Object -ComObject wscript.shell
$popup.popup("Running Script, Please Wait....",0,"Running...",0x1)
}
#Run script here..
但是我看不出有什么方法可以强制弹出窗口在脚本结尾处关闭(尝试Remove-Job -Force
甚至Stop-Process conhost -Force
,但似乎都没有用)。
但是,正如其他人所说,更好的选择是将状态写入PowerShell窗口。您可能需要查看Write-Progress
cmdlet,可使用它在正在运行的脚本上显示进度条。