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...")
 
mNo edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Category:Powershell]]
[[Category:Powershell]]
[[Category:Weblog-2020-01]]
__toc__


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.<br><br>
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.<br><br>
Line 5: Line 8:
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==
 
* maybe someday just check the local dir vs hard-coding it?  Did you even really need that?  Tired...
 
<pre>
<pre>
$name=""
$name=""
Line 16: Line 22:
</pre>
</pre>


=step2.ps1=
==step2.ps1==
* The title bar is the only really needed element here.  Making the window small and with a little color change is just a thing to do.
 
<pre>
<pre>
param (
param (

Latest revision as of 13:02, 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

  • maybe someday just check the local dir vs hard-coding it? Did you even really need that? Tired...
$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

  • The title bar is the only really needed element here. Making the window small and with a little color change is just a thing to do.
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
		}
	}