Search code examples
iispowershellappcmd

Can I use PowerShell instead of AppCmd.exe to monitor IIS state?


I want to write a script that uses appcmd.exe to monitor IIS state - restart a site if it's down and/or restart IIS if it's down.

Can this be done with powershell? is there a more natural/easier method of doing this?

Thanks :-)


Solution

  • Windows PowerShell is always the Answer! This should do it for your particular question:

    # Cycle IIS if it's not running    
    Get-Service W3SVC  |
        Where-Object {$_.Status -ne 'Running' }  | 
        Start-Service
    
    Import-Module WebAdministration
    
    # Cycle websites that are not started
    Get-Website | 
        Where-Object { $_.State -ne 'Started' }  | 
        ForEach-Object { $_.Start() } 
    

    Hope this Helps