【问题标题】:Write-progress trouble with foreach loopforeach 循环的写入进度问题
【发布时间】:2012-08-23 04:51:18
【问题描述】:

这可能是一个非常简单的答案。我无法让写入进度正常工作。我很确定问题出在我的变量上,但我似乎无法弄清楚到底是什么。

它现在所做的是在不存在任何输出文件时在第一次运行时进行炸弹。它会告诉我:

Write-Progress : Cannot validate argument on parameter 'PercentComplete'. The 300 ar gument is greater than the maximum allowed range of 100. Supply an argument that is less than 100 and then try the command again.

它提到的“300 参数”每次都是一个不同的随机数。

现在,如果我使用现在存在的 livepcs.txt 再次运行脚本,它可能会工作,也可能不会工作。即使是这样,进度条也会从一半开始。

这是我第一次尝试让 write-progress 工作,所以它可能非常简单,但我不太确定要寻找什么。您能提供的任何建议将不胜感激。

最终代码

    #Import active directory module
Import-Module active*

#Query AD for all computers by name filter, AND that have an IP listed in AD, store in variable (use line 10)

$PCs = Get-ADComputer -Properties * -Filter {name -like "PCNameDomainPrefix*"} | Where-Object {$_.Ipv4Address -ne $null} | select name,ipv4address

#Dump list of PCs into CSV
$PCs | Export-Csv c:\script\pcs.csv

#Begin foreach loop to see which servers are alive

$i = 0

$livePCs = ForEach($name.name in $PCs) #specify the name string using .name after the variable
    {
    $entry = Test-Connection $name.name -count 1 -quiet #Test-connection pings. The -quiet parameter forces a boolean result (True/False).
    if ($entry -eq "True") {out-file -InputObject $name.name -Encoding ASCII -Width 50 -Append c:\script\livepcs.txt ; Write-Host "$name.name pings"} 
    else { write-host "server $name could not be contacted"}
    $i++
    Write-Progress -activity "Pinging servers . . ." -status "Pinged: $i of $($PCs.Count)" -percentComplete (($i / $PCs.Count)  * 100)
    }


#Announce WMI portion of script, clear host to get rid of status bar    
Clear-Host
Write-Host
Write-Host
Write-Host
Write-Host "Beginning WMI scans. Please wait..."
Write-Host
Write-Host
Write-Host


$livePCs = Get-Content C:\script\livepcs.txt

#Begin foreach loop to query each live machine using WMI. 

$i = 0    

foreach($livePC in $livePCs)
{
   $entry = Get-WmiObject win32_product -computername $livePC -Filter "Name LIKE '%db2%'"

   # do the work
   if ($entry -ne $null)
   {
     $livePc,$entry | out-file c:\script\db2pcs.txt -Encoding ASCII -Width 50 -Append
     Write-Host "$livePC has DB2 installed"
   } 
   else
   {
     write-host "$livePC does not have DB2 installed"
   }

   # update counter and write progress
   $i++
   Write-Progress -activity "Scanning WMI . . ." -status "Scanned: $i of $($livePCs.Count)" -percentComplete (($i / $livePCs.Count)  * 100)
}

【问题讨论】:

  • 您尚未在示例代码中的任何位置定义变量$i
  • 你不必这样做 $($livePCs).. $livePCs 就足够了
  • @latkin,谢谢,这有帮助。进度条不会移动。它只是停留在起点附近,再也没有移动过。还有其他建议吗?
  • 每次查询 WMI 时都需要增加$i,然后再次调用Write-Progress。请更新您的代码以显示您所做的更改。
  • @latkin,好的,代码已更新! WMI 调用完成后如何正确增加我的 $i 变量?

标签: powershell active-directory foreach


【解决方案1】:

在最后一个循环中,您需要在每次 WMI 查询后递增计数器,然后使用更新后的值调用 Write-Progress

清理干净,格式精美:

$i = 0    

foreach($livePC in $livePCs)
{
   $entry = Get-WmiObject win32_product -computername $livePC -Filter "Name LIKE '%db2%'"

   # do the work
   if ($entry -ne $null)
   {
     $livePc,$entry | out-file c:\script\db2pcs.txt -Encoding ASCII -Width 50 -Append
     Write-Host "$livePC has DB2 installed"
   } 
   else
   {
     write-host "$livePC does not have DB2 installed"
   }

   # update counter and write progress
   $i++
   Write-Progress -activity "Scanning WMI . . ." -status "Scanned: $i of $($livePCs.Count)" -percentComplete (($i / $livePCs.Count)  * 100)
}

【讨论】:

  • 我为所有好奇的人发布了我的最终代码(为了人类的福祉)。我继续为脚本的测试连接部分添加了另一个写入进度例程,因为我最终需要查询数百台 PC。无论如何,再次感谢您的帮助!
猜你喜欢
  • 2014-04-19
  • 2017-10-22
  • 1970-01-01
  • 1970-01-01
  • 2011-08-08
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 2020-05-08
相关资源
最近更新 更多