This script checks all the IIS servers which are specified in the input file, and get the application pool status. It then exports the result in CSV and HTML file format. Finally, the script sends the result in email, attaching both the files.

For best result, customize the script and incorporate it with Windows Task scheduler. The source server should be Windows Server 2012 R2.

 

1) Location of the input file: It should be a TXT file.

2) Location of the output folder : It creates date wise subfolder while exporting the output, under the main folder that you specify. Under the subfolder, it exports the output in CSV and HTML format.

3) Email Configuration.

 

 

 

PowerShell
Edit|Remove
 
$date=Get-Date 
 
$date1$date.ToString("dd-MM-yyyy") 
 
# Enter the location, where the reports will be stored. Datewise sub folders will be created under this location. 
 
$path"C:\input" 
 
$directoryNew-Item -ItemType directory -Path "$path\$date1" 
 
 
 
# This is the location of the input file, which contains server list. 
 
 
$servers = get-content -path "c:\input\input.txt" 
 
 
$Results= @() 
 
 
foreach ($server in $servers)  
{ 
       
            Import-Module WebAdministration 
             
            Set-Location IIS:\AppPools 
                               
            $appPoolCollections = dir 
 
            foreach ($apppool in $appPoolCollections) 
 
            { 
 
                $properties= @{  
 
                         ServerName= $server 
                         AppPoolName = $apppool.Name 
                         AppPoolState = $apppool.state 
                         AppPoolVersion= $apppool.managedRuntimeVersion 
 
                              } 
 
             $Results+New-Object psobject -Property $Properties | select-object ServerName,AppPoolName,AppPoolState,AppPoolVersion 
 
              
 
            } 
        }  
 
        $Results |ft * 
 
        $Results | select-object ServerName,AppPoolName,AppPoolState,AppPoolVersion | export-csv -Path "$directory\AppPoolDetails.csv" -NoTypeInformation 
 
        $Results | ConvertTo-Html | out-file $directory\AppPoolDetails.htm -Append 
 
        $File"$directory\AppPoolDetails.CSV" 
 
        $File1"$directory\AppPoolDetails.htm" 
 
$bodyText= 
@' 
 
Hi Team, 
 
Please find the Application Pool status of IIS Servers. 
 
This is an auto generated mail. Please do not reply. 
 
 
Regards, 
 
IT Team 
 
'@ 
 
 
        
#SMTP server name 
$smtpServer = "mail.yourdomain.com" 
 
#Creating a Mail object 
$msg = new-object Net.Mail.MailMessage 
 
#Creating SMTP server object 
$smtp = new-object Net.Mail.SmtpClient($smtpServer) 
 
#Email structure 
 
$msg.From = "ServerName@yourmaildomain.com" 
 
$msg.To.Add("DL@yourmaildomain.com") 
 
$msg.subject = "IIS Server Application Pool Status" 
 
$msg.body = $bodyText 
 
        
$att = new-object Net.Mail.Attachment($File) 
 
 
$att1 = new-object Net.Mail.Attachment($File1) 
 
#Attaching the CSV file 
 
$msg.Attachments.Add($att)  
 
#Attaching the HTML file 
 
$msg.Attachments.Add($att1)   
   
#Sending email 
 
$smtp.Send($msg)