使用进度条和Powershell下载

时间:2019-12-12 10:11:45

标签: forms powershell download

我是PowerShell的新手,需要帮助 我正在尝试在脚本中应用进度条 我发现了这个清单,以表格形式显示了文件计数

$Path = "E:\AppTest"    ## --- Put Folder-Path Here 
If (Test-Path $Path) {
Write-Host
Write-Host "Listing All Files Found In $Path" -ForegroundColor "Yellow"
Write-Host "=========================================" -ForegroundColor "Yellow"
Add-Type -assembly System.Windows.Forms
## -- Create The Progress-Bar
$ObjForm = New-Object System.Windows.Forms.Form
$ObjForm.Text = "Demonstration of Progress-Bar In PowerShell"
$ObjForm.Height = 100
$ObjForm.Width = 500
$ObjForm.BackColor = "White"

$ObjForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$ObjForm.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen

## -- Create The Label
$ObjLabel = New-Object System.Windows.Forms.Label
$ObjLabel.Text = "Starting. Please wait ... "
$ObjLabel.Left = 5
$ObjLabel.Top = 10
$ObjLabel.Width = 500 - 20
$ObjLabel.Height = 15
$ObjLabel.Font = "Tahoma"
## -- Add the label to the Form
$ObjForm.Controls.Add($ObjLabel)

$PB = New-Object System.Windows.Forms.ProgressBar
$PB.Name = "PowerShellProgressBar"
$PB.Value = 0
$PB.Style="Continuous"

$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 500 - 40
$System_Drawing_Size.Height = 20
$PB.Size = $System_Drawing_Size
$PB.Left = 5
$PB.Top = 40
$ObjForm.Controls.Add($PB)
## -- Show the Progress-Bar and Start The PowerShell Script
$ObjForm.Show() | Out-Null
$ObjForm.Focus() | Out-NUll
$ObjLabel.Text = "Starting. Please wait ... "
$ObjForm.Refresh()
Start-Sleep -Seconds 1

## -- Execute The PowerShell Code and Update the Status of the Progress-Bar

$Result = Get-ChildItem -Path $Path -File -Recurse -Force | Select Name, @{Name="Path";Expression={$_.FullName}}
$Counter = 0
ForEach ($Item In $Result) {
    ## -- Calculate The Percentage Completed
    $Counter++
    [Int]$Percentage = ($Counter/$Result.Count)*100
    $PB.Value = $Percentage
    $ObjLabel.Text = "Recursive Search: Writing Names of All Files Found Inside $Path"
    $ObjForm.Refresh()
    Start-Sleep -Milliseconds 150
    # -- $Item.Name
    "`t" + $Item.Path
}
$ObjForm.Close()
Write-Host "`n"} Else {
Write-Host
Write-Host "`t Cannot Execute The Script." -ForegroundColor "Yellow"
Write-Host "`t $Path Does Not Exist in the System." -ForegroundColor "Yellow"
Write-Host}

我创建了一个看起来像这样的表单(我将其缩小到更清楚),我希望能够在表单的进度栏中显示下载内容 上面的命令显示了count命令 如何将其应用于下载和进度栏

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '1370,720'
$Form.TopMost                    = $true
$Form.StartPosition = "CenterScreen"

$btn                      = New-Object system.Windows.Forms.Button
$btn.Text                 = 'download'
$btn.width                = 300
$btn.height               = 130
$btn.location             = New-Object System.Drawing.Point(716,485)
$btn.Font                 = 'Microsoft Sans Serif,10'

$ProgressBar                     = New-Object system.Windows.Forms.ProgressBar
$ProgressBar.width               = 703
$ProgressBar.height              = 37
$ProgressBar.location            = New-Object System.Drawing.Point(345,660)
$ProgressBar.Maximum             = 100
$ProgressBar.Minimum             = 0
$progressBar.Step                = 1
$i = 0

$Form.Controls.Add($ProgressBar)
$Form.controls.AddRange(@($btn,$ProgressBar))

$btn.Add_Click({Invoke-WebRequest -Uri "https://speed.hetzner.de/100MB.bin"  -OutFile "C:\script\100MB.bin"})

[void]$Form.ShowDialog()

有人可以协助这个吗?

0 个答案:

没有答案