Actions

Powershell: Ping Many Servers: Difference between revisions

From Rabbi Blog

(Created page with "Category:Powershell There are probably better ways to do this, but when I'm working on multiple servers I like to open a CMD and ping <host> so I have an eye on it when i...")
 
Line 5: Line 5:
This one is messy, it's been a long night\early morning, but it functionally meets the goal.
This one is messy, it's been a long night\early morning, but it functionally meets the goal.


=psping.ps1=
==psping.ps1==
<pre>
<pre>
$name=""
$name=""

Revision as of 12:59, 25 January 2020


There are probably better ways to do this, but when I'm working on multiple servers I like to open a CMD and ping <host> so I have an eye on it when it comes back up. Many servers... many cmd... messy.

Instead, let's make a PowerShell script that takes in server name input from the user and spawns a PS window that is small and checking the server for up\down via Test-Connection.

This one is messy, it's been a long night\early morning, but it functionally meets the goal.

psping.ps1

$name=""

while ($name -ne 0)
{
$name = Read-Host 'Enter Computer Name or IP:'
cmd /c start powershell -noexit -command "c:\powershell\ping\step2.ps1 -server $name"
}

step2.ps1

param (
        [Parameter(Mandatory=$true)][string]$server
     )

function Set-ConsoleWindow
{
    param(
        [int]$Width,
        [int]$Height
    )

    $WindowSize = $Host.UI.RawUI.WindowSize
    $WindowSize.Width  = [Math]::Min($Width, $Host.UI.RawUI.BufferSize.Width)
    $WindowSize.Height = $Height

    try{
        $Host.UI.RawUI.WindowSize = $WindowSize
    }
    catch [System.Management.Automation.SetValueInvocationException] {
        $Maxvalue = ($_.Exception.Message |Select-String "\d+").Matches[0].Value
        $WindowSize.Height = $Maxvalue
        $Host.UI.RawUI.WindowSize = $WindowSize
    }
}

Set-ConsoleWindow 35 2

$Host.UI.RawUI.WindowTitle = "Server: $server | Checking..."

$SERVERWINDOW=$server

write-host "Checking $SERVERWINDOW"

$FOREVER=666

while ($FOREVER -eq 666)
	{
	If (Test-Connection $server -count 1 -delay 4 -quiet) 
		{
		Write-Host '***' $SERVERWINDOW 'responded' -ForegroundColor Black
		$Host.UI.RawUI.BackgroundColor = ($bckgrnd = 'Green')
		$Host.UI.RawUI.WindowTitle = "UP: "+ $SERVERWINDOW 
		cls
		}
	else
		{
		Write-Host '!!!' $SERVERWINDOW 'no response' -ForegroundColor Black
		$Host.UI.RawUI.WindowTitle = "DN: "+ $SERVERWINDOW 
		$Host.UI.RawUI.BackgroundColor = ($bckgrnd = 'Yellow')
		cls
		}
	}