PowerShell Ping the TXT file server, the TXT file has hundreds of servers to ping. One per line
#//*************************************************************#//Editor:
#//Edit unit:
#//Edit role: Ping#//Preparation time: 2016.01.05#//*************************************************************$stopWatch= [System.diagnostics.stopwatch]::StartNew ()#************ Get the current script execution directory$Location="D:\" #$PSScriptRoot#********************** Creating a log folder with Yyyy-mm-dd$folderName="Ping" #********************* Full path$folderPath=$Location+"\"+$folderName#********************* If the root folder does not exist. The root folder is createdIf((Test-path$folderPath)-eq $False) {Write-host"Start Creating folder ...---------------"-Foregroundcolor Green New-item-path$Location-name$folderName-itemtype"Directory"Write-host"Create folder complete ...---------------"-Foregroundcolor Green}#************************** creation of 2 files$pingFileName="Ok.txt"#************************** Creating a ping pass file$pingFilePath=$folderPath+"\"+$pingFileName ;If((Test-path$pingFilePath)-eq $False) {Write-host"start creating a ping Pass file ...---------------"-Foregroundcolor Green New-item-path$folderPath-name$pingFileName-itemtype"File"Write-host"Create ping Pass file complete ...---------------"-Foregroundcolor Green}#************************** Creating a file that is not ping$nopingFileName="No.txt"$nopingFilePath=$folderPath+"\"+$nopingFileName ;If((Test-path$nopingFilePath)-eq $False) {Write-host"Start creating ping non-pass file ...---------------"-Foregroundcolor Green New-item-path$folderPath-name$nopingFileName-itemtype"File"Write-host"Create ping not pass file complete ...---------------"-Foregroundcolor Green}#************** Read the computer file txt (one line in a format)$computerObjects= get-Content c:\DNS.txt#*************** Get the total number of computers to be processed$totalCount=$computerObjects. Count;#*************** Tip Information$sContent="altogether there are:"+$totalCount. ToString () +"server needs to be processed! "Write-host$sContent-Foregroundcolor Green#*************** number of successful server stations[INT]$successCount= 0;#*************** number of failed server tables[INT]$failCount= 0;ForEach($computerObject inch $computerObjects) {try {#****************** If the ping is to pass if(test-connection$computerObject-count 1-ea 0-Quiet) { #*********************ping Pass Information printing $pingOK="Ping Pass"+$computerObject. ToString () Write-host$pingOK-Foregroundcolor Green#********************* writing to a ping pass fileAdd-content-path$pingFilePath-value$computerObject #********************* counter +1 $successCount=$successCount+ 1 } #******************* If the ping does not pass Else { #*********************ping Non-pass information printing $pingNO="Ping does not pass"+$computerObject. ToString () Write-host$pingNO-Foregroundcolor Red#********************* Write ping non-pass fileAdd-content-path$nopingFilePath-value$computerObject #********************* counter +1 $failCount=$failCount+ 1}} catch {#********************* Error occurred $ERRMSG="Ping"+$computerObject. ToString () +"An error occurred during the process"Write-host$ERRMSG-Foregroundcolor Blue#********************* Write ping non-pass fileAdd-content-path$nopingFilePath-value$computerObject #********************* counter +1 $failCount=$failCount+ 1 }}#************* Execution Complete$stopWatch. Stop ()#**************** Calculate how much time it takes$totalseconds=$stopWatch. Elapsed.totalseconds#********************** How much time it takes to print out$tooltip="after processing, it costs"+$totalseconds. ToString () +"seconds"Write-host$tooltip-foregroundcolor Red
PowerShell Ping the TXT file server