gpt4 book ai didi

c# - WPF 进度条 PowerShell

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:28 24 4
gpt4 key购买 nike

一段时间以来,我一直在尝试将 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 地址列表(一个 System.Windows.Forms.TextBox 在我的脚本中命名为 $boxIpInput) 并对每个执行 Test-Connection 条件语句。为了让用户看到脚本在 ping 测试中取得的进展,我使用了 System.Windows.Forms.ProgressBar 来传达脚本的进度;我通过谷歌搜索“Powershell Forms Progress Bar”发现了这一点,并发生在this上。链接。

为了创建 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 的输入并将其通过管道传输到 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)
}
}
}

就我个人而言,我建议使用 System.Windows.Forms.ProgressBar 对象,而不是您目前使用的 Write-Progress 方法;对于您的代码,您可以将 ProgressBar 对象添加为 Add_Click 事件的一部分或者在生成主 $Form 对象然后开始时创建它Add_Click 事件期间的值增量。

为了进一步帮助您,我需要了解您计算进度的依据是什么 - 我真的看不出您计算的依据是什么(除了“设备检查”部分)因此,我不愿意结合 ProgressBar 表单如何在您的代码上下文中工作的示例。如果你想让我进一步帮助你,你需要给我更多的细节。 :)

关于c# - WPF 进度条 PowerShell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35490704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com