简体   繁体   English

Powershell 4写入进度

[英]Powershell 4 Write-Progress

I'm trying to run an IP check from a loooong list of.. well, IPs.. with [System.Net.DNS] This works great, however I want to put a simple progress bar on it. 我正在尝试通过[System.Net.DNS]的长期列表进行IP检查。这很好,但是我想在上面放一个简单的进度条。 Be it seconds or Percentage... Don't really care. 是几秒钟还是百分比...不在乎。 I just want a nice progress bar to show up and tell me how long I need to wait. 我只希望显示一个不错的进度栏,并告诉我需要等待多长时间。

$colComputers = get-content $File
foreach ($strComputer in $colComputers)
{
$IP = try {$dnsresult = [System.Net.DNS]::GetHostEntry($strComputer)} `
catch {$dnsresult = "Fail"}
$IP

for ($IP=100; $IP -gt 1; $IP--) {
  Write-Progress -Activity "Working..." `
   -SecondsRemaining $IP `
   -Status "Please wait."
}

Script runs great, just getting stuck on this progress bar. 脚本运行良好,只是停留在此进度条上。 I was thinking it would be nice if at all possible to determine how many IPs the list contains and just let it count down from last to first. 我当时想,如果可以确定列表包含多少IP,然后从倒数第一开始倒数,那将是很好的选择。

I'm having trouble understanding your script. 我在理解您的脚本时遇到了麻烦。

  • What is $IP = try { } ? 什么是$IP = try { }
  • You output $IP (which I though would always be null), why?. 您输出$IP (尽管我总是为空),为什么呢?
  • You never use $dnsresult .. 您永远不会使用$dnsresult ..
  • I'm not even sure how that progressbar is going to help anyone..... 我什至不确定进度条将如何帮助任何人.....
  • You really need make your code more readable. 您确实需要使代码更具可读性。 Avoid "escaping linebreaks". 避免“转义换行符”。

Is this what you were trying to do? 这是您想要做的吗?

$colComputers = @(get-content $File)
$count = $colComputers.Count
$i = 1
foreach ($strComputer in $colComputers)
{

    #Write-Progress needs -percentagecomplete to make the progressbar move
    Write-Progress -Activity "Working... ($i/$count)" -PercentComplete ($i/$colComputers.Count*100) -Status "Please wait."

    #What is IP = try { } :S
    try {
        $dnsresult = [System.Net.DNS]::GetHostEntry($strComputer)
    }
    catch {
        $dnsresult = "Fail"
    }

    #Do something with $dnsresults...

    #Increase counter i
    $i++

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM