Link to home
Start Free TrialLog in
Avatar of usslindstrom
usslindstromFlag for Japan

asked on

PowerShell Looping Script / Convert to better looking UI.

Experts,

I have a working Powershell script (I pasted it in the codeblock below) - that I use to get a quick overview on a site when they call and say they have servers that aren't responding.

The script isn't anything special, as you can see...  But I'd like to take it to the next level if it's at all possible.

Right now, the script queries AD for a list of units, then has a variable that I added to it that hits all the servers that aren't in AD for that particular site.  It tries to ping the unit, reads the results (or lack thereof) and just returns the unit's name in either red or green.  Simple, right?

What I'd like to do, is instead of the way it behaves now, which is to just cycle over the list of computers again and again...  Is keep a static list of computers in green on the screen, and as it cycles through the list just change the color of the unit it can/can't reach - each loop through.  Sort-of creating a "real-time" monitor on the servers.

Is something like that possible using Powershell?  Please keep in mind that I"m not a programming guy at all - so keep it as simple as possible.  :)
#Add the Quest ActiveDirectory ToolSet to Standard PowerShell
Add-PSSnapin Quest.ActiveRoles.ADManagement

#Set BasicPowerShell UI
$PowerShellUI = (Get-Host).UI.RawUI

#Define the Primary Function
Function SiteHealth()
{

#Display Title and Prompt User for Input
$PowerShellUI.ForegroundColor = "White"

  clear host;
  Write-Host "****************************************"
  Write-Host "*** xxxxxxxxx Site Monitoring Script ***"
  Write-Host "****************************************"
  Write-Host " "
  $PowerShellUI.ForegroundColor = "Green"
  Write-Host "Units Will Reply Green."
  $PowerShellUI.ForegroundColor = "Red"
  Write-Host "Non-Responsive Units will be in Red."
  $PowerShellUI.ForegroundColor = "White"
  Write-Host " "
  $Site = Read-Host "Please enter the 4 digit site code"
  Write-Host " "

#Define the Variables
$ServerContainer = "SERVERS"
$DCContainer = "Domain Controllers"
$OUName = $Site
$OUName = $OUName.ToUpper()
$DomainName = "xxxxxxxxx.xxx"
$NumOfPings = 1
$ServersNotInAD = @("$OUName-xxxx","$OUName-xxxx","$OUName-xxxx","$OUName-xxxx","$OUName-xxxx","$OUName-xxxx","$OUName-xxxx","$OUName-xxxx")

#Set Internet Explorer Options
$ie = New-Object -ComObject InternetExplorer.Application
$ie.Navigate2("$OUName-xxxx")
$ie.Navigate2("$OUName-xxxx",0x1000)
$ie.Navigate2("$OUName-xxxx",0x1000)
$ie.Navigate2("$OUName-xxxx",0x1000)

#Open Internet Explorer
$ie.Visible = $True

#Set to Off-Color for Status Message and Display Message
$PowerShellUI.ForegroundColor = "Magenta"

Echo "Connecting to $DomainName/$OUName/$ServerContainer & $DCContainer"

  Write-Host " "

#Return Color to Standard
$PowerShellUI.ForegroundColor = "Green"

#Create Infinite Loop for Echo Requests
For(;;)

{

#Query Active Directory under the OU Specified for all Units in the "SERVERS" OU and Ping Them
ForEach ($TargetComputer in (Get-QADComputer -SearchRoot $DomainName/$OUName/$ServerContainer)) {
	If (Test-Connection -ComputerName $TargetComputer.name -Count $NumOfPings -Quiet) {
	    $PowerShellUI.ForegroundColor = "Green"
		echo $TargetComputer.name }
	Else {
	    $PowerShellUI.ForegroundColor = "Red"
		echo $TargetComputer.name }
	$PowerShellUI.ForegroundColor = "Green"
	}

#Query Active Directory under the $DCContainer OU for all Units that Share the User's Input And Ping Them
ForEach ($TargetComputer in (Get-QADComputer -SearchRoot $DomainName/$DCContainer -LDapFilter "(Name=*$OUName*)")) {
    If (Test-Connection -ComputerName $TargetComputer.name -Count $NumofPings -Quiet) {
	    $PowerShellUI.ForegroundColor = "Green"
		echo $TargetComputer.name }
	Else {
	    $PowerShellUI.ForegroundColor = "Red"
		echo $TargetComputer.name }
	$PowerShellUI.ForegroundColor = "Green"
    }

#Ping all units that aren't in AD ($ServersNotInAD)
ForEach ($ComputerName in $ServersNotInAD) {
    If (Test-Connection -ComputerName $ComputerName -Count $NumofPings -Quiet) {
	    $PowerShellUI.ForegroundColor = "Green"
		echo $ComputerName }
	Else {
	    $PowerShellUI.ForegroundColor = "Red"
		echo $ComputerName }
	$PowerShellUI.ForegroundColor = "Green"
    }

$PowerShellUI.ForegroundColor = "White"

  Write-Host " "
  Write-Host "Loop Completed.  Returning to Top..."
  Write-Host " "

$PowerShellUI.ForegroundColor = "Green"

}

}

