Powershell 4写入进度

Powershell 4写入进度,powershell,powershell-3.0,Powershell,Powershell 3.0,我正试图从一个长长的列表中运行IP检查。。嗯,IPs。。使用[System.Net.DNS] 这很好用,不过我想在上面放一个简单的进度条。无论是秒还是百分比。。。我真的不在乎。我只是想要一个好的进度条出现,告诉我需要等待多久 $colComputers = get-content $File foreach ($strComputer in $colComputers) { $IP = try {$dnsresult = [System.Net.DNS]::GetHostEntry($strCo

我正试图从一个长长的列表中运行IP检查。。嗯,IPs。。使用[System.Net.DNS] 这很好用,不过我想在上面放一个简单的进度条。无论是秒还是百分比。。。我真的不在乎。我只是想要一个好的进度条出现,告诉我需要等待多久

$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."
}
脚本运行得很好,只是停留在这个进度条上。
我在想,如果能够确定列表中包含多少IP,并让它从最后一个倒数到第一个倒数,那就太好了。

我很难理解您的脚本

  • 什么是
    $IP=try{}
  • 您输出了
    $IP
    (我认为它总是空的),为什么
  • 您从未使用过
    $dnsresult
  • 我甚至不知道progressbar将如何帮助任何人
  • 您确实需要使代码更具可读性。避免“转义换行符”
这就是你想做的吗

$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++

}

我修改了脚本,希望能让进度条正常工作…-)有了你的改变,它就像一个符咒,谢谢你,伙计!我的帖子被编辑了,但它清楚地表明我是一个有进度条的新手