代码之家  ›  专栏  ›  技术社区  ›  Robert Cotterman

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

  •  1
  • Robert Cotterman  · 技术社区  · 7 年前

    我正在创建一个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
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Mark Wragg    7 年前

    你可以用 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。