可能是这个问题的一个非常简单的答案。我无法使写入进度工作。我很确定问题出在我的变量上,但我似乎无法弄清楚究竟是什么。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)
}
您还没有定义的变量'你不必做'$($ livePCs)$ i'在示例代码 – latkin
任何地方''.. $ livePCs'就足够了 – manojlds
@latkin,谢谢,这有帮助。进度条不会移动。它只是停留在开始,并且永远不会比这更进一步。任何其他建议? – John