2014-12-09 6 views
1

나는 시간이 지연된 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() 
+0

네이티브 진행률 표시 줄에'Write-Progress'를 사용하지 않은 특별한 이유와'$ host.UI.RawUI.ReadKey ("NoEcho, IncludeKeyDown")'사용자가 완료 될 때까지 기다리는 중입니까? –

답변

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()