Powershell performs ping on the server of the txt file,

Source: Internet
Author: User

Powershell performs ping on the server of the txt file,

Powershell performs ping on the server of the txt file. The txt file has hundreds of servers that need to be pinged. One per line

#//************************************* * *********************** # // Edited:
# // Edit unit:
# // Editing function: ping # // preparation time: 2016.01.05 #//************************************ * ************************ $ stopWatch = [system. diagnostics. stopwatch]: startNew () # ************* get the directory for executing the current script $ Location = "d: \ "# $ PSScriptRoot # ********************** create a log folder with yyyy-MM-dd $ folderName = "ping" # ********************* full path $ folderPath = $ Location + "\" + $ folderName # ********************** if the root folder does not exist. Create the root folder If (Test-Path $ folderPath)-eq $ False) {Write-Host "to create the folder... --------------- "-ForegroundColor Green New-Item-path $ Location-name $ folderName-itemType" directory "Write-Host" folder created... --------------- "-ForegroundColor Green} # *************************** create 2 files $ pingFileName = "OK .txt" # ************************* create a ping-through file $ pingFilePath = $ folderPath + "\" + $ pingFileName; if (Test-Path $ pingF IlePath)-eq $ False) {Write-Host "to start creating the ping file... --------------- "-ForegroundColor Green New-Item-path $ folderPath-name $ pingFileName-itemType" File "Write-Host" the ping File has been created... --------------- "-ForegroundColor Green} # *************************** create a pinging File $ nopingFileName = "no.txt" $ nopingFilePath = $ folderPath + "\" + $ nopingFileName; if (Test-Path $ nopingFilePath)-eq $ False) {Write-Host "start to create pi Ng file not available... --------------- "-ForegroundColor Green New-Item-path $ folderPath-name $ nopingFileName-itemType" File "Write-Host" the ping File cannot be created... --------------- "-ForegroundColor Green} # **************** read the computer file TXT (one row in format) $ computerObjects = Get-Content c: \ DNS.txt # *************** obtain the total number of computers to be processed. $ totalCount = $ computerObjects. count; # *************** message $ sContent = "Total:" + $ totalCount. toString () + "servers need to be processed! "Write-Host $ sContent-ForegroundColor Green # ****************** number of successful servers [int] $ successCount = 0; # *************** number of failed servers [int] $ failCount = 0; ForEach ($ computerObject in $ computerObjects) {try {# ******************* if the ping succeeds if (Test-Connection $ computerObject-Count 1-ea 0-Quiet) {# ********************* print the ping Information $ pingOK = "ping" + $ computerObject. toString () write-Host $ pingOK-ForegroundColor Green # *********************** Write the ping file Add-Content-Path $ pingFilePath-Value $ computerObject # ********************** counter + 1 $ successCount = $ successCount + 1 }#* * **************** if else cannot be pinged {#**************** * *** print the ping Failure Information $ pingNO = "ping failure" + $ computerObject. toString () write-Host $ pingNO-ForegroundColor Red # *********************** Add-Content-Path $ nopingFilePath-Value $ computerObject # *********************** counter + 1 $ failCount = $ failCount + 1} catch {# ******************** error $ errMsg = "ping" + $ computerObject. toString () + "an error occurs during the process." Write-Host $ errMsg-ForegroundColor Blue # *********************** the ping cannot be written. file Add-Content-Path $ nopingFilePath-Value $ computerObject # ********************* counter + 1 $ failCount = $ failCount + 1 }}# ************** the execution is complete $ stopWatch. stop () # ***************** calculate the total time spent $ totalseconds = $ stopWatch. elapsed. totalSeconds # ********************** print out the total time spent $ tooltip = "processing is complete, total cost "+ $ totalseconds. toString () + "second" Write-Host $ tooltip-ForegroundColor Red

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.