[英]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.