Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 671
  • Last Modified:

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
0
usslindstrom
Asked:
usslindstrom
  • 2
1 Solution
 
soostibiCommented:
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

0
 
soostibiCommented:
Sorry, that was the original, this is mine:
#Add the Quest ActiveDirectory ToolSet to Standard PowerShell  
Add-PSSnapin Quest.ActiveRoles.ADManagement  
  
#Set BasicPowerShell UI  
$PowerShellUI = (Get-Host).UI.RawUI  
Clear-Host
  
#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"  

$saveY = [console]::CursorTop  
#Create Infinite Loop for Echo Requests  
For(;;)  
  
{ 
[console]::setcursorposition(0,$saveY) 
  
#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

0
 
usslindstromAuthor Commented:
That was EXACTLY what I was looking for!

Much appreciated on the assistance!
0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now