使用foreach循环写入进度问题

时间:2012-08-28 16:11:14

标签: powershell active-directory foreach

对于这个问题,可能是一个非常简单的答案。我无法获得写作进度。我很确定问题是我的变量,但我似乎无法弄明白究竟是什么。

现在它的作用是在没有输出文件存在的情况下第一次运行炸弹。它会告诉我:

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现有脚本再次运行该脚本,它可能会起作用,它可能无效。即使它确实如此,进度条也会在中途开始。

这是我第一次尝试让写作进度工作,所以它可能非常简单,但我不太清楚到底要找什么。我们非常感谢您提供的任何建议。

最终代码

    #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)
}

1 个答案:

答案 0 :(得分:13)

在最后一个循环中,您需要在每次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)
}