powershell對txt文件的服務器進行ping操作,txt文件有幾百臺服務器要進行Ping操作。每行一個
#//*************************************************************#//編輯人:
#//編輯單位:
#//編輯作用:ping#//編制時間:2016.01.05#//*************************************************************$stopWatch = [system.diagnostics.stopwatch]::startNew()#************獲取當前腳本執(zhí)行的目錄$Location ="d:/" #$PSScriptRoot#**********************創(chuàng)建以yyyy-MM-dd的日志文件夾$folderName ="ping" #*********************全路徑$folderPath = $Location + "/" + $folderName#*********************如果根文件夾不存在。則創(chuàng)建根文件夾If((Test-Path $folderPath) -eq $False) { Write-Host "開始創(chuàng)建文件夾...---------------" -ForegroundColor Green New-Item -path $Location -name $folderName -itemType "directory" Write-Host "創(chuàng)建文件夾完畢...---------------" -ForegroundColor Green}#**************************創(chuàng)建2個文件$pingFileName ="ok.txt"#**************************創(chuàng)建ping通的文件$pingFilePath = $folderPath + "/" + $pingFileName ;If((Test-Path $pingFilePath) -eq $False) { Write-Host "開始創(chuàng)建ping通文件...---------------" -ForegroundColor Green New-Item -path $folderPath -name $pingFileName -itemType "File" Write-Host "創(chuàng)建ping通文件完畢...---------------" -ForegroundColor Green}#**************************創(chuàng)建ping不通的文件$nopingFileName ="no.txt"$nopingFilePath = $folderPath + "/" + $nopingFileName ;If((Test-Path $nopingFilePath) -eq $False) { Write-Host "開始創(chuàng)建ping不通文件...---------------" -ForegroundColor Green New-Item -path $folderPath -name $nopingFileName -itemType "File" Write-Host "創(chuàng)建ping不通文件完畢...---------------" -ForegroundColor Green}#**************讀取計算機文件TXT(格式一行一個)$computerObjects = Get-Content c:/DNS.txt#***************得到總的要處理的計算機臺數(shù)$totalCount = $computerObjects.count;#***************提示信息$sContent = "一共有:" + $totalCount.ToString() +"臺服務器需要處理!"Write-Host $sContent -ForegroundColor Green#***************成功的服務器臺數(shù)[int]$successCount = 0;#***************失敗的服務器臺數(shù)[int]$failCount = 0;ForEach($computerObject in $computerObjects){ try { #******************如果ping得通 if (Test-Connection $computerObject -Count 1 -ea 0 -Quiet) { #*********************ping通信息打印 $pingOK = "ping通" + $computerObject.ToString() Write-Host $pingOK -ForegroundColor Green #*********************寫入ping通文件 Add-Content -Path $pingFilePath -Value $computerObject #*********************計數(shù)器+1 $successCount = $successCount + 1 } #*******************如果ping不通 else { #*********************ping不通信息打印 $pingNO = "ping不通" + $computerObject.ToString() Write-Host $pingNO -ForegroundColor Red #*********************寫入ping不通文件 Add-Content -Path $nopingFilePath -Value $computerObject #*********************計數(shù)器+1 $failCount = $failCount + 1 } } catch { #*********************出現(xiàn)錯誤 $errMsg = "ping"+$computerObject.ToString()+ "過程中出現(xiàn)錯誤" Write-Host $errMsg -ForegroundColor Blue #*********************寫入ping不通文件 Add-Content -Path $nopingFilePath -Value $computerObject #*********************計數(shù)器+1 $failCount = $failCount + 1 }}#*************執(zhí)行完畢$stopWatch.Stop()#****************計算一共花費多少時間$totalseconds = $stopWatch.Elapsed.TotalSeconds#**********************打印出一共花費多少時間$tooltip = "處理完畢,一共花費" + $totalseconds.ToString() +"秒"Write-Host $tooltip -ForegroundColor Red
新聞熱點
疑難解答