PowerShell进度条挂程序

时间:2014-12-09 22:05:28

标签: powershell

我正在尝试编写一个延时关闭的PowerShell脚本。我有一个窗口弹出一个进度条,然后窗口关闭。我的问题是,当进度条运行时,我无法点击任何按钮或做任何事情。我已经包含了我的代码。思考? (提前感谢您的时间!)

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

# The Form Window
$objForm = New-Object System.Windows.Forms.Form                    # Create form
$objForm.Text = "UWB: IT Updates"                                  # Form title
$objForm.StartPosition = "CenterScreen"                            # Form start position

# Background Image
$Image = [system.drawing.image]::FromFile("C:\Users\pcampbell\Desktop\UW_W-Logo_RGB.png")
$objForm.BackgroundImage = $Image
$objForm.BackgroundImageLayout = "Center"
$objForm.Width = $Image.Width * 1.25                               # Modified width to accomodate label
$objForm.Height = $Image.Height
$objForm.Opacity = 0.9                                             # Set opacity

# Font
$Font = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Bold)
$objForm.Font = $Font


$objForm.KeyPreview = $True                                        # Allow form to get all keypresses first
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Text = "Postpone"
$OKButton.AutoSize = $True
$OKButton.Location = New-Object System.Drawing.Size((($Image.Width * 1.25/4) - $OKButton.Width),(($Image.Height/2) - $OKButton.Height))
$OKButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(((($Image.Width * 1.25/4) * 3) - $CancelButton.Width),(($Image.Height/2) - $CancelButton.Height))
$CancelButton.AutoSize = $True
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.AutoSize = $True
$objLabel.Text = "UWB: IT - Would you like to postpone updates?"
$objLabel.Location = New-Object System.Drawing.Size((($objForm.Width/5)-$objLabel.Width),20)
$objLabel.BackColor = "Transparent"
$objForm.Controls.Add($objLabel)

# Progress Bar
$progressBar1 = New-Object System.Windows.Forms.ProgressBar
$progressBar1.Name = 'progressBar1'
$progressBar1.Value = 0
$progressBar1.Style="Continuous"

# Size of Progress Bar
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = $objForm.Width - 40
$System_Drawing_Size.Height = 20
$progressBar1.Size = $System_Drawing_Size

# Position of Progress Bar
$progressBar1.Left = 10
$progressBar1.Top = ($objForm.Height - 80)
$objForm.Controls.Add($progressBar1)

# Spawn Form
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})

# Stopwatch and Progress Bar
$sw = [System.Diagnostics.Stopwatch]::startNew()

$cd_time = 10
[void] $objForm.ShowDialog()


while ($sw.Elapsed.TotalSeconds -lt $cd_time) 
{ 
    start-sleep -s 1;
    $sw.Elapsed.Seconds | write-host;
    [int]$pct = ($sw.Elapsed.Seconds/$cd_time)*100;
    # update the progress bar
    $progressbar1.Value = $pct;
    $objForm.Refresh();

}

$sw.Stop()
$objForm.Close()

1 个答案:

答案 0 :(得分:0)

你应该看看System.Windows.Forms.Timer。我刚才玩了一下,并想出了这个:

$cd_time = 10
$elapsed = 0
$myTimer = New-Object System.Windows.Forms.Timer
$myTimer.Interval = 1000 # 1000 = 1 second
$myTimer.Add_Tick({
   $script:elapsed += 1
   Write-host $elapsed
   $script:progressbar1.Value = [int]($script:elapsed/$script:cd_time*100)
   If ($script:elapsed -eq $script:cd_time) { $script:objForm.Close() }
   $script:objForm.Refresh()
})

# Spawn Form
$objForm.Topmost = $True
$objForm.Add_Shown({
   $objForm.Activate()
   $myTimer.Start()
})
[void] $objForm.ShowDialog()