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

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($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
上一篇:win10系统下载-靠谱推荐


下一篇:【JSP】上传图片到数据库中