Ping multiple hostnames in single PowerShell script.

Guys, In a day-to-day activity, we have to perform repetitive tasks like check the availability of the server by using ping utility and send the report to management. while doing this activity manually, this is an irritative task, while using automation, it's pretty easy.

Let's create a new PowerShell script and insert the below code. once you save the ps1 file.

create another file with extension .txt and put destination servers which do you want to ping and save that text file with the name "inputcomputers.txt" at the location where you save the ps1 file.

You can run the PowerShell with Task Scheduler or Simply run PS1 to get the ping report in "outputcomputers.log" 😀👍

<#

    .SYNOPSIS

    Ping multiple hostnames and get output in CSV format.

    .DESCRIPTION

    This Powershell Script will be useful for those who want to ping the multiple server/workstations and get the report on the central location.

    .NOTES

    =============================================================================================================================================

    Created with:     Windows PowerShell ISE

    Created on:       Tuesday, August 17, 2021 2:05:44 PM

    Created by:       Dashrath Chate

    Organization:     Blogger.com

    =============================================================================================================================================

#>

$names = Get-Content "$PSScriptRoot\inputcomputers.txt"

$logfile = "$PSScriptRoot\outputcomputers.log"

foreach ($name in $names) 

{

if ( Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue ) 

{

Write-Host "$name is up" -ForegroundColor Magenta | Export-Csv $logfile

}

else 

{

Write-Host "$name is down" -ForegroundColor Red | Export-Csv $logfile

}

}

No comments:

Post a Comment

Leave your valuable words here for improve better.