Friday, September 5, 2014

VB script to start stopped app pools

This VB script will start the stopped app pool and send email to desired email address, also it will exclude the undesired apppool. It will send email only and after if a apppool was stopped and started by this script:
=========================================================================


Set oWebAdmin = GetObject("winmgmts:root\WebAdministration")
Set oAppPools = oWebAdmin.InstancesOf("ApplicationPool")
'Search in all App Pools
For Each oAppPool In oAppPools
 IF (oAppPool.name <> "Devtesting" ) Then
   IF oAppPool.GetState= 1 Then
 'wscript.echo "App Pool Started"
 
 ElseIF  oAppPool.GetState= 3 Then
   'wscript.echo "App Pool Stopped"
   ' Following command Starts App Pool
    oAppPool.Start
   temp = temp & ", " & oAppPool.Name
    END IF
END IF
Next
 IF (temp <> "") Then
  Set objMessage = CreateObject("CDO.Message")
  objMessage.Subject = "Monitoring APP POOLS on SP Farm"
  objMessage.From = "santosh.sethi@gmail.com"
  objMessage.To = "santosh.sethi@gmail.com"
   objMessage.TextBody = "Monitorinig Message, App Pool: "   & temp & " was down and automatically started."

  '==This section provides the configuration information for the remote SMTP server.
  '==Normally you will only change the server name or IP.
  objMessage.Configuration.Fields.Item _
  ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  'Name or IP of Remote SMTP Server
  objMessage.Configuration.Fields.Item _
  ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "SMTP server name"
  'Server port (typically 25)
  objMessage.Configuration.Fields.Item _
  ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  objMessage.Configuration.Fields.Update
  '==End remote SMTP server configuration section==
  objMessage.Send
 END IF
set oWebAdmin = nothing
Set oAppPools = nothing
Set objMessage = nothing


**Reference link : http://msdn.microsoft.com/en-us/library/ms691445(v=vs.90).aspx

Thursday, September 4, 2014

Powershell to Start the stopped Appools at a server

#The following powershell script will start the stopped apppools, the excluded a appools can be included in place of "Devtesting". Also this script will be in sleep mode in every 300 sec
===================================================================


Import-Module WebAdministration
$logPath = "D:\AppPool\logs\iis-recycle-" + (Get-Date).ToShortDateString() -replace "/","-"
$logPath += ".txt"
#Specify the time counter over here
$TimeCounter = 300
#Run in Infinite Loop
While ($true)
{
$Date = Get-Date
Write-Output $date | Out-File -FilePath $logPath -Append
$AppPools = Get-WmiObject -Class IIsApplicationPool -Namespace "root/MicrosoftIISv2" | Select-Object -property @{N="Name";E={$name = $_.Name; $name.Split("/")[2] }}
$ApppoolsEXclude = $AppPools | where-object {$_.Name -ne "Devtesting"}
foreach($Pool in $AppPoolsExclude)
{
$PoolName = $pool.Name
        $State = Get-WebAppPoolState -Name $PoolName
        $StateValue = $State.Value
        if($StateValue -eq "Stopped")
        {
            Write-Output "Found $PoolName as $StateValue. Proceeding to start..." | Out-File -FilePath $logPath -Append
            Start-WebAppPool -Name $PoolName
            if(-not $?)
            {
                Write-Output "Encountered an error. Pls check manually." | Out-File -FilePath $logPath -Append
            }
            else
            {
                Write-Output "Started" | Out-File -FilePath $logPath -Append
            }
        }
        else
        {
        }  
    }
     
   Write-Output "Proceeding to sleep for $TimeCounter seconds." | Out-File -FilePath $logPath -Append
   Sleep $TimeCounter
}