We help IT Professionals succeed at work.
Get Started
Research Question

Modify Powershell script to exclude  IIS application pool "names" restart.

michalek19
michalek19 asked
on
39 Views
Last Modified: 2021-03-04
I have poweshell script that monitors IIS Application Pool nonstop against remote servers. If the application pool is stopped, alert is triggered and message is send about application pool status down and force restarted.

What I would like to ask you, if you can help me to modify  powershell script to either add function to exclude some IIS applications pool "names" from restarting or add function that can import from txt or csv file list of application pools that have to be excluded from monitoring and restarting.

###################################Declear Servers in text file##############################################

$Servers = Get-Content E:\Script_utilities\IIS_Application_Pool_status\server.txt

################ Scans each server and import IIS web-administration module##################################

$SMPTServer = "mailrelay.ns.net"
$result = "The following application pools were restarted:`n`n" # default output

$Servers | ForEach-Object {
    $result += Invoke-Command -ComputerName $_ -ScriptBlock { # add output of scriptblock to $result
        Import-Module WebAdministration
        cd IIS:/AppPools
        $CompName = (Get-WmiObject -Class Win32_ComputerSystem).Name
        $ApplicationPools = dir
        foreach ($item in $ApplicationPools)
        {
            $ApplicationPoolName = $item.Name
            $ApplicationPoolStatus = Get-WebAppPoolState $ApplicationPoolName
            If($ApplicationPoolStatus.value -eq "Stopped")
                {    
                    Write-Output "Server $CompName - Application pool $ApplicationPoolName is Down - Restarting`n" # any action is added to otput
                    Start-WebAppPool -Name $ApplicationPoolName
                }
        }

    }
}
if ($result -ne "The following application pools were restarted:`n`n") { # If any action was taken send mail with $result
    send-mailmessage -to "micrex@rmns.com" -from "AppPoolMon@rmns.com" -subject "Application Pool Maitenance" -Body $result -SmtpServer $SMPTServer
}
##################################### End of Script ##########################################################AppPoolmon.txt
Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This problem has been solved!
Unlock 1 Answer and 17 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE