[英]WPF Progress Bar PowerShell
我已經嘗試將WPF進度欄插入PowerShell腳本已有一段時間了,但是我無法弄清楚。 我嘗試了13種不同的方法,但均無濟於事。 這是我的代碼:
$WPFRunQuick_Button.Add_Click({
<# Add-Type -AssemblyName System.Windows.Forms
$Form.Text = "Processing"
$Label = New-Object System.Windows.Forms.Label
$Font = New-Object System.Drawing.Font("Times New Roman",16,[System.Drawing.FontStyle]::Bold)
$form.Font = $Font
$Form.Controls.Add($Label)
$Label.Text = "You have run the quick test, please be patient as it runs."
$Label.AutoSize = $True
$form.autosize = $true
$form.AutoScroll = $true
$form.autosizemode = "GrowAndShrink"
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.SizeGripStyle = "Hide"
$form.startposition = "CenterScreen"
$Form.Visible = $True
$Form.Update() #>
$max = 100
For($i = 1; $i -le $max; $i++){
Write-progress -Activity “Device Check” -Status “Testing, please wait.” `
-percentcomplete ($i / $max*100) -id 1
sleep 1
}
# Quick Test Run Function
Quick_Test_Run
# Prompt to send logs to associate support
$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("Do you want to send these results?", 0,"Results",4)
if ($intAnswer -eq 6) {
$a.popup("Results Sent")
# Create email
$EmailSubject = "Tasa Test"
$EmailTo = "usupportlogs@test.com"
$EmailBody = [IO.File]::ReadAllText("C:\Temp\PMCS_TicketLogs.log")
# Send email to usupportlogs inbox
Send-MailMessage -From "PMCS_No_Reply@test.com" -To $EmailTo -subject $EmailSubject -Body $EmailBody -SmtpServer "SMTPRR.Cerner.Com"
} else {
$a.popup("Sending Cancelled")
}
$Form.Close()
})
當我嘗試在調用快速測試功能之前放入一個時,它將一路加載並啟動測試,如果在測試之后放入它,則將進行測試,然后啟動進度欄。 我只想要此腳本的簡單進度欄。 任何幫助或見解將不勝感激,謝謝!
在我最近創建的腳本中,我的GUI應用程序將通過迭代用戶提交的IP地址列表(在我的腳本中命名為$boxIpInput
的System.Windows.Forms.TextBox
來檢查公司財產中一系列計算機的可用性。並對每個語句執行一個Test-Connection
條件語句。 為了讓用戶看到ping測試腳本的進度,我使用了System.Windows.Forms.ProgressBar
來傳達腳本的進度; 我通過搜索“ Powershell Forms進度欄”發現了這一點,並在此鏈接上發生了。
為了創建ProgressBar
對象,我使用了以下代碼;
$barPingProgress= New-Object System.Windows.Forms.ProgressBar
$barPingProgress.Size= New-Object System.Drawing.Size(274,20);
$barPingProgress.Location= New-Object System.Drawing.Size(8,440);
$barPingProgress.Style= 'Continuous'
$barPingProgress.Value= 0
$objForm.Controls.Add($barKioskPingProgress);
單擊$btnRunIpCheck
按鈕執行ping測試后,我使用了$boxIpInput
的輸入,並將其通過管道$boxIpInput
到ForEach-Object
循環中,以類似於Quick_Test_Run函數之前的For循環的方式遞增地增加進度欄的數量(為了簡潔起見,我從下面的代碼段中刪除了所有不相關的代碼);
//other button initialisation code (removed)
...
$btnRunIpCheck.Add_Click({
//some code to validate entries (removed)
...
$arrTmpIpAddresses= $boxIpInput.Text.Split(" `n",[System.StringSplitOptions]::RemoveEmptyEntries)
//some code to check size of list and warn users of the time it would take (removed)
...
$arrTmpIpAddresses | ForEach-Object {
$intOrderCount++
[int]$tmpProgNum= ($intOrderCount/$arrTmpIpAddresses.Count) * 100
$barPingProgress.Value= $tmpProgNum
//ping test code (removed)
}
}
}
就個人而言,我建議您在當前使用的Write-Progress
方法上使用System.Windows.Forms.ProgressBar
對象。 在你的代碼的情況下,您可以添加進度對象的一部分Add_Click
事件或者當你產生主$窗體對象,然后在開始值遞增它創造Add_Click
事件。
為了進一步為您提供幫助,我需要了解您在計算進度的基礎是什么-我真的看不到您在計算的基礎(除了“設備檢查”部分),所以我不知道結合一個在您的代碼上下文中如何使用ProgressBar表單的示例感到不自在。 如果您想讓我進一步幫助您,則需要給我更多細節。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.