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

王朝学院·作者佚名  2016-08-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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

#//*************************************************************#//编辑人:

#//编辑单位:

#//编辑作用: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($computerObjectin$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

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航