<#
Ester Niclos Ferreras

Checks IIS Site state

OK - started
CRITICAL - STOPPED
UNKNOWN - not found


#>


#
# Shell arguments
#
[CmdletBinding()]
Param(
    [Parameter(Mandatory=$True,Position=1)]
    [string]$website
)

$bReturnOK       = $TRUE
$bReturnCritical = $FALSE
$bReturnWarning  = $FALSE

$returnStateOK       = 0
$returnStateWarning  = 1
$returnStateCritical = 2
$returnStateUnknown  = 3

$DataTexte   = ""
$strCritical = ""
$strWarning  = ""

[System.Reflection.Assembly]::LoadFrom( "C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll" )  >  $null

$servermanager = [Microsoft.Web.Administration.ServerManager]::OpenRemote("localhost")
$site          = $servermanager.Sites["$website"]
$iis           = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\InetStp\  | select setupstring

# Nagios output

$resultstring="IISSITE UNKNOWN  $website not found"
$exit_code = $UNKNOWN
  
If ($site -ne $null) {
      
  $status= $site.State
  
  If ($status -eq "Started") {
    $str = 'OK ' + $website + ' ' + $status + '-' + $iis.setupstring
  }
  Elseif ($status -eq $null) {
	$strWarning = "UNKNOWN  $website exists, but has no state. Check it is not a FTP site."
    $bReturnWarning = $TRUE
  }
  Else {	
	$strCritical = 'CRITICAL '+ $website + ' ' + $status + '-' + $iis.setupstring
	$bReturnCritical = $TRUE
  }
}

If ($bReturnCritical) {
    write-output $strCritica "|" $DataTexte
    exit $returnStateCritical
}
Elseif ($bReturnWarning) {
    write-output $strWarning "|" $DataTexte
    exit $returnStateWarning
}
Else {
    write-output $str "|" $DataTexte
    exit $returnStateOK
}
