我做了一個腳本來檢查用戶的桌面文件夾是否在cuota的限制下,如果它們在cuota的限制下,服務器的備份將會正確完成。加快Foreach之前的測試連接
每個用戶都有自己的電腦,所以源CSV樣子:
pc1,user1
pc2,user2
pc800,user800
某些計算機Windows XP和一些W7,並且路徑可以不同「的因由我使用測試的路徑
W7 = C:\users\$user\desktop
XP = C:\document and settings\$user\desktop
但測試的路徑是超級慢,我開始每個測試路徑之前,使用一個測試連接-count 1
不管怎麼說,腳本仍然緩慢,在每個「壞ping測試」我輸了很多ti我。
CODE:
$csvLocation = '~\desktop\soourceReport.csv'
$csv = import-csv $csvLocation -Header PCName, User
$OuputReport = '~\desktop\newReport.csv'
# info:
# "209715200" Bytes = 200 MB
$cuota = "209715200"
$cuotaTranslate = "$($cuota/1MB) MB"
Write-Host "Cuota is set to $cuotaTranslate"
$count=1
foreach($item in $csv)
{
write-host "$count# Revisando" $item.User "en" $item.PCName "..." #For debug
if (Test-Connection -Quiet -count 1 -computer $($item.PCname)){
$w7path = "\\$($item.PCname)\c$\users\$($item.User)\desktop"
#echo $w7path #debug
$xpPath = "\\$($item.PCname)\c$\Documents and Settings\$($item.User)\Escritorio"
#echo $xp #debug
if(Test-Path $W7path){
$desktopSize = (Get-ChildItem -Recurse -force $w7path | Measure-Object -ErrorAction "SilentlyContinue" -property length -sum)
write-host -ForegroundColor Green "access succeed"
if($($desktopSize.sum) -gt $cuota){
$newLine = "{0},{1},{2}" -f $($item.PCname),$($item.User),"$("{0:N0}" -f $($desktopSize.sum/1MB)) MB"
$newLine | add-content $outputReport
Write-Host -ForegroundColor Yellow "cuota exceeded! -- added"
}
else{
Write-Host -ForegroundColor DarkYellow "cuota OK"
}
}
elseif(Test-Path $xpPath){
$desktopSize = (Get-ChildItem -Recurse -force $xpPath | Measure-Object -ErrorAction "SilentlyContinue" -property length -sum)
write-host -ForegroundColor Green "access succeed"
if($($desktopSize.sum) -gt $cuota){
$newLine = "{0},{1},{2}" -f $($item.PCname),$($item.User),"$("{0:N0}" -f $($desktopSize.sum/1MB)) MB"
$newLine | add-content $outputReport
Write-Host -ForegroundColor Yellow "cuota exceeded! -- added"
}
else{
Write-Host -ForegroundColor DarkYellow "cuota OK"
}
else{
write-host -ForegroundColor Red "Error! - bad path"
}
}
else{
write-host -ForegroundColor Red "Error! - no ping"
}
$count++
}
Write-Host -ForegroundColor green -BackgroundColor DarkGray "All done! new report stored in $report"
爲了改善它,我保存的所有計算機$列表使用其他的foreach,首先將提到SLOW-的foreach循環之前。
foreach($pcs in $csv){
$alivelist += @($pcs.PCName)
}
Test-Connection -quiet -count 2 -computer $alivelist
現在,我不是現在如何更新或從SOURCE CSV刪除行(「死」 PC用戶)之前,進入第二的foreach做。
我需要你的一些「魔力」,或者至少有一些想法!
感謝
真正能夠加快速度的是並行處理。在PS 2.0中,您可以使用後臺作業,而在3.0中有工作流程。在[這個問題]檢查答案(http://stackoverflow.com/questions/24166998/powershell-script-running-slowly)。 –
http://myitforum.com/cs2/blogs/gramsey/archive/2011/01/07/get-pingstatuslist-ps1-ping-hundreds-of-systems-quickly-using-the-start-job-command-in -powershell.aspx。添加@AlexanderObersht關於後臺作業的評論。我在 – mrwhale
之前使用過這個傢伙腳本,我正在尋找一個快速的ping掃描器並且遇到了這個問題。我解決了這個腳本:https://gallery.technet.microsoft.com/scriptcenter/Fast-asynchronous-ping-IP-d0a5cf0e/ – YetAnotherRandomUser