Open in new window

SiteHealth-Prompt.png
SiteHealth-Cycle.png
Avatar of soostibi
soostibi
Flag of Hungary image

My solution just outputs the results onto the same window position, so it is seemed to be static util something changes.
I also inserted some waiting between cycles (5 seconds) and a current date-time indication, so you can see that it really progresses.
#Add the Quest ActiveDirectory ToolSet to Standard PowerShell  
Add-PSSnapin Quest.ActiveRoles.ADManagement  
  
#Set BasicPowerShell UI  
$PowerShellUI = (Get-Host).UI.RawUI  

  
#Define the Primary Function  
Function SiteHealth()  
{  
  
#Display Title and Prompt User for Input  
$PowerShellUI.ForegroundColor = "White"  
  
  clear host;  
  Write-Host "****************************************"  
  Write-Host "*** xxxxxxxxx Site Monitoring Script ***"  
  Write-Host "****************************************"  
  Write-Host " "  
  $PowerShellUI.ForegroundColor = "Green"  
  Write-Host "Units Will Reply Green."  
  $PowerShellUI.ForegroundColor = "Red"  
  Write-Host "Non-Responsive Units will be in Red."  
  $PowerShellUI.ForegroundColor = "White"  
  Write-Host " "  
  $Site = Read-Host "Please enter the 4 digit site code"  
  Write-Host " "  
  
#Define the Variables  
$ServerContainer = "SERVERS"  
$DCContainer = "Domain Controllers"  
$OUName = $Site  
$OUName = $OUName.ToUpper()  
$DomainName = "xxxxxxxxx.xxx"  
$NumOfPings = 1  
$ServersNotInAD = @("$OUName-xxxx","$OUName-xxxx","$OUName-xxxx","$OUName-xxxx","$OUName-xxxx","$OUName-xxxx","$OUName-xxxx","$OUName-xxxx")  
  
#Set Internet Explorer Options  
$ie = New-Object -ComObject InternetExplorer.Application  
$ie.Navigate2("$OUName-xxxx")  
$ie.Navigate2("$OUName-xxxx",0x1000)  
$ie.Navigate2("$OUName-xxxx",0x1000)  
$ie.Navigate2("$OUName-xxxx",0x1000)  
  
#Open Internet Explorer  
$ie.Visible = $True  
  
#Set to Off-Color for Status Message and Display Message  
$PowerShellUI.ForegroundColor = "Magenta"  
  
Echo "Connecting to $DomainName/$OUName/$ServerContainer & $DCContainer"  
  
  Write-Host " "  
  
#Return Color to Standard  
$PowerShellUI.ForegroundColor = "Green"  
  

#Create Infinite Loop for Echo Requests  
For(;;)  
  
{  

  
#Query Active Directory under the OU Specified for all Units in the "SERVERS" OU and Ping Them  
ForEach ($TargetComputer in (Get-QADComputer -SearchRoot $DomainName/$OUName/$ServerContainer)) {  
        If (Test-Connection -ComputerName $TargetComputer.name -Count $NumOfPings -Quiet) {  
            $PowerShellUI.ForegroundColor = "Green"  
                echo $TargetComputer.name }  
        Else {  
            $PowerShellUI.ForegroundColor = "Red"  
                echo $TargetComputer.name }  
        $PowerShellUI.ForegroundColor = "Green"  
        }  
  
#Query Active Directory under the $DCContainer OU for all Units that Share the User's Input And Ping Them  
ForEach ($TargetComputer in (Get-QADComputer -SearchRoot $DomainName/$DCContainer -LDapFilter "(Name=*$OUName*)")) {  
    If (Test-Connection -ComputerName $TargetComputer.name -Count $NumofPings -Quiet) {  
            $PowerShellUI.ForegroundColor = "Green"  
                echo $TargetComputer.name }  
        Else {  
            $PowerShellUI.ForegroundColor = "Red"  
                echo $TargetComputer.name }  
        $PowerShellUI.ForegroundColor = "Green"  
    }  
  
#Ping all units that aren't in AD ($ServersNotInAD)  
ForEach ($ComputerName in $ServersNotInAD) {  
    If (Test-Connection -ComputerName $ComputerName -Count $NumofPings -Quiet) {  
            $PowerShellUI.ForegroundColor = "Green"  
                echo $ComputerName }  
        Else {  
            $PowerShellUI.ForegroundColor = "Red"  
                echo $ComputerName }  
        $PowerShellUI.ForegroundColor = "Green"  
    }  
  
$PowerShellUI.ForegroundColor = "White"  
  
  Write-Host " "  
  Write-Host "Loop Completed.  $(get-date) Returning to Top..."  
  Write-Host " "  
Start-Sleep 5  
$PowerShellUI.ForegroundColor = "Green"  
  
}  
  
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of soostibi
soostibi
Flag of Hungary image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of usslindstrom

ASKER

That was EXACTLY what I was looking for!

Much appreciated on the assistance!