有没有办法在脚本运行时运行弹出窗口?

问题描述 投票:0回答:2

我正在创建一个图形用户界面,并希望有一个弹出窗口让您知道它很忙,但在完成特定任务后关闭。我唯一能找到的是以下...

$popup = New-Object -ComObject wscript.shell
$popup.popup("Running Script, Please Wait....",0,"Running...",0x1)

但问题是,这是在等待响应,然后它将运行脚本。我并不是要求有人给我写一个脚本,而是要求一些关于在哪里可以找到这些信息的指南。

我需要 powershell 弹出一个窗口,然后在运行脚本时将其保留,然后在脚本运行完毕后将其关闭。 最好只是有另一个窗口窗体来运行带有标签的脚本吗?对于一项简单的任务来说,这似乎是过多的工作量。但它是 powershell...

有没有类似...

$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
powershell popup wsh
2个回答
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,您可以使用它在正在运行的脚本上显示进度条。


0
投票

如果有人感兴趣的话,这就是我所做的。新弹出窗口不适用于在后台运行的进程,因此我只是将其用于前后提示,然后在搜索文件时使用写入进度进行等待。 new-popup函数是别人写的,我在网上找到的。它使用 Wscript.shell

new-popup -message "Searching for files" -title "FILE SEARCH" -time 1 -Buttons "OK" -Icon "Information"
$x = 0
do
{
    Write-Progress -Activity "FILE SEARCH" -status "Searching for files" -PercentComplete 50
    $filestodelete = Get-ChildItem -Path 'C:\' -Recurse -ErrorAction SilentlyContinue | ? {$_.Name -like 'FailoverDistributed_*.log'}
    $x = 50
}
until ($x -eq 50)
new-popup -message "File Search is complete" -title "FILE SEARCH" -time 1 -Buttons "OK" -Icon "Information"
© www.soinside.com 2019 - 2024. All rights reserved.