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