博客
关于我
powershell对txt文件的服务器进行ping操作
阅读量:797 次
发布时间:2023-03-04

本文共 2018 字,大约阅读时间需要 6 分钟。

powershell对txt文件中的服务器列表进行ping操作,每行一个IP地址进行处理。处理几百台服务器需要注意性能优化,以避免长时间运行。

$stopWatch = [system.diagnostics.stopwatch]::startNew()$Location = "d:\\" + $PSScriptRoot$folderName = "ping"$folderPath = $Location + "\" + $folderNameIf((Test-Path $folderPath) -eq $False) {    Write-Host "创建文件夹..."    New-Item -path $Location -name $folderName -itemType "directory"}$pingFileName = "ok.txt"$pingFilePath = $folderPath + "\" + $pingFileNameIf((Test-Path $pingFilePath) -eq $False) {    Write-Host "创建ping通文件..."    New-Item -path $folderPath -name $pingFileName -itemType "File"}$nopingFileName = "no.txt"$nopingFilePath = $folderPath + "\" + $nopingFileNameIf((Test-Path $nopingFilePath) -eq $False) {    Write-Host "创建ping不通文件..."    New-Item -path $folderPath -name $nopingFileName -itemType "File"}$computerObjects = Get-Content C:\DNS.txt$totalCount = $computerObjects.count$sContent = "一共有:" + $totalCount.ToString() + "台服务器需要处理!"Write-Host $sContent -ForegroundColor Green$successCount = 0$failCount = 0ForEach($computerObject in $computerObjects) {    try {        if (Test-Connection $computerObject -Count 1 -ea 0 -Quiet) {            $pingOK = "ping通" + $computerObject.ToString()            Write-Host $pingOK -ForegroundColor Green            Add-Content -Path $pingFilePath -Value $computerObject            $successCount++        } else {            $pingNO = "ping不通" + $computerObject.ToString()            Write-Host $pingNO -ForegroundColor Red            Add-Content -Path $nopingFilePath -Value $computerObject            $failCount++        }    } catch {        $errMsg = "ping过程中出现错误:" + $computerObject.ToString()        Write-Host $errMsg -ForegroundColor Blue        Add-Content -Path $nopingFilePath -Value $computerObject        $failCount++    }}$stopWatch.Stop()$totalseconds = $stopWatch.Elapsed.TotalSeconds$tooltip = "处理完毕,一共花费" + $totalseconds.ToString() + "秒"Write-Host $tooltip -ForegroundColor Red

 

转载于:https://www.cnblogs.com/love007/p/5126605.html

你可能感兴趣的文章
Plotly 中的行悬停文本
查看>>
Plotly 停用 x 轴排序
查看>>
Plotly 域变量解释(多图)
查看>>
Plotly 绘制表面 3D 未显示
查看>>
Plotly-Dash 存在未知问题并创建“加载依赖项时出错“;通过使用 Python-pandas.date_range
查看>>
Plotly-Dash:如何过滤具有多个数据框列的仪表板?
查看>>
Plotly:如何为 x 轴上的时间序列设置主要刻度线/网格线的值?
查看>>
Plotly:如何从 x 轴删除空日期?
查看>>
Plotly:如何从单条迹线制作堆积条形图?
查看>>
Plotly:如何以 Root 样式绘制直方图,仅显示直方图的轮廓?
查看>>
Plotly:如何使用 Plotly Express 组合散点图和线图?
查看>>
Plotly:如何使用 plotly.graph_objects 和 plotly.express 定义图形中的颜色?
查看>>
Plotly:如何使用 Python 对绘图对象条形图进行颜色编码?
查看>>
Plotly:如何使用 updatemenus 更新一个特定的跟踪?
查看>>
Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?
查看>>
Plotly:如何向烛台图添加交易量
查看>>
Plotly:如何在 plotly express 中找到趋势线的系数?
查看>>
Plotly:如何在桑基图中设置节点位置?
查看>>
pm2 start命令中的json格式详解
查看>>
pm2启动报错
查看>>