5

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

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

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

1 回答 1

18

在最后一个循环中,您需要在每次 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)
}
于 2012-08-28T19:51:26.403 回答