powershell对txt文件的服务器进行ping操做

powershell对txt文件的服务器进行ping操做,txt文件有几百台服务器要进行Ping操做。每行一个shell

#//*************************************************************
#//编辑人:
#//编辑单位:
#//编辑做用:ping #//编制时间:2016.01.05 #//************************************************************* $stopWatch = [system.diagnostics.stopwatch]::startNew() #************获取当前脚本执行的目录 $Location ="d:\" #$PSScriptRoot #**********************建立以yyyy-MM-dd的日志文件夹 $folderName ="ping" #*********************全路径 $folderPath = $Location + "\" + $folderName #*********************若是根文件夹不存在。则建立根文件夹 If((Test-Path $folderPath) -eq $False) { Write-Host "开始建立文件夹...---------------" -ForegroundColor Green New-Item -path $Location -name $folderName -itemType "directory" Write-Host "建立文件夹完毕...---------------" -ForegroundColor Green } #**************************建立2个文件 $pingFileName ="ok.txt" #**************************建立ping通的文件 $pingFilePath = $folderPath + "\" + $pingFileName ; If((Test-Path $pingFilePath) -eq $False) { Write-Host "开始建立ping通文件...---------------" -ForegroundColor Green New-Item -path $folderPath -name $pingFileName -itemType "File" Write-Host "建立ping通文件完毕...---------------" -ForegroundColor Green } #**************************建立ping不通的文件 $nopingFileName ="no.txt" $nopingFilePath = $folderPath + "\" + $nopingFileName ; If((Test-Path $nopingFilePath) -eq $False) { Write-Host "开始建立ping不通文件...---------------" -ForegroundColor Green New-Item -path $folderPath -name $nopingFileName -itemType "File" Write-Host "建立ping不通文件完毕...---------------" -ForegroundColor Green } #**************读取计算机文件TXT(格式一行一个) $computerObjects = Get-Content c:\DNS.txt #***************获得总的要处理的计算机台数 $totalCount = $computerObjects.count; #***************提示信息 $sContent = "一共有:" + $totalCount.ToString() +"台服务器须要处理!" Write-Host $sContent -ForegroundColor Green #***************成功的服务器台数 [int]$successCount = 0; #***************失败的服务器台数 [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 #*********************计数器+1 $successCount = $successCount + 1 } #*******************若是ping不通 else { #*********************ping不通讯息打印 $pingNO = "ping不通" + $computerObject.ToString() Write-Host $pingNO -ForegroundColor Red #*********************写入ping不通文件 Add-Content -Path $nopingFilePath -Value $computerObject #*********************计数器+1 $failCount = $failCount + 1 } } catch { #*********************出现错误 $errMsg = "ping"+$computerObject.ToString()+ "过程当中出现错误" Write-Host $errMsg -ForegroundColor Blue #*********************写入ping不通文件 Add-Content -Path $nopingFilePath -Value $computerObject #*********************计数器+1 $failCount = $failCount + 1 } } #*************执行完毕 $stopWatch.Stop() #****************计算一共花费多少时间 $totalseconds = $stopWatch.Elapsed.TotalSeconds #**********************打印出一共花费多少时间 $tooltip = "处理完毕,一共花费" + $totalseconds.ToString() +"" Write-Host $tooltip -ForegroundColor Red
相关文章
相关标签/搜索