Table des matières

check_file.vbs

' Check files under a given path
' ==============================
'
' based on example scripts found in nsclient++/scripts directory
'
' Author: werner.fuerst@assmann.at  2010-12-21
'
' uses NagiosPlugin.vbs  from nsclient++/scripts/lib
'       - NagiosPlugin.vbs which came with nsclient++ vers. 0.3.9 (earlier version needed adaptation)
'            - compares bounds to double values, so 10 comes after 9 (numeric sorting)
'            - bounds definition conforms to nagios plugin guidelines (http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT)
'
' modifications in NSC.ini:
' =========================
'
'
' in [modules]:
' CheckExternalScripts.dll
'
' in [NRPE]:
' allow_arguments=1
' allow_nasty_meta_chars=1
' allowed_hosts=x.x.x.x
'
' in [External Script]:
' allow_arguments=1
' allow_nasty_meta_chars=1
'
' in [Script Wrappings]:
' obsolet, as modifications in wrapperCDbl.vbs are included since version 0.3.9: vbs=cscript.exe //T:30 //NoLogo scripts\lib\wrapperCDbl.vbs %SCRIPT% %ARGS%
'
' in [Wrapped Scripts]:
' check_files=check_files.vbs $ARG1$ $ARG2$ $ARG3$ $ARG4$ $ARG5$ $ARG6$ $ARG7$ $ARG8$ $ARG9$ $ARG10$ $ARG11$ $ARG12$ $ARG13$ $ARG14$
'
'
' nagios usage:
' =============
'
' define command{
'        command_name check_nrpe_external
'        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -a $ARG2$
' }
'
' define service{
'        use                     generic-service
'        host_name               windowsxx
'        service_description     Backup DB2
'        check_command           check_nrpe_external!check_files!/path:"e:\\BACKUP\\DB" /namefilter:"DB2\.DAT" /expectmatch:1 /age:3 /selage:hour /warning:1: /critical:1: /size:9000000000 /weekdays:"2,3"
' }
' give alarm when file DB2.DAT in e:\BACKUP\DB was not written until 3 o clock on Monday or Tuesday (alarm if count is less than 1, so threshold is set to 1:)
'
'
'
'        check_command           check_nrpe_external!check_files!/path:"d:\\journal" /searchdepth:1 /selage:ignore /warning:30 /critical:40
' give alarm if there are more than 30 files under d:\journal
'
'
'        check_command           check_nrpe_external!check_files!/path:"T:\\nfs\\interface1"  /searchdepth:2 /age:2d /warning:5: /critical:3: /selage:newer /seldat:created
' give alarm if there are fewer than 3 files which are newer than 2 days under t:\nfs\interface1, search only 1 subdir down, use the creation date for comparison
'
'
'
' args:
' =====
'
'
' warning: threshold warning 
'    alert if x...
'             10	         < 0 or > 10, (outside the range of {0 .. 10})
'             10:	         < 10, (outside {10 .. 8})
'             ~:10	       > 10, (outside the range of {-8 .. 10})
'             10:20	       < 10 or > 20, (outside the range of {10 .. 20})
'             @10:20	     >= 10 and <= 20, (inside the range of {10 .. 20})
' critical: threshold critical 
' namefilter: regular expressionon on which files have to match
' age: files have to be older/newer (see selage) than age
'    e.g.: 5d: 5 days, 4h: 4 hours, 10n: 10 Minutes, 90s: 90 seconds   
' selage: older/newer/hour/ignore
'    older/newer: count only files if the are older/newer
'    hour: alert if file is not written until hour
'    ignore: count files independent of their age 
' searchdepth: search down x subdirs (searchdepth:1 - do not go down in directory hierarchy)
' seldat: modified/created
'    modified: date, when file was written
'    created: date, when file was created
' size: if file is smaller than given size give a warning
' expectmatch: if less than expectmatch files correspond to namefilter give a warning
' weekdays: 
'    if selage:hour files have to be written only on weekdays (1:sunday, 2:monday, ...)
'    if selage:newer or selage:older and the timeunit of age is d (days), we add as many days to age as the last weekday is back
'    e.g.: weekdays:23456 files are written on Monday, Tuesday, Wednesday, Thursday, Friday
'

Const PROGNAME = "check_files"
Const VERSION = "0.1.3"
' ver 0.1.3 2011-10-06
'    Comment in header
' ver 0.1.2 2011-06-15
'    start with 1 and not with 0 when trying to find the last day when the file should have been written according to weekdays
'    fixed some typos in the coments
' ver 0.1.1 2011-01-07
'    replaced Testing... with Checking...

Dim verbose
verbose = 0

Dim lastfile
Dim lastdat
Dim lastsize
Dim intdif
Dim warnsize
Dim matchcount
Dim weekdays
Dim ageint
Dim ageunit
Dim agestring
Dim inlevel


' Default settings for script.
threshold_warning = 10
threshold_critical = 20
alias = "default"
agestring = "5d"    '5 days
ageint = 5
ageunit = "d"
selage = "newer"
namefilter = ""
searchdepth = 0
seldat = "modified"
size = 0
expectmatch = 0
weekdays = "1,2,3,4,5,6,7"

' Create the NagiosPlugin object
Set np = New NagiosPlugin

' Define what args should be used
np.add_arg "path", "Path", 1
np.add_arg "namefilter", "Filename Filter", 0
np.add_arg "age", "Age", 0
np.add_arg "selage", "newer, older, hour, ignore", 0
np.add_arg "searchdepth", "depth of subdirs to search", 0
np.add_arg "seldat", "modified or created", 0
np.add_arg "size", "size", 0
np.add_arg "warning", "warning threshold", 0
np.add_arg "critical", "critical threshold", 0
np.add_arg "expectmatch", "expect at least x matches", 0
np.add_arg "weekdays", "1,2,3,... 1-Sun 2-Mon 3-Tue...", 0
np.add_arg "alias", "Alias", 0

' If we have no args or arglist contains /help or not all of the required arguments are fulfilled show the usage output,.
If Args.Count < 1 Or Args.Exists("help") Or np.parse_args = 0 Then
	WScript.Echo Args.Count
	np.Usage
End If

' If we define /warning /critical on commandline it should override the script default.
If Args.Exists("warning") Then threshold_warning = Args("warning")
If Args.Exists("critical") Then threshold_critical = Args("critical")
If Args.Exists("namefilter") Then namefilter = Args("namefilter")
If Args.Exists("age") Then agestring = Args("age")
If Args.Exists("selage") Then selage = Args("selage")
If Args.Exists("searchdepth") Then searchdepth = Cint(Args("searchdepth"))
If Args.Exists("seldat") Then seldat = Args("seldat")
If Args.Exists("size") Then size = CDbl(Args("size"))
If Args.Exists("expectmatch") Then expectmatch = CInt(Args("expectmatch"))
If Args.Exists("weekdays") Then weekdays = Args("weekdays")
If Args.Exists("alias") Then alias = Args("alias")

' Set the msg output to be used (OK/WARNING/CRITICAL/UNKNOWN will be applied automaticly)
np.set_thresholds threshold_warning, threshold_critical



' Set ageint and ageunit
Set reage = New RegExp
reage.IgnoreCase = False
reage.Pattern = "^([0-9]+)([dhns]*)$"     'd: days h:hours n:minutes s:seconds

Set ages = reage.Execute(agestring)

For Each age In ages
  pt age & " > " & age.SubMatches(0) & " > " & age.SubMatches(1)
  ageint = CInt(age.SubMatches(0))
  ageunit = age.SubMatches(1)
  If ageunit = "" Then
    If selage = "hour" Then
      ageunit = "h"
    Else
      ageunit = "d"
    End If
  End If
Next

' add some days if this and the days before do not belong to the weekdays
If ageunit="d" And (selage="newer" Or selage="older") Then
  date1 = Now()
  date00 = DateValue(Now)

' start with 1 and not with 0 when trying to find the last day when the file should have been written according to weekdays
'    if a machine is saved only on mondays, and we start with 0, 0 is added to age, and we get an alarm every monday mornung until the copy is started
'    if we start with 1, and it is monday, we add 7 to age
'    drawback: we add always at least 1 to age (we could count how many weekdays are defined, ...)
  For i = 1 To 7
    ' datex: last day when the file should have been written according to weekdays
    datex = SubtractDate(date00,i)
    wdnowx = WeekDay(datex)
    If Instr(weekdays,wdnowx) Then
      ' exit for if we found the youngest weekday
      Exit For
    End If
  Next 

  datex0 = DateValue(datex)
  diffd = DateDiff("d",datex0,date00)
  ageint = ageint + diffd
End If



intdif=0
matchcount=0
warnsize = 0

' go down the hierarchy, in intdif we get the number of corresponding files
CheckSubdirs Args("path")

' get return code according to intdif files
return_code = np.check_threshold(CDbl(intdif))


last = ""
If Len(namefilter) Then
  last = "filter: " & namefilter
End If

' if there was only one file...
If matchcount = 1 Then
  last = last & " found one: " & lastfile & " " & lastdat & " " & lastsize
End If

' warning message if file too small or too few of them
message = ""
If warnsize = matchcount And warnsize > 0 Then
    If return_code = 0 Then
      return_code = 1
    End If
    message = " TOO SMALL!!! "
End If

If matchcount < expectmatch Then
    If return_code = 0 Then
      return_code = 1
    End If
    message = " NOT FOUND!!! "
End If

If selage = "ignore" Then
  agemessage = " count: " 
ElseIf selage = "newer" Or selage ="older" Then
  agemessage = selage & " than " & ageint & " " & ageunit & " : "
ElseIf selage = "hour" Then
  agemessage = " written before " & ageint & " o-clock on " & weekdays & " : "
Else
  agemessage = selage & ": " & age & " " & ageunit & " "
End If



' for the performance data format warning and critical thresholds (no :@~)
cintw=np.get_threshold_perfdat("warning")          'PerfDat(np.get_threshold("warning"))
cintc=np.get_threshold_perfdat("critical")


' if we watch only a single file we will see the size in the performance data
If expectmatch = 1 and matchcount = 1 Then
  perfdata = "size=" &  lastsize
Else
  perfdata = "count=" & Cint(intdif) & ";" & cintw & ";" & cintc
End If

msg = "Checking " & Replace(Args("path"),"\","/") & " " & last & " " & agemessage & intdif & " w:" & np.get_threshold("warning") & " c: " & np.get_threshold("critical") & " " & message & " |" & perfdata

' Nice Exit with msg and exitcode
np.nagios_exit msg, return_code



Sub CheckSubdirs(StartDir)

    Set FSO = CreateObject("Scripting.FileSystemObject")

    inlevel = 0

    ' go down hierarchy
    ShowSubfolders FSO.GetFolder(StartDir)

    'Clean up
    Set FSO = Nothing
End Sub





Sub ShowSubFolders(Folder)
    'inlevel: we start with 1, so the startdir has level 1!
    inlevel = inlevel + 1
    pt "level: " & inlevel & " Folder: " & Folder.Name & " searchdepth: " & searchdepth
    If (inlevel > searchdepth) And (searchdepth > 0) Then
        ' we are too deep!
        inlevel = inlevel - 1
        pt "level2: " & inlevel & " Folder: " & Folder.Name
        Exit sub
    End If
    pt "level3: " & inlevel & " Folder: " & Folder.Name
    ListFiles Folder
    If ((inlevel < searchdepth) And (searchdepth > 0)) Or (searchdepth = 0) Then
      For Each Subfolder in Folder.SubFolders
          ' go further down
          ShowSubFolders Subfolder
      Next
    End If
    ' leave level
    inlevel = inlevel - 1
End Sub



Sub ListFiles(Folder)

    Set colFiles = Folder.Files
    
    pt "in folder " & Folder.Name

    For Each File in colFiles

        matched = true
        'pt "namefilter: " &  namefilter & " len:" & Len(namefilter)
        ' if we defined a namefilter, use it (regexp!)
        If Len(namefilter) > 0 Then
        
            filename = File.Name
        
            'pt "namefilter1: " &  filename
            Set re = new regexp
            re.IgnoreCase = True
            re.Pattern = namefilter
            
            'pt "namefilter2: " &  namefilter & " len:" & Len(namefilter)
            If re.Test(filename) <> true Then
                matched = false
            End If
        End If
        
        ' now test the matched files against the age
        If matched = true Then

          ' count the matched files (we warn later if there are fewer the expectmatch files)
          matchcount = matchcount + 1
          pt "match: " & matchcount & " file: " & File.name

          ' Date2 can be the DateCreated (backup of old machine, which is not time synchronized, so the created date is the date when the file was backed up to our server)
  	      Date1 = Now()
	        If seldat = "created" Then
              Date2 = File.DateCreated
          Else
	            Date2 = File.DateLastModified
          End If 

          ' time difference between file and now
	        dif =  Cint(DateDiff(ageunit,Date2,Date1))

          'remember the last file checked (we use it later in the message)
          lastfile = File.name
          lastdat = Date2
          lastsize = File.Size

          ' remember the count of files which where too small
          If CDbl(File.Size) < CDbl(size) Then
              warnsize = warnsize + 1
          End If

          'pt "c0 dif: " &  dif & " age: " & age & " selage: " & selage & " seldat: " & seldat 
          ' count the numer of files which are newer/older than given age
          If dif < ageint And selage = "newer" Then
              intdif = intdif + 1
          ElseIf dif > ageint And selage = "older" Then
              intdif = intdif + 1
          ElseIf selage = "ignore" Then
              intdif = intdif + 1
          ElseIf selage = "hour" Then
          ' if selage:hour check if file has been written not after age (here age is the hour)
              If isnotolder(File) Then
                intdif = intdif + 1
              End If
          End If
        End If
    Next

End Sub



Function isnotolder(File)


date1 = Now()
date2 = File.DateLastModified 
date00 = DateValue(Now)
date10 = DateValue(Now)
date20 = DateValue(date2)


For i = 0 To 7
  ' datex: last day when the file should have been written according to weekdays
  datex = SubtractDate(date00,i)
  'date00 = DateValue(datex)
  wdnowx = WeekDay(datex)
  If Instr(weekdays,wdnowx) Then
    ' exit for if we found the youngest weekday
    Exit For
  End If
Next 


' we also need the second youngest weekday, when the file should have been written (is this english?)

date00 = datex

For i = 1 To 7
  datex2 = SubtractDate(date00,i)
  wdnowx2 = WeekDay(datex2)
  If Instr(weekdays,wdnowx2) Then
    Exit For
  End If
Next 


diffdx = DateDiff("d",datex,Now)     ' days between expected write date and today 
diffh0 = DateDiff("h",date10,Now)    ' hours since midnight
diffd = DateDiff("d",date20,datex)   ' days between written and expect to be written
diffd2 = DateDiff("d",date20,datex2) ' days between second oldest expected write day and today
diff1 = DateDiff("h",date20,date2)   ' hour of write (we currently do not use it)


' we expect the best
iswritten = 1


' today to be written an hour passed: should be written today!
If diffdx = 0 And diffh0 > ageint And diffd > 0 Then
  pt "if1: diffdx=0 and diffh0>0ageint and diffd>0: diffdx: " & diffdx & " diffh0: " & diffh0 & " diffd: " & diffd
  iswritten = 0
End If

' today to be written an hour not passed: should be written at least on second oldest expected day
If diffdx = 0 And diffh0 <= ageint And diffd2 > 0 Then
  pt "if2: diffdx=0 and diffh0<= ageint and diffd2>0: diffdx: " & diffdx & " diffh0: " & diffh0 & " diffd2: " & diffd2
  iswritten = 0
End If


' it should have been written on a day before, diffd gives a positive value: expected day passed
If diffdx > 0 And diffd > 0 Then
  pt "if3: diffdx>0 and diffd>0 diffdx: " & diffdx & " diffd: " & diffd
  iswritten = 0
End If


' older the 7 days!
If diffd > 7 Then
  pt "if4: diffd>7 diffd: " & diffd 
  iswritten = 0
End If


' if none of the bad conditions matched, we still have iswritten=1
isnotolder = iswritten

End Function



Function SubtractDate(datea,ii)
dateb = DateAdd("d",-ii,datea)
SubtractDate = dateb
End Function





Function pt(strMsg)
If verbose = 1 Then
  wscript.echo strMsg
End If
End Function

check_licences_windows

$VAR=cscript /Nologo "C:\Windows\System32\slmgr.vbs" /dlv | Select-String -Pattern "Expiration"
$OUT=($VAR -split " ")[5] / 1440
$RESULT=([math]::Round($OUT))

if ($RESULT -lt "1"){
	Write-Output "Critique: La licence a expirée $RESULT jours"
	exit 2
}
if ($RESULT -lt "15"){
	Write-Output "Critique: La licence expire dans $RESULT jours"
	exit 2
}
if ($RESULT -lt "30"){
	Write-Output "Warning: La licence expire dans $RESULT jours"
	exit 1
}

Write-Output "OK: La licence expire dans $RESULT jours"
exit 0

check_win_task

# Script name:  check_ms_win_tasks.ps1
# Version:      v7.06.191031
# Created on:   01/02/2014
# Author:       Willem D'Haese
# Purpose:      Checks Microsoft Windows enabled scheduled tasks excluding defined folders and task patterns, returning state of tasks
#               with name, author, exit code and performance data to Nagios.
# On Github:    https://github.com/willemdh/check_ms_win_tasks
# On OutsideIT: https://outsideit.net/check-ms-win-tasks
# Copyright:
#   This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published
#   by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed 
#   in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
#   PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public 
#   License along with this program.  If not, see <http://www.gnu.org/licenses/>.

#Requires -Version 2.0

$DebugPreference = 'SilentlyContinue'
$VerbosePreference = 'SilentlyContinue'

$Struct = New-Object -TypeName PSObject -Property @{
  Hostname = [string]'localhost'
  ExclFolders = [string[]]@()
  InclFolders = [string[]]@()
  ExclTasks = [string[]]@()
  InclTasks = [string[]]@()
  ExclAuthors = [string[]]@()
  InclAuthors = [string[]]@()
  FolderRef = [string]''
  AllValidFolders = [string[]]@()
  ExitCode = [int]3
  Hidden = [int]0
  TasksOk = [int]0
  TasksNotOk = [int]0
  TasksRunning = [int]0
  TasksTotal = [int]0
  TasksDisabled = [int]0
  BadTasks = [object[]]@()
  RunningTasks = [object[]]@()
  DisabledTasks = [object[]]@()
  AlertOnDisabled = [bool]$False
  FullPath = [bool]$False
  OutputString = [string]'Unknown: Error processing, no data returned.'
  WarningTreshold =  [int]0
  CriticalTreshold = [int]0
  LastExec = [bool]$false                    
}

#region Functions
Function Write-Log {
  Param (
    [parameter(Mandatory=$True,HelpMessage='Log output')][string]$Log,
    [parameter(Mandatory=$True,HelpMessage='Log severity')][ValidateSet('Debug', 'Info', 'Warning', 'Error', 'Unknown')][string]$Severity,
    [parameter(Mandatory=$True,HelpMessage='Log message')][string]$Message
  )
  $Now = Get-Date -Format 'yyyy-MM-dd HH:mm:ss,fff'
  $LocalScriptName = Split-Path -Path $myInvocation.ScriptName -Leaf
  If ( $Log -eq 'Verbose' ) {
    Write-Verbose -Message ('{0}: {1}: {2}: {3}' -f $Now, $LocalScriptName, $Severity, $Message)
  }
  ElseIf ( $Log -eq 'Debug' ) {
    Write-Debug -Message ('{0}: {1}: {2}: {3}' -f $Now, $LocalScriptName, $Severity, $Message)
  }
  ElseIf ( $Log -eq 'Output' ) {
    Write-Host ('{0}: {1}: {2}: {3}' -f $Now, $LocalScriptName, $Severity, $Message)
  }
  ElseIf ( $Log -match '^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])(?::(?<port>\d+))$' -or $Log -match '^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$' ) {
    $IpOrHost = $log.Split(':')[0]
    $Port = $log.Split(':')[1]
    If ( $IpOrHost -match '^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$' ) {
      $Ip = $IpOrHost
    }
    Else {
      $Ip = [Net.Dns]::GetHostAddresses($IpOrHost)[0].IPAddressToString
    }
    Try {
      $LocalHostname = ([Net.Dns]::GetHostByName((& "$env:windir\system32\hostname.exe")).HostName).tolower()
      $JsonObject = (New-Object -TypeName PSObject | 
        Add-Member -PassThru -NotePropertyName NoteProperty -NotePropertyValue logsource -InputObject $LocalHostname | 
        Add-Member -PassThru -NotePropertyName NoteProperty -NotePropertyValue hostname -InputObject $LocalHostname | 
        Add-Member -PassThru -NotePropertyName NoteProperty -NotePropertyValue scriptname -InputObject $LocalScriptName | 
        Add-Member -PassThru -NotePropertyName NoteProperty -NotePropertyValue logtime -InputObject $Now | 
        Add-Member -PassThru -NotePropertyName NoteProperty -NotePropertyValue severity_label -InputObject $Severity | 
        Add-Member -PassThru -NotePropertyName NoteProperty -NotePropertyValue message -InputObject $Message ) 
      If ( $psversiontable.psversion.major -ge 3 ) {
        $JsonString = $JsonObject | ConvertTo-Json
        $JsonString = $JsonString -replace "`n",' ' -replace "`r",' '
      }
      Else {
        $JsonString = $JsonObject | ConvertTo-Json2
      }               
      $Socket = New-Object -TypeName System.Net.Sockets.TCPClient -ArgumentList ($Ip,$Port) 
      $Stream = $Socket.GetStream() 
      $Writer = New-Object -TypeName System.IO.StreamWriter -ArgumentList ($Stream)
      $Writer.WriteLine($JsonString)
      $Writer.Flush()
      $Stream.Close()
      $Socket.Close()
    }
    Catch {
      Write-Host ("{0}: {1}: Error: Something went wrong while trying to send message to logserver `"{2}`"." -f $Now, $LocalScriptName, $Log)
    }
    Write-Verbose -Message ('{0}: {1}: {2}: Ip: {3} Port: {4} JsonString: {5}' -f $Now, $LocalScriptName, $Severity, $Ip, $Port, $JsonString)
  }
  ElseIf ($Log -match '^((([a-zA-Z]:)|(\\{2}\w+)|(\\{2}(?:(?:25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)(?(?=\.?\d)\.)){4}))(\\(\w[\w ]*))*)') {
    If (Test-Path -Path $Log -pathType container){
      Write-Host ('{0}: {1}: Error: Passed Path is a directory. Please provide a file.' -f $Now, $LocalScriptName)
      Exit 1
    }
    ElseIf (!(Test-Path -Path $Log)) {
      Try {
        $Null = New-Item -Path $Log -ItemType file -Force	
      } 
      Catch { 
        $Now = Get-Date -Format 'yyyy-MM-dd HH:mm:ss,fff'
        Write-Host ("{0}: {1}: Error: Write-Log was unable to find or create the path `"{2}`". Please debug.." -f $Now, $LocalScriptName, $Log)
        exit 1
      }
    }
    Try {
      ('{0}: {1}: {2}: {3}' -f $Now, $LocalScriptName, $Severity, $Message) | Out-File -filepath $Log -Append   
    }
    Catch {
      Write-Host ("{0}: {1}: Error: Something went wrong while writing to file `"{2}`". It might be locked." -f $Now, $LocalScriptName, $Log)
    }
  }
}

Function Initialize-Args {
  Param ( 
    [Parameter(Mandatory=$True,HelpMessage='Argument list')]$Args
  )
  Try {
    For ( $i = 0; $i -lt $Args.count; $i++ ) { 
      $CurrentArg = $Args[$i].ToString()
      If ($i -lt $Args.Count-1) {
        $Value = $Args[$i+1];
        If ($Value.Count -ge 2) {
          ForEach ($Item in $Value) {
            $Null = Test-Strings -String $Item
          }
        }
        Else {
          $Value = $Args[$i+1];
          $Null = Test-Strings -String $Value
        }
      } 
      Else {
        $Value = ''
      }
      Switch -regex -casesensitive ($CurrentArg) {
        '^(-H|--Hostname)$' {
          If ($value -match '^[a-zA-Z.]+') {
            If ($Value -ne ([Net.Dns]::GetHostByName((& "$env:windir\system32\hostname.exe")).HostName).tolower() -and $Value -ne 'localhost') {
              $Null = & "$env:windir\system32\ping.exe" -n 1 $Value
              If($? -eq $true) {
                $Struct.Hostname = $Value
                $i++
              }
              Else {
                Throw ('Ping to {0} failed! Please provide valid reachable hostname.' -f $Value)
              }
            }
            Else {
              $Struct.Hostname = $Value
              $i++
            }
          }
          Else {
            Throw ("Hostname `"{0}`" does not meet regex requirements." -f $value)
          }
        }
        '^(-EF|--ExclFolders)$' {
          If ($Value.Count -ge 2) {
            ForEach ($Item in $Value) {
              If ($Item -match '^[a-zA-Z0-9\\.]+') {
                $Struct.ExclFolders += $Item
              }
              Else {
                Throw ("ExclFolders `"{0}`" does not meet regex requirements." -f $Value)
              }
            }
          }
          Else {
            If ($Value -match '^[a-zA-Z0-9\\.]+') {
              $Struct.ExclFolders = $Value 
            }
            Else {
              Throw ("ExclFolders `"{0}`" does not meet regex requirements." -f $value)
            }
          }
          $i++
        }
        '^(-IF|--InclFolders)$' {
          If ($Value.Count -ge 2) {
            ForEach ($Item in $Value) {
              If ($Item -match '^[a-zA-Z0-9\\.]+') {
                $Struct.InclFolders += $Item
              }
              Else {
                Throw ("InclFolders `"{0}`" does not meet regex requirements." -f $value)
              }
            }
          }
          Else {
            If ($Value -match '^[a-zA-Z0-9\\.]+') {
              $Struct.InclFolders = $Value 
            }
            Else {
              Throw ("InclFolders `"{0}`" does not meet regex requirements." -f $value)
            }
          }
          $i++
        }
        '^(-ET|--ExclTasks)$' {
          If ($Value.Count -ge 2) {
            ForEach ($Item in $Value) {
              If ($Item -match '^[a-zA-Z0-9.]+') {
                $Struct.ExclTasks += $Item
              }
              Else {
                Throw ("ExclTasks `"{0}`" does not meet regex requirements." -f $value)
              }
            }
          }
          Else {
            If ($Value -match '^[a-zA-Z0-9.]+') {
              $Struct.ExclTasks = $Value 
            }
            Else {
              Throw ("ExclTasks `"{0}`" does not meet regex requirements." -f $value)
            }
          }
          $i++
        }
        '^(-IT|--IncllTasks)$' {
          If ($Value.Count -ge 2) {
            ForEach ($Item in $Value) {
              If ($Item -match '^[a-zA-Z0-9.]+') {
                $Struct.InclTasks += $Item
              }
              Else {
                Throw ("InclTasks `"{0}`" does not meet regex requirements." -f $value)
              }
            }
          }
          Else {
            If ($Value -match '^[a-zA-Z0-9.]+') {
              $Struct.InclTasks = $Value 
            }
            Else {
              Throw ("InclTasks `"{0}`" does not meet regex requirements." -f $value)
            }
          }
          $i++
        }
        '^(-EA|--ExclAuthors)$' {
          If ($Value.Count -ge 2) {
            ForEach ($Item in $Value) {
                $Struct.ExclAuthors += $Item
            }
          }
          Else {
            $Struct.ExclAuthors = $Value 
          }
          $i++
        }
        '^(-IA|--InclAuthors)$' {
          If ($Value.Count -ge 2) {
            ForEach ($Item in $Value) {
              $Struct.InclAuthors += $Item
            }
          }
          Else {
            $Struct.InclAuthors = $Value 
          }
          $i++
        }
        '^(-Hid|--Hidden)$' {
          If ($value -match '^[0-1]{1}$') {
            $Struct.hidden = $Value
          }
          Else {
            Throw ("Method `"{0}`" does not meet regex requirements." -f $value)
          }
          $i++
        }
        '^(-w|--Warning)$' {
          If (($value -match '^[\d]+$') -and ([int]$value -lt 100)) {
            $Struct.WarningTreshold = $value
          } 
          Else {
            Throw ('Warning treshold should be numeric and less than 100. Value given is {0}.' -f $value)
          }
          $i++
        }
        '^(-c|--Critical)$' {
          If (($value -match '^[\d]+$') -and ([int]$value -lt 100)) {
            $Struct.CriticalTreshold = $value
          } 
          Else {
            Throw ('Critical treshold should be numeric and less than 100. Value given is {0}.' -f $value)
          }
          $i++
        }
        '^(-AD|--AlertOnDisabled)$' {
          $Struct.AlertOnDisabled = $True
        }
        '^(-FP|--FullPath)$' {
          $Struct.FullPath = $True
        }
        '^(-h|--Help)$' {
          Write-Help
        }
        '^(-LE|--LastExec)$' {
          $Struct.LastExec = $True
        }
        default {
          Throw ('Illegal arguments detected: {0}' -f $_)
        }
      }
    }
  } 
  Catch {
    Write-Host ('CRITICAL: Argument: {0} Value: {1} Error: {2}' -f $CurrentArg, $Value, $_)
    Exit 2
  }
}

Function Test-Strings {
  Param ( [Parameter(Mandatory=$True,HelpMessage='String to check')][string]$String )
  $BadChars = @("``", '|', ';', "`n")
  $BadChars | ForEach-Object {
    If ( $String.Contains(('{0}' -f $_)) ) {
      Write-Host ("Error: String `"{0}`" contains illegal characters." -f $String)
      Exit $Struct.ExitCode
    }
  }
  Return $true
}

Function Get-AllTaskSubFolders {
  If ($Struct.ExclFolders){
    If ( ! ( Compare-Array -Str $Struct.FolderRef.Name -Patterns $Struct.ExclFolders ) ) {
      $Struct.AllValidFolders+=$Struct.FolderRef	         
    }
  }
  Else {
    $Struct.AllValidFolders+=$Struct.FolderRef
  }
  If ( ( $folders = $Struct.FolderRef.getfolders(1)).count -ge 1) {
    ForEach ( $folder in $folders ) {
      If ( $Struct.ExclFolders -notcontains $folder.Name ) {
        If ( ( $folder.getfolders(1).count -ge 1 ) ) {
          $Struct.FolderRef=$folder
          Get-AllTaskSubFolders
        }
        Else {
          $Struct.AllValidFolders+=$folder
        }
      }
    }
    Return
  }
}

Function Find-InclFolders {
  $TempValidFolders = $Struct.AllValidFolders
  $Struct.AllValidFolders = @()
  ForEach ($folder in $TempValidFolders) {
    If (Compare-Array -Str $Folder.Name -Patterns $Struct.InclFolders){
      $Struct.AllValidFolders += $Folder	
    }
  }
}

Function Compare-Array  {
  Param(
    [Parameter(Mandatory=$True,HelpMessage='String to search')][string]$Str,
    [Parameter(Mandatory=$True,HelpMessage='Array to search')][AllowEmptyCollection()][String[]]$Patterns
  )
  ForEach ( $Pattern in $Patterns ) { 
    If ( $Str -Match $Pattern ) {
      Return $True
    } 
  }
  Return $False
}

Function Write-Help {
  Write-Host @'
check_ms_win_tasks.ps1: This script is designed to check Windows 2008 or higher scheduled tasks and alert in case tasks
  failed in Nagios style output.
Arguments:
  -H   | --Hostname        => Optional hostname of remote system, default is localhost, not yet tested on remote host.
  -EF  | --ExclFolders     => Name of folders to exclude from monitoring.
  -IF  | --InclFolders     => Name of folders to include in monitoring.
  -ET  | --ExclTasks       => Name of task patterns to exclude from monitoring.
  -IT  | --InclTasks       => Name of task patterns to include in monitoring.
  -EA  | --ExclAuthors     => Name of task author patterns to exclude from monitoring.
  -IA  | --InclAuthors      => Name of task author patterns to include in monitoring.
  -Hid | --Hidden          => Switch to determine if hidden tasks need to be excluded.
  -a   | --AlertOnDisabled => If any tasks are disabled, throw a CRITICAL alert.
  -FP  | --FullPath        => Displays full path in plugin output
  -w   | --Warning         => Threshold for warning alert. (not yet implemented)
  -c   | --Critical        => Threshold for critical alert. (not yet implemented)
  -h   | --Help            => Print this help output.
  -LE  | --LastExec        => check if last execution is >warn or >critical in hours                                                                            
'@
  Exit $Struct.ExitCode
} 
Function Search-Tasks { 
  Try {
    $schedule = New-Object -ComObject('Schedule.Service') 
  } 
  Catch {
    Write-Host ('Error: Schedule.Service COM Object not found on {0}, which is required by this script.' -f $Struct.Hostname)
    Exit 2
  } 
  $Schedule.connect($Struct.Hostname) 
  $Struct.FolderRef = $Schedule.getfolder('\')
  Get-AllTaskSubFolders
  If ($Struct.InclFolders){
    Find-InclFolders
  }
  $OutputString = ''
  ForEach ($Folder in $Struct.AllValidFolders) {
    If (($Tasks = $Folder.GetTasks($Struct.Hidden))) {
      foreach ($Author in $Struct.ExclAuthors) {
        $Tasks = $Tasks | ? {([xml]($_.xml)).Task.RegistrationInfo.Author -notlike $Author}
      }
      if ($Struct.InclAuthors) {
        $NewTasks = @()
        foreach ($Author in $Struct.InclAuthors) {
          $newTasks += $Tasks | ? {([xml]($_.xml)).Task.RegistrationInfo.Author -like $Author}
        }
        $Tasks = $NewTasks | Select * -Unique
      }
      $Tasks | Select-TaskInfo
    }
  } 
  $Struct.TasksTotal = $Struct.TasksOk + $Struct.TasksNotOk + $Struct.TasksRunning
  If ( $Struct.TasksNotOk -gt '0' ) {
    $OutputString += ('{0} / {1} tasks failed! ' -f $Struct.TasksNotOk, $Struct.TasksTotal)
    ForEach ($BadTask in $Struct.BadTasks) {
      If ( $Struct.FullPath -eq $False ) {
              $OutputString += ("{{Taskname: `"{0}`" (Author: {1})(Exitcode: {2})(Last runtime: {3})}} " -f $BadTask.Name, $BadTask.Author, $BadTask.lasttaskresult, $BadTask.lastruntime)
      }
      Else {
        $OutputString += ("{{Taskname: `"{0}`" (Author: {1})(Exitcode: {2})(Last runtime: {3})}} " -f $BadTask.Path, $BadTask.Author, $BadTask.lasttaskresult, $BadTask.lastruntime)
      }
    }
    If ( $Struct.TasksRunning -gt '0' ) {
      $OutputString += ('{0} / {1} tasks still running! ' -f $Struct.TasksRunning, $Struct.TasksTotal)
      ForEach ( $RunningTask in $Struct.RunningTasks ) {
        If ( $Struct.FullPath -eq $False ) {
          $OutputString += ("{{Taskname: `"{0}`" (Author: {1})(Exitcode: {2})(Last runtime: {3})}} " -f $RunningTask.Name, $RunningTask.Author, $RunningTask.lasttaskresult, $RunningTask.lastruntime)
        }
        Else {
          $OutputString += ("{{Taskname: `"{0}`" (Author: {1})(Exitcode: {2})(Last runtime: {3})}} " -f $RunningTask.Path, $RunningTask.Author, $RunningTask.lasttaskresult, $RunningTask.lastruntime)
        }
      }
    }
    If (( $Struct.AlertOnDisabled -eq $True ) -and ( $Struct.TasksDisabled -gt 0 )) {
      $OutputString += ('{0} / {1} tasks disabled! ' -f $Struct.TasksDisabled, $Struct.TasksTotal)
      ForEach ( $DisabledTask in $Struct.DisabledTasks ) {
        If ( $Struct.FullPath -eq $False ) {
          $OutputString += ("{{Taskname: `"{0}`" (Author: {1}))(Last runtime: {2})}} " -f $DisabledTask.Name, $DisabledTask.Author, $DisabledTask.lastruntime)
        }
        Else {
          $OutputString += ("{{Taskname: `"{0}`" (Author: {1})(Last runtime: {2})}} " -f $DisabledTask.Path, $DisabledTask.Author, $DisabledTask.lastruntime)
        }
      }
    }
    $OutputString +=  " | 'Total Tasks'=$($Struct.TasksTotal) 'OK Tasks'=$($Struct.TasksOk);;;0;$($Struct.TasksTotal) 'Failed Tasks'=$($Struct.TasksNotOk);;1;0;$($Struct.TasksTotal) 'Running Tasks'=$($Struct.TasksRunning);;;0;$($Struct.TasksTotal) 'Disabled Tasks'=$($Struct.TasksDisabled);;;0;$($Struct.TasksTotal)"
    $Struct.ExitCode = 2
  }
  Elseif (( $Struct.AlertOnDisabled -eq $True ) -and ( $Struct.TasksDisabled -gt 0 )) {
    $OutputString += ('{0} / {1} tasks disabled! ' -f $Struct.TasksDisabled, $Struct.TasksTotal)
    ForEach ( $DisabledTask in $Struct.DisabledTasks ) {
      If ( $Struct.FullPath -eq $False ) {
        $OutputString += ("{{Taskname: `"{0}`" (Author: {1}))(Last runtime: {2})}} " -f $DisabledTask.Name, $DisabledTask.Author, $RunningTask.lastruntime)
      }
      Else {
        $OutputString += ("{{Taskname: `"{0}`" (Author: {1})(Last runtime: {2})}} " -f $DisabledTask.Path, $DisabledTask.Author, $DisabledTask.lastruntime)
      }
    }
    $OutputString +=  " | 'Total Tasks'=$($Struct.TasksTotal) 'OK Tasks'=$($Struct.TasksOk);;;0;$($Struct.TasksTotal) 'Failed Tasks'=$($Struct.TasksNotOk);;1;0;$($Struct.TasksTotal) 'Running Tasks'=$($Struct.TasksRunning);;;0;$($Struct.TasksTotal) 'Disabled Tasks'=$($Struct.TasksDisabled);;;0;$($Struct.TasksTotal)"
    $Struct.ExitCode = 2
  }
  Else {
    $OutputString +=  ('{0} / {1} tasks ran successfully. ' -f $Struct.TasksOk, $Struct.TasksTotal)
    If ($Struct.TasksRunning -gt '0') {
      $OutputString += ('{0} / {1} tasks still running! ' -f $Struct.TasksRunning, $Struct.TasksTotal)
      ForEach ($RunningTask in $Struct.RunningTasks) {
        If ( $Struct.FullPath -eq $False ) {
          $OutputString += ("{{Taskname: `"{0}`" (Author: {1})(Exitcode: {2})(Last runtime: {3})}} " -f $RunningTask.Name, $RunningTask.Author, $RunningTask.lasttaskresult, $RunningTask.lastruntime)
        }
        Else {
          $OutputString += ("{{Taskname: `"{0}`" (Author: {1})(Exitcode: {2})(Last runtime: {3})}} " -f $RunningTask.Path, $RunningTask.Author, $RunningTask.lasttaskresult, $RunningTask.lastruntime)
        }
      }
    }
    $OutputString +=  " | 'Total Tasks'=$($Struct.TasksTotal) 'OK Tasks'=$($Struct.TasksOk);;;0;$($Struct.TasksTotal) 'Failed Tasks'=$($Struct.TasksNotOk);;1;0;$($Struct.TasksTotal) 'Running Tasks'=$($Struct.TasksRunning);;;0;$($Struct.TasksTotal) 'Disabled Tasks'=$($Struct.TasksDisabled);;;0;$($Struct.TasksTotal)"
    $Struct.ExitCode = 0
  }
  Write-Host ('{0}' -f $outputString)
  Exit $Struct.ExitCode
}

Function Select-TaskInfo {
  Param (
    [Parameter(Mandatory=$True,ValueFromPipeline=$True,HelpMessage='Task to process')]$InputObject
  )
  Process {
    $ObjTask = New-Object -TypeName PSCustomObject -Property @{
      'Name' = $InputObject.name
      'Path' = $InputObject.path
      'State' = $InputObject.state
      'Enabled' = $InputObject.enabled
      'LastRunTime' = $InputObject.lastruntime
      'LastTaskResult' = $InputObject.lasttaskresult
      'NumberOfMissedRuns' = $InputObject.numberofmissedruns
      'NextRunTime' = $InputObject.nextruntime
      'Author' =  ([xml]$InputObject.xml).Task.RegistrationInfo.Author
      'UserId' = ([xml]$InputObject.xml).Task.Principals.Principal.UserID
      'Description' = ([xml]$InputObject.xml).Task.RegistrationInfo.Description
      'Cmd' = ([xml]$InputObject.xml).Task.Actions.Exec.Command 
      'Params' = ([xml]$InputObject.xml).Task.Actions.Exec.Arguments
    }
    #setting up things to handle last execution for now checking unit is hour
    If ($Struct.LastExec -eq $true) {	
      $lastExecWarn = (get-date).addHours(-$Struct.WarningTreshold)
      $lastExecCrit = (get-date).addHours(-$Struct.CriticalTreshold)
    }
    #emit warning if last task execution too old
    If ( $ObjTask.LastRunTime -lt $lastExecWarn) {
      If ($ObjTask.LastRunTime -lt $lastExecCrit) {
        #write-host 'Task ' + $ObjTask.name + ' : CRITICAL'
      }
      Else {
        #write-host 'Task ' + $ObjTask.name + ' : WARNING'
      }
      If ( ! $Struct.InclTasks ) {
        If ( ! ( Compare-Array -Str $ObjTask.Name -Patterns $Struct.ExclTasks ) ) {
          $Struct.BadTasks += $ObjTask
          $Struct.TasksNotOk += 1
        }
      }
      Else {
        If ( Compare-Array -Str $ObjTask.Name -Patterns $Struct.InclTasks ) {
          $Struct.BadTasks += $ObjTask
          $Struct.TasksNotOk += 1
        }
      }
    }
    ElseIf ( $ObjTask.LastTaskResult -eq '0'-or $ObjTask.LastTaskResult -eq '0x00041325' -or $ObjTask.LastTaskResult -eq '0x00041306' -or $ObjTask.LastRunTime -lt (get-date 2000-01-01) -and $ObjTask.Enabled ) {
# 0x00041325 => The Task Scheduler service has asked the task to run
# 0x00041306 => The last run of the task was terminated by the user
      If ( ! $Struct.InclTasks ) {
        If ( ! ( Compare-Array -Str $ObjTask.Name -Patterns $Struct.ExclTasks ) ) {
          $Struct.TasksOk += 1
        }
      }
      Else {
        If ( Compare-Array -Str $ObjTask.Name -Patterns $Struct.InclTasks ) {
          $Struct.TasksOk += 1
        }
      }
    }
    ElseIf ( $ObjTask.LastTaskResult -eq '0x8004131F' -or $ObjTask.LastTaskResult -eq '0x00041301' -or $ObjTask.LastTaskResult -eq '0x800710E0' -and $ObjTask.Enabled ) {
# 0x00041301 => The task is currently running
# 0x8004131F => An instance of this task is already running
# 0x800710E0 => The operator or administrator has refused the request
      If ( ! $Struct.InclTasks ) {
        If ( ! ( Compare-Array -Str $ObjTask.Name -Patterns $Struct.ExclTasks ) ) {
          $Struct.RunningTasks += $ObjTask
          $Struct.TasksRunning += 1
        }
      }
      Else {
        If ( Compare-Array -Str $ObjTask.Name -Patterns $Struct.InclTasks ) {
          $Struct.RunningTasks += $ObjTask
          $Struct.TasksRunning += 1
        }
      }
    }
    ElseIf ( $ObjTask.Enabled ) {
      If ( ! $Struct.InclTasks ) {
        If ( ! ( Compare-Array -Str $ObjTask.Name -Patterns $Struct.ExclTasks ) ) {
          $Struct.BadTasks += $ObjTask
          $Struct.TasksNotOk += 1
        }
      }
      Else {
        If ( Compare-Array -Str $ObjTask.Name -Patterns $Struct.InclTasks ) {
          $Struct.BadTasks += $ObjTask
          $Struct.TasksNotOk += 1
        }
      }
    }
    Else {
      If ( ! $Struct.InclTasks ) {
        If ( ! ( Compare-Array -Str $ObjTask.Name -Patterns $Struct.ExclTasks ) ) {
          $Struct.DisabledTasks += $ObjTask
          $Struct.TasksDisabled += 1
        }
      }
      Else {
        If ( Compare-Array -Str $ObjTask.Name -Patterns $Struct.InclTasks ) {
          $Struct.DisabledTasks += $ObjTask
          $Struct.TasksDisabled += 1
        }
      }
    }
  }
}
#endregion Functions

#region Main
If ( $Args ) {
  If ( ! ( $Args[0].ToString()).StartsWith('$') ) {
    If ( $Args.count -ge 1 ) {
      Initialize-Args -Args $Args
    }
  }
  Else {
    Write-Host ('CRITICAL: Seems like something is wrong with your parameters: Args: {0}.' -f $Args)
    Exit 2
  }
}
Search-Tasks
Write-Host 'UNKNOWN: Script exited in an abnormal way. Please debug...'
Exit $Struct.ExitCode
#endregion Main

check_print_server_job

# Script to check print queues of printers as they are registered on an MS print server 
# It checks for current jobs in each printer and the status is decided by the command line arguments the total jobs statuses have priority over individual printers
#
#
# http://www.opensource.org/licenses/gpl-2.0.php
#
# Copyright (c) George Panou panou.g@gmail.com https://github.com/george-panou
#

#.\check_print_server_jobs.ps1  <InfoLevel-per-printer> <Waring-per-printer> <Critical-per-printer> <TotalWarning> <TotalCritical>
#Sample call : .\check_print_server_jobs.ps1 3 5 7 10 15

#Command line arguments with default values...

param(

	[int] $InfoLevel = 2,
	[int] $Warning = 5,
	[int] $Critical = 7,
	[int] $TotalWarning = 10,
	[int] $TotalCritical = 20
  
)

#Write-Output "You specified: $Arguments"

$Result  = @(Get-WMIObject Win32_PerfFormattedData_Spooler_PrintQueue | Select Name, @{Expression={$_.jobs};Label="CurrentJobs"}, TotalJobsPrinted, JobErrors)

$InfoLevel -= 1
$Warning -= 1
$Critical -= 1
$TotalWarning -= 1
$TotalCritical -= 1
	

$OutputNagios = ""


$nl = [Environment]::NewLine
$flag=0


foreach ( $line in $Result ){
	
	$Name=$line.Name
	$Jobs=$line.CurrentJobs
	$k=0
	$l=0
    #Write-Output "$Name"
	
	#check each printer
	if($Jobs -gt $Warning -and $Name -ne "_Total"){
		 $warnOut += "$Name :  $Jobs, $nl" 
		 if($flag -lt 2){
			$flag=1
			}
	}
	elseif($Jobs -gt $Critical  -and $Name -ne "_Total"){
		 $critOut += "$Name :  $Jobs, $nl" 
		 $flag=2

	}elseif($Jobs -gt $InfoLevel -and $line.Name -ne "_Total"){
		 $tmpOut += "$Name :  $Jobs, $nl" 
	}
	
	#check total jobs of the print server
	if($Name -eq "_Total" -and $Jobs -gt $TotalWarning){
	
		$tmpOut = "Total :  $Jobs, $nl" + $tmpOut
		 if($flag -lt 2){
			$flag=1
			}
	}elseif($Name -eq "_Total" -and $Jobs -gt $TotalCritical){
	
		$tmpOut = "Total :  $Jobs, $nl" + $tmpOut
		$flag=2
	}
	
	elseif($Name -eq "_Total"){
		$tmpOut = "Total :  $Jobs, $nl" + $tmpOut
	}
	
}
	
	
if ($flag -eq 1){
	$tmpOut = "Warning : " + $tmpOut
	
}
elseif ($flag -eq 2){#
	$tmpOut = "Critical : " + $tmpOut
	
}
else{
	$tmpOut = "OK : " + $tmpOut
}
$tmpOut = $tmpOut + $critOut 
$tmpOut = $tmpOut + $warnOut

Write-Output "$tmpOut"

exit($flag)

check_veeam_eventlogs

#
# Script name:	check_veeam_eventlogs.ps1
# Version: 		1.1
# Created on: 	6May2015
# Modified on:  24April2018
# Author: 		Dallas Haselhorst
# Purpose: 		Check Veeam Backup success or failure via event logs 
#				Note: this requires PowerShell, however, it does NOT use the Veeam PowerShell plug-in 
#
# Copyright:
#	This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published
#	by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed 
#	in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
#	PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public 
#	License along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Heavily modified from the original script watch-eventlogs.ps1 
#	written by Aaron Wurthmann (aaron (AT) wurthmann (DOT) com). Nonetheless, thanks Aaron!
#
#  Older versions of NCPA used the following format, i.e. the following line can be copied to the $ARG1$ text box.
#  -t '<token>' -P <port number> -M 'agent/plugin/check_veeam_eventlogs.ps1/<ArgBackupJobName>/<ArgLastHours>'
#  Newer versions of NCPA would use the following format. Note the removal of "agent" and the added "s" to plugin
#  -t '<token>' -P <port number> -M 'plugins/check_veeam_eventlogs.ps1/<ArgBackupJobName>/<ArgLastHours>'
#
#  For testing from the Nagios command line, add './check_ncpa.py -H <IP address>' to the above line
#	ArgBackupJobName is required. 
#       *** If your backup job name has special characters or spaces, I would suggest removing them! ***
#	ArgLastMinutes should be populated with the time to check in minutes, e.g. 60 (for 1 hour), 120 (for 2 hours), 
#
#  Old Example
#  -t 'TokenPass' -P 5693 -M 'agent/plugin/check_veeam_eventlogs.ps1/Management_VMs/24' 
#	-- above line would check the last 24 hours of Veeam Backup logs for the job named "Management_VMs"
#  New Example
#  -t 'TokenPass' -P 5693 -M 'plugins/check_veeam_eventlogs.ps1/TS01/24'
#	-- above line would check the last 24 hours of Veeam Backup logs for the job named "TS01"


# Pull in arguments
$ArgLogName = "Veeam Backup" # veeam backup event log
$ArgEntryType = 1,2,3,4 # look for critical, error, warning and informational logs
$ArgProviderName = "Veeam MP"
$ArgEventID = 190 # backup job complete event id

$ArgBackupJobName = $args[0]
$ArgLastHours = $args[1]


if (!$ArgBackupJobName) { 
write-host "Sorry... at the very least, I need a backup job name."
write-host "Command line usage: check_veeam_eventlogs.ps1 <Job Name> <Last X Hours>" 
write-host "Nagios NCPA usage: agent/plugin/check_veeam_eventlogs.ps1/<Job Name>/<Last X Hours>" 
exit
}
# Setting default values if null 
if (!$ArgLastHours) { $ArgLastHours = (24) }
if (!$ArgWarningTH) { $ArgWarningTH = 0 }
if (!$ArgCriticalTH) { $ArgCriticalTH = 0 }
if (!$ArgMaxEntries) { $ArgMaxEntries = 50 }

$CriticalErrorResultCount = 0
$WarningResultCount = 0
$InfoResultCount = 0
$EventTypeLoopCount = 0
$LogNameLoopCount = 0
$ProviderNameLoopCount = 0
$EventIDLoopCount = 0

$Properties='Level','Message','ProviderName','TimeCreated','Id'

$Filter = @{
    LogName = $ArgLogName
    StartTime = (Get-Date).AddHours(-$ArgLastHours)
}

if($ArgProviderName) { $Filter += @{ProviderName = $ArgProviderName } }
if($ArgEventID) { $Filter += @{Id = $ArgEventID } }
if($ArgEntryType) { $Filter += @{Level = $ArgEntryType } }

# -ea SilentlyContinue gets rid of non-terminating error resulting from zero events
$LogEntries = Get-WinEvent -MaxEvents $ArgMaxEntries -FilterHashtable $Filter -ea SilentlyContinue -Oldest | Select-Object -Property $Properties 

if ($LogEntries) {

    ForEach ($LogEntry in $LogEntries) {
		if ($LogEntry.Message.ToString() -like "*Backup Job `'$ArgBackupJobName`'*")
		{
        $Level=$LogEntry.Level.ToString()
		if (($Level -eq 1) -Or ($Level -eq 2)) # find critical and errors
		{
		$Message=$LogEntry.Message.Substring(0,[System.Math]::Min(180, $LogEntry.Message.Length)).TrimEnd().ToString()
		$ProviderName=$LogEntry.ProviderName.ToString()
        $TimeCreated=$LogEntry.TimeCreated.ToString()
        $Id=$LogEntry.Id.ToString()
        $CriticalErrorResultCount++ 
         
                $CriticalErrorResults=@"
				
At: $TimeCreated
Level: $Level 
Event ID: $Id
Message: $Message
Source: $ProviderName
$CriticalErrorResults
"@
		}
		elseif ($Level -eq 3) # find warnings
		{
		$Message=$LogEntry.Message.Substring(0,[System.Math]::Min(180, $LogEntry.Message.Length)).TrimEnd().ToString()
		$ProviderName=$LogEntry.ProviderName.ToString()
        $TimeCreated=$LogEntry.TimeCreated.ToString()
        $Id=$LogEntry.Id.ToString()
        $WarningResultCount++ 
         
                $WarningResults=@"
At: $TimeCreated
Level: $Level 
Event ID: $Id
Message: $Message
Source: $ProviderName
$WarningResults
"@
		}
		else # all that's left, find info (4) messages
		{
		$Message=$LogEntry.Message.Substring(0,[System.Math]::Min(180, $LogEntry.Message.Length)).TrimEnd().ToString()
		$ProviderName=$LogEntry.ProviderName.ToString()
        $TimeCreated=$LogEntry.TimeCreated.ToString()
        $Id=$LogEntry.Id.ToString()
        $InfoResultCount++ 
         
                $InfoResults=@"
				
At: $TimeCreated
Level: $Level 
Event ID: $Id
Message: $Message
Source: $ProviderName
$InfoResults
"@
		}
    }

}

}

$Results= @"
$CriticalErrorResults $WarningResults $InfoResults
"@

if ($ArgEntryType) {
$TypeArray = @("all level","critical","error","warning","informational")
$LevelString = foreach ($Entry in $ArgEntryType) { 
	if ($ArgEntryType.Count -gt 1) { 
	$LevelStringBuild = $TypeArray[$Entry]
		if ($ArgEntryType.Count -ne $EventTypeLoopCount+1) {
		$LevelStringBuild +=","
		}
	}

	else { $LevelStringBuild = $TypeArray[$Entry] }
	$EventTypeLoopCount++
	$LevelStringBuild
	}
}

$LogNameString = foreach ($LogNameEntry in $ArgLogName) { 
	$LogNameStringBuild += $LogNameEntry
	if ($ArgLogName.Count -gt 1 -And $ArgLogName.Count -ne $LogNameLoopCount+1) {
		$LogNameStringBuild += ", "
		}
	$LogNameLoopCount++
	}

$ProviderNameString = foreach ($ProviderNameEntry in $ArgProviderName) { 
	$ProviderNameStringBuild += $ProviderNameEntry
	if ($ArgProviderName.Count -gt 1 -And $ArgProviderName.Count -ne $ProviderNameLoopCount+1) {
		$ProviderNameStringBuild += ", "
		}
	$ProviderNameLoopCount++
	}

$EventIDString = foreach ($EventIDEntry in $ArgEventID) { 
	$EventIDStringBuild += "$EventIDEntry"
	if ($ArgEventID.Count -gt 1 -And $ArgEventID.Count -ne $EventIDLoopCount+1) {
		$EventIDStringBuild += ", "
		}
	$EventIDLoopCount++
	}	

If ($CriticalErrorResultCount -gt 0) {
        $ResultString += "Backup failed: $CriticalErrorResultCount critical error(s) for backup job $ArgBackupJobName in last $ArgLastHours hours "
		$NagiosMetricString += "'Errors'=$CriticalErrorResultCount 'BackupUnknown'=1 "
		$ExitCode = 1
    }

If ($WarningResultCount -gt 0) {
        $ResultString += "Warning: backup job $ArgBackupJobName had $WarningResultCount warning message(s) in the last $ArgLastHours hours "
		If ($ExitCode -ne 1) {
		$NagiosMetricString += "'BackupUnknown'=1 "	
		$ExitCode = 1 
		}
		$NagiosMetricString += "'Warnings'=$WarningResultCount "
		
    }

If (($InfoResultCount -lt 1) -And ($ExitCode -ne 1)) {
        $ResultString += "Backup failed: backup job $ArgBackupJobName has not run in last $ArgLastHours hours "
		$NagiosMetricString += "'BackupNotRun'=1 "
		If ($ExitCode -ne 1) { $ExitCode = 1 }
    }
	
If (($InfoResultCount -ge 1) -And ($CriticalErrorResultCount -eq 0 ) -And ($WarningResultCount -eq 0 )){
        $ResultString += "OK: backup job $ArgBackupJobName completed successfully in last $ArgLastHours hours "
		$NagiosMetricString = "'BackupSuccess'=1 "
		$ExitCode = 0 
    }

	write-host $ResultString 
	write-host $Results 
	write-host $ResultString"|"$NagiosMetricString
	exit $ExitCode

check_veeam_eventlogs2

#
# Script name:	check_veeam_eventlogs.ps1
# Version: 		1.1
# Created on: 	6May2015
# Modified on:  24April2018
# Author: 		Dallas Haselhorst
# Purpose: 		Check Veeam Backup success or failure via event logs 
#				Note: this requires PowerShell, however, it does NOT use the Veeam PowerShell plug-in 
#
# Copyright:
#	This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published
#	by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed 
#	in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
#	PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public 
#	License along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Heavily modified from the original script watch-eventlogs.ps1 
#	written by Aaron Wurthmann (aaron (AT) wurthmann (DOT) com). Nonetheless, thanks Aaron!
#
#  Older versions of NCPA used the following format, i.e. the following line can be copied to the $ARG1$ text box.
#  -t '<token>' -P <port number> -M 'agent/plugin/check_veeam_eventlogs.ps1/<ArgBackupJobName>/<ArgLastHours>'
#  Newer versions of NCPA would use the following format. Note the removal of "agent" and the added "s" to plugin
#  -t '<token>' -P <port number> -M 'plugins/check_veeam_eventlogs.ps1/<ArgBackupJobName>/<ArgLastHours>'
#
#  For testing from the Nagios command line, add './check_ncpa.py -H <IP address>' to the above line
#	ArgBackupJobName is required. 
#       *** If your backup job name has special characters or spaces, I would suggest removing them! ***
#	ArgLastMinutes should be populated with the time to check in minutes, e.g. 60 (for 1 hour), 120 (for 2 hours), 
#
#  Old Example
#  -t 'TokenPass' -P 5693 -M 'agent/plugin/check_veeam_eventlogs.ps1/Management_VMs/24' 
#	-- above line would check the last 24 hours of Veeam Backup logs for the job named "Management_VMs"
#  New Example
#  -t 'TokenPass' -P 5693 -M 'plugins/check_veeam_eventlogs.ps1/TS01/24'
#	-- above line would check the last 24 hours of Veeam Backup logs for the job named "TS01"


# Pull in arguments
$ArgLogName = "Veeam Backup" # veeam backup event log
$ArgEntryType = 1,2,3,4 # look for critical, error, warning and informational logs
$ArgProviderName = "Veeam MP"
$ArgEventID = 490 # backup Copy job complete event id

$ArgBackupJobName = $args[0]
$ArgLastHours = $args[1]


if (!$ArgBackupJobName) { 
write-host "Sorry... at the very least, I need a backup job name."
write-host "Command line usage: check_veeam_eventlogs_bck_copy.ps1 <Job Name> <Last X Hours>" 
write-host "Nagios NCPA usage: agent/plugin/check_veeam_eventlogs_bck_copy.ps1/<Job Name>/<Last X Hours>" 
exit
}
# Setting default values if null 
if (!$ArgLastHours) { $ArgLastHours = (24) }
if (!$ArgWarningTH) { $ArgWarningTH = 0 }
if (!$ArgCriticalTH) { $ArgCriticalTH = 0 }
if (!$ArgMaxEntries) { $ArgMaxEntries = 50 }

$CriticalErrorResultCount = 0
$WarningResultCount = 0
$InfoResultCount = 0
$EventTypeLoopCount = 0
$LogNameLoopCount = 0
$ProviderNameLoopCount = 0
$EventIDLoopCount = 0

$Properties='Level','Message','ProviderName','TimeCreated','Id'

$Filter = @{
    LogName = $ArgLogName
    StartTime = (Get-Date).AddHours(-$ArgLastHours)
}

if($ArgProviderName) { $Filter += @{ProviderName = $ArgProviderName } }
if($ArgEventID) { $Filter += @{Id = $ArgEventID } }
if($ArgEntryType) { $Filter += @{Level = $ArgEntryType } }

# -ea SilentlyContinue gets rid of non-terminating error resulting from zero events
$LogEntries = Get-WinEvent -MaxEvents $ArgMaxEntries -FilterHashtable $Filter -ea SilentlyContinue -Oldest | Select-Object -Property $Properties 

if ($LogEntries) {

    ForEach ($LogEntry in $LogEntries) {
		if ($LogEntry.Message.ToString() -like "*Backup Copy Job*`'$ArgBackupJobName`'*")
		{
        $Level=$LogEntry.Level.ToString()
		if (($Level -eq 1) -Or ($Level -eq 2)) # find critical and errors
		{
		$Message=$LogEntry.Message.Substring(0,[System.Math]::Min(180, $LogEntry.Message.Length)).TrimEnd().ToString()
		$ProviderName=$LogEntry.ProviderName.ToString()
        $TimeCreated=$LogEntry.TimeCreated.ToString()
        $Id=$LogEntry.Id.ToString()
        $CriticalErrorResultCount++ 
         
                $CriticalErrorResults=@"
				
At: $TimeCreated
Level: $Level 
Event ID: $Id
Message: $Message
Source: $ProviderName
$CriticalErrorResults
"@
		}
		elseif ($Level -eq 3) # find warnings
		{
		$Message=$LogEntry.Message.Substring(0,[System.Math]::Min(180, $LogEntry.Message.Length)).TrimEnd().ToString()
		$ProviderName=$LogEntry.ProviderName.ToString()
        $TimeCreated=$LogEntry.TimeCreated.ToString()
        $Id=$LogEntry.Id.ToString()
        $WarningResultCount++ 
         
                $WarningResults=@"
At: $TimeCreated
Level: $Level 
Event ID: $Id
Message: $Message
Source: $ProviderName
$WarningResults
"@
		}
		else # all that's left, find info (4) messages
		{
		$Message=$LogEntry.Message.Substring(0,[System.Math]::Min(180, $LogEntry.Message.Length)).TrimEnd().ToString()
		$ProviderName=$LogEntry.ProviderName.ToString()
        $TimeCreated=$LogEntry.TimeCreated.ToString()
        $Id=$LogEntry.Id.ToString()
        $InfoResultCount++ 
         
                $InfoResults=@"
				
At: $TimeCreated
Level: $Level 
Event ID: $Id
Message: $Message
Source: $ProviderName
$InfoResults
"@
		}
    }

}

}

$Results= @"
$CriticalErrorResults $WarningResults $InfoResults
"@

if ($ArgEntryType) {
$TypeArray = @("all level","critical","error","warning","informational")
$LevelString = foreach ($Entry in $ArgEntryType) { 
	if ($ArgEntryType.Count -gt 1) { 
	$LevelStringBuild = $TypeArray[$Entry]
		if ($ArgEntryType.Count -ne $EventTypeLoopCount+1) {
		$LevelStringBuild +=","
		}
	}

	else { $LevelStringBuild = $TypeArray[$Entry] }
	$EventTypeLoopCount++
	$LevelStringBuild
	}
}

$LogNameString = foreach ($LogNameEntry in $ArgLogName) { 
	$LogNameStringBuild += $LogNameEntry
	if ($ArgLogName.Count -gt 1 -And $ArgLogName.Count -ne $LogNameLoopCount+1) {
		$LogNameStringBuild += ", "
		}
	$LogNameLoopCount++
	}

$ProviderNameString = foreach ($ProviderNameEntry in $ArgProviderName) { 
	$ProviderNameStringBuild += $ProviderNameEntry
	if ($ArgProviderName.Count -gt 1 -And $ArgProviderName.Count -ne $ProviderNameLoopCount+1) {
		$ProviderNameStringBuild += ", "
		}
	$ProviderNameLoopCount++
	}

$EventIDString = foreach ($EventIDEntry in $ArgEventID) { 
	$EventIDStringBuild += "$EventIDEntry"
	if ($ArgEventID.Count -gt 1 -And $ArgEventID.Count -ne $EventIDLoopCount+1) {
		$EventIDStringBuild += ", "
		}
	$EventIDLoopCount++
	}	

If ($CriticalErrorResultCount -gt 0) {
        $ResultString += "Backup Copy failed: $CriticalErrorResultCount critical error(s) for backup Copy job $ArgBackupJobName in last $ArgLastHours hours "
		$NagiosMetricString += "'Errors'=$CriticalErrorResultCount 'BackupUnknown'=1 "
		$ExitCode = 1
    }

If ($WarningResultCount -gt 0) {
        $ResultString += "Warning: backup Copy job $ArgBackupJobName had $WarningResultCount warning message(s) in the last $ArgLastHours hours "
		If ($ExitCode -ne 1) {
		$NagiosMetricString += "'BackupUnknown'=1 "	
		$ExitCode = 1 
		}
		$NagiosMetricString += "'Warnings'=$WarningResultCount "
		
    }

If (($InfoResultCount -lt 1) -And ($ExitCode -ne 1)) {
        $ResultString += "Backup Copy failed: backup Copy job $ArgBackupJobName has not run in last $ArgLastHours hours "
		$NagiosMetricString += "'BackupNotRun'=1 "
		If ($ExitCode -ne 1) { $ExitCode = 1 }
    }
	
If (($InfoResultCount -ge 1) -And ($CriticalErrorResultCount -eq 0 ) -And ($WarningResultCount -eq 0 )){
        $ResultString += "OK: backup Copy job $ArgBackupJobName completed successfully in last $ArgLastHours hours "
		$NagiosMetricString = "'BackupSuccess'=1 "
		$ExitCode = 0 
    }

	write-host $ResultString 
	write-host $Results 
	write-host $ResultString"|"$NagiosMetricString
	exit $ExitCode

check_queue_printer

#$WARN = $args[0]
#$CRIT = $args[1]

$VAR=Get-WMIObject Win32_PerfFormattedData_Spooler_PrintQueue |Select  Name, @{Expression={$_.jobs};Label="CurrentJobs"}, JobErrors  -skip 1

foreach ($ligne in $VAR){
    if ($ligne.JobErrors -gt "5"){
    Write-Output "Jobs queue en Critical $($ligne.Name), JobErrors $($Ligne.JobErrors)"
    exit 2
    }
    elseif ($ligne.CurrentJobs -gt "500"){
    Write-Output "Jobs queue en Critical $($ligne.Name), CurrentJobs $($Ligne.CurrentJobs)"
    exit 2
    }
    elseif ($ligne.JobErrors -gt "10"){
    Write-Output "Jobs queue en Warning $($ligne.Name), JobErrors $($ligne.JobErrors)"
    exit 1
    }
    elseif ($ligne.CurrentJobs -gt "250"){
    Write-Output "Jobs queue en Warning $($ligne.Name), CurrentJobs $($ligne.CurrentJobs)"
    exit 1
    }
    }

    Write-Output "Jobs queue OK"
    exit 0
 

check_veeam_status

# Arguments $Job = Nom du job $Hrs = Depuis combien de temp en Heures
# le script doit être dans: C:\Program Files\NSClient++\scripts 
# Exemple .\check-veeam-backup-status.ps1 "S_VMS" "210"
# Dans le nsclient.ini Ajouter juste en dessous de [/settings/external scripts/scripts]: 
# check_veeam_backup= cmd /c echo scripts\\check-veeam-backup-status.ps1 "$ARG1$" "$ARG2$"; exit($lastexitcode) | powershell.exe -command -

$Job = $args[0]
$Hrs = $args[1]
$VAR = Get-VBRBackupSession | Where-Object {$_.OrigJobName -eq "$Job"} | Where-Object {$_.creationtime-gt (Get-Date).AddHours(-"$Hrs")} | Select-Object name, result, creationtime, state | sort creationtime –Descending
if ($VAR -eq $null){
    Write-Output "Aucun Jobs trouve dans le delais demande"
    exit 1
    }
foreach ($ligne in $VAR){
    if ($ligne.result = "Success"){
    Write-Output "Jobs $Job OK $($ligne.name), result: $($Ligne.result), Date: $($Ligne.creationtime)"
    exit 0
    }
    elseif ($ligne.result = "Failed"){
    Write-Output "Jobs $Job CRITICAL $($ligne.Name), result: $($Ligne.result), Date: $($Ligne.creationtime)"
    exit 2
    }
    elseif ($ligne.result = "Warning"){
    Write-Output "Jobs $Job Warning $($ligne.Name), result $($ligne.result), Date: $($Ligne.creationtime)"
    exit 1
    }
    }
    

CheckIISAppPoolState

# +-------------------------------------------------------------------------------
# | File      : CheckIISAppPoolState.ps1
# | Version   : 1.0
# | Purpose   : 
# | Template  : PowerShellSupervisionTemplate.ps1 - 1.0
# |            
# | Synopsis  : 
# | Usage     : .\CheckIISAppPoolState.ps1 -ApplicationPool "AXXXAPPPOOL"
# | Copyright : STELIA Aerospace © 2016, All Rights Reserved
# +-------------------------------------------------------------------------------
# | Maintenance History                                            
# +-------------------------------------------------------------------------------
# | Name             | Date        | Version   | Description        
# +------------------|-------------|-----------|----------------------------------
# | Willy LAROCHE    | 2016-08-04  | 1.0       | Initial release
# +-------------------------------------------------------------------------------
# | SCRIPT FUNCTIONS
# +-------------------- 
# | Main ()
# +-------------------------------------------------------------------------------
[CmdletBinding()]
param (
	[Parameter(Mandatory=$true,Position=1)]
	[string]$ApplicationPool
)

Add-Type -AssemblyName "System"

# EXIT_CODE returned to Supervision
$EXIT_CODE       = 3          # Default value
$RESULT_OK       = "OK"       # => 0
$RESULT_WARNING  = "WARNING"  # => 1
$RESULT_CRITICAL = "CRITICAL" # => 2
$RESULT_UNKNOWN  = "UNKNOWN"  # => 3

# Globals
$global:CurrentExecutionPath = (Get-Location).Path
$global:ScriptName = $MyInvocation.MyCommand.Name
$global:ScriptFile = Get-Item $MyInvocation.MyCommand.Definition
$global:ScriptFilePath = split-path -parent $MyInvocation.MyCommand.Definition

# Other Variables
$global:resultString = $null

#*=============================================================================
#* MAIN SCRIPT BODY
#*=============================================================================
Function Main {
param ()
	$result = $RESULT_UNKNOWN # Default value
	$status = $RESULT_UNKNOWN
	$perfData = $null
	
	try {
		[System.Reflection.Assembly]::LoadFrom( "C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll" )  >  $null

		$servermanager = [Microsoft.Web.Administration.ServerManager]::OpenRemote("localhost")
		if ($servermanager.ApplicationPools.Count -eq 0) {
			throw "Application Pool is not found"
		}

		$appPools = $servermanager.ApplicationPools["$ApplicationPool"]
		$iis=get-itemproperty HKLM:\SOFTWARE\Microsoft\InetStp\  | select setupstring

		if ($appPools -ne $null)  {
			$status = $appPools.state
			
			if ($status -eq "Started") {
				$result = $RESULT_OK
			}
			else {
				$result = $RESULT_CRITICAL
			}
		}

		$resultString = ("{0}: {1}: {2}" -f $result, $ApplicationPool, $status)
		# $perfData = ("'{0}'={1}" -f $CounterName, $counterValue)
		# $resultString += " | " + $perfData

		write-host $resultString

		# $resultString = ("IIS Application Pool '{0}': [{1}] | Status: {2} | {3} " -f $ApplicationPool, $result, $status, $iis.SETUPSTRING)
		# write-host $resultString
	}
	catch {
		$resultString = ("Error: {0} ({1}) " -f $_.Exception.Message, $_.Exception.GetType().FullName)
		$resultString += ("Detail: {0} " -f $_.Exception.ToString())
		write-host $resultString -foreground red
		$result = $RESULT_CRITICAL
	}	
	return $result
}

#*=============================================================================
#* Call Main Method
#*=============================================================================
$result_code = Main
switch($result_code){
	$RESULT_OK 	     { $EXIT_CODE = 0 }
	$RESULT_WARNING  { $EXIT_CODE = 1 }
	$RESULT_CRITICAL { $EXIT_CODE = 2 }
	$RESULT_UNKNOWN  { $EXIT_CODE = 3 }
	default 	     { $EXIT_CODE = 3 }
}
exit $EXIT_CODE

CheckIISCounter

# +-------------------------------------------------------------------------------
# | File      : CheckIISCounter.ps1
# | Version   : 1.0
# | Purpose   : 
# | Template  : PowerShellSupervisionTemplate.ps1 - 1.0
# |            
# | Synopsis  : 
# | Usage     : .\CheckIISCounter.ps1 -Server "AXXXAPPPOOL" -Website "IIS_EXXXX" -CounterName "Current Connections" -Warning_value 10 -Critical_value 20
# |             Other Counters : "Current Connections"
# |                              "Bytes Sent/sec"
# |                              "Bytes Received/sec"
# |                              "Total Method Requests/sec"
# |                              "..."
# |
# | Copyright : STELIA Aerospace © 2016, All Rights Reserved
# +-------------------------------------------------------------------------------
# | Maintenance History                                            
# +-------------------------------------------------------------------------------
# | Name             | Date        | Version   | Description        
# +------------------|-------------|-----------|----------------------------------
# | Willy LAROCHE    | 2016-08-04  | 1.0       | Initial release
# +-------------------------------------------------------------------------------
# | SCRIPT FUNCTIONS
# +-------------------- 
# | Main ()
# +-------------------------------------------------------------------------------
[CmdletBinding()]
Param(
   [Parameter(Mandatory=$True,Position=1)]
   [string]$Server,
   [Parameter(Mandatory=$True,Position=2)]
   [string]$Website,
   [Parameter(Mandatory=$True,Position=3)]
   [string]$CounterName,
   [Parameter(Mandatory=$True,Position=4)]
   [int]$Warning_value,
   [Parameter(Mandatory=$True,Position=5)]
   [int]$Critical_value
)

Add-Type -AssemblyName "System"

# EXIT_CODE returned to Supervision
$EXIT_CODE       = 3          # Default value
$RESULT_OK       = "OK"       # => 0
$RESULT_WARNING  = "WARNING"  # => 1
$RESULT_CRITICAL = "CRITICAL" # => 2
$RESULT_UNKNOWN  = "UNKNOWN"  # => 3

# Globals
$global:CurrentExecutionPath = (Get-Location).Path
$global:ScriptName = $MyInvocation.MyCommand.Name
$global:ScriptFile = Get-Item $MyInvocation.MyCommand.Definition
$global:ScriptFilePath = split-path -parent $MyInvocation.MyCommand.Definition

# Other Variables
$global:resultString = $null

#*=============================================================================
#* MAIN SCRIPT BODY
#*=============================================================================
Function Main {
param ()
	$result = $RESULT_UNKNOWN # Default value
	$perfData = $null
	
	try {
		
		Import-Module WebAdministration
		$counter = Get-Counter "\\$Server\Web Service($Website)\$CounterName"
		
		if ($counter -ne $null) {

			$counterValue = $counter.CounterSamples.CookedValue

			if ($counterValue -gt $Critical_value) {
				$result = $RESULT_CRITICAL
			}
			elseif ($counterValue -gt $Warning_value) {
				$result = $RESULT_WARNING
			}
			else {
				$result = $RESULT_OK
			}			
			$resultString = ("{0}: {2}: {3}" -f $result, $Website, $CounterName, $counterValue)
		}

		$perfData = ("'{0}'={1}" -f $CounterName, $counterValue)
		$resultString += " | " + $perfData

		write-host $resultString
	}
	catch {
		$resultString = ("Error: {0} ({1}) " -f $_.Exception.Message, $_.Exception.GetType().FullName)
		$resultString += ("Detail: {0} " -f $_.Exception.ToString())
		write-host $resultString -foreground red
		$result = $RESULT_CRITICAL
	}	
	return $result
}

#*=============================================================================
#* Call Main Method
#*=============================================================================
$result_code = Main
switch($result_code){
	$RESULT_OK 	     { $EXIT_CODE = 0 }
	$RESULT_WARNING  { $EXIT_CODE = 1 }
	$RESULT_CRITICAL { $EXIT_CODE = 2 }
	$RESULT_UNKNOWN  { $EXIT_CODE = 3 }
	default 	     { $EXIT_CODE = 3 }
}
exit $EXIT_CODE

CheckIISWebsiteState

# +-------------------------------------------------------------------------------
# | File      : CheckIISWebsiteState.ps1
# | Version   : 1.0
# | Purpose   : 
# | Template  : PowerShellSupervisionTemplate.ps1 - 1.0
# |            
# | Synopsis  : 
# | Usage     : .\CheckIISWebsiteState.ps1 -Website "IIS_EXXX"
# | Copyright : STELIA Aerospace © 2016, All Rights Reserved
# +-------------------------------------------------------------------------------
# | Maintenance History                                            
# +-------------------------------------------------------------------------------
# | Name             | Date        | Version   | Description        
# +------------------|-------------|-----------|----------------------------------
# | Willy LAROCHE    | 2016-08-04  | 1.0       | Initial release
# +-------------------------------------------------------------------------------
# | SCRIPT FUNCTIONS
# +-------------------- 
# | Main ()
# +-------------------------------------------------------------------------------
[CmdletBinding()]
param (
	[Parameter(Mandatory=$true,Position=1)]
	[string]$Website
)

Add-Type -AssemblyName "System"

# EXIT_CODE returned to Supervision
$EXIT_CODE       = 3          # Default value
$RESULT_OK       = "OK"       # => 0
$RESULT_WARNING  = "WARNING"  # => 1
$RESULT_CRITICAL = "CRITICAL" # => 2
$RESULT_UNKNOWN  = "UNKNOWN"  # => 3

# Globals
$global:CurrentExecutionPath = (Get-Location).Path
$global:ScriptName = $MyInvocation.MyCommand.Name
$global:ScriptFile = Get-Item $MyInvocation.MyCommand.Definition
$global:ScriptFilePath = split-path -parent $MyInvocation.MyCommand.Definition

# Other Variables
$global:resultString = $null

#*=============================================================================
#* MAIN SCRIPT BODY
#*=============================================================================
Function Main {
param ()
	$result = $RESULT_UNKNOWN # Default value
	$status = $RESULT_UNKNOWN
	$perfData = $null
	
	try {
		
		[System.Reflection.Assembly]::LoadFrom( "C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll" )  >  $null

		$servermanager = [Microsoft.Web.Administration.ServerManager]::OpenRemote("localhost")
		if ($servermanager.Sites.Count -eq 0) {
			throw "Website is not found"
		}

		$site= $servermanager.Sites["$Website"]
		
		$iis=get-itemproperty HKLM:\SOFTWARE\Microsoft\InetStp\  | select setupstring
		
		if ($site -ne $null) {
			$status= $site.State
		
			if ($status -eq "Started") {
				$result = $RESULT_OK
			}
			elseif ($status -eq $null) {
				$status = "Website exists but has no state"
				$result = $RESULT_CRITICAL
			}
			else {	
				$result = $RESULT_CRITICAL
			}
		}
		$resultString = ("{0}: {1}: {2}" -f $result, $Website, $status)
		# $perfData = ("'{0}'={1}" -f $CounterName, $counterValue)
		# $resultString += " | " + $perfData
		# $resultString = ("IIS Website '{0}': [{1}] | Status: {2} | {3} " -f $Website, $result, $status, $iis.SETUPSTRING)
		write-host $resultString
	}
	catch {
		$resultString = ("Error: {0} ({1}) " -f $_.Exception.Message, $_.Exception.GetType().FullName)
		$resultString += ("Detail: {0} " -f $_.Exception.ToString())
		write-host $resultString -foreground red
		$result = $RESULT_CRITICAL
	}	
	return $result
}

#*=============================================================================
#* Call Main Method
#*=============================================================================
$result_code = Main
switch($result_code){
	$RESULT_OK 	     { $EXIT_CODE = 0 }
	$RESULT_WARNING  { $EXIT_CODE = 1 }
	$RESULT_CRITICAL { $EXIT_CODE = 2 }
	$RESULT_UNKNOWN  { $EXIT_CODE = 3 }
	default 	     { $EXIT_CODE = 3 }
}
exit $EXIT_CODE

CollectIISCounter

<#
UNKNOWN - not found
OK - connections
warning - current connections greater than warning value
critical - current connection greater than critical value

#>

#requires -runasadministrator

#
# Shell arguments
#
[CmdletBinding()]
Param(
   [Parameter(Mandatory=$True,Position=1)]
   [string]$server,
   [Parameter(Mandatory=$True,Position=2)]
   [string]$website,
   [Parameter(Mandatory=$True,Position=3)]
   [string]$counterName,
   [Parameter(Mandatory=$True,Position=4)]
   [int]$warning_value,
   [Parameter(Mandatory=$True,Position=5)]
   [int]$critical_value
   )

Set-Variable OK 0 -option Constant
Set-Variable WARNING 1 -option Constant
Set-Variable CRITICAL 2 -option Constant
Set-Variable UNKNOWN 3 -option Constant


#
# ASK STATUS
#
Import-Module WebAdministration

$counter = Get-Counter "\\$server\Web Service($website)\$counterName"




# Nagios output

$resultstring=$counterName + ' UNKNOWN ' + $website + ' not found' 
$exit_code = $UNKNOWN
  
if ($counter -ne $null) {

	$counterValue=$counter.CounterSamples.CookedValue
	
	if ($counterValue -gt $critical_value) {
		$status_str= 'COUNTER CRITICAL '+ $website + ' '+$counterName+'= '+ $counterValue
		$exit_code = $CRITICAL
	}
	elseif ($counterValue -gt $warning_value) {
		$status_str= 'COUNTER WARNING '+ $website +' '+$counterName+' '+ $counterValue
		$exit_code = $WARNING
	}
	else{
		$status_str= 'COUNTER OK '+ $website +' '+$counterName+' '+ $counterValue
		$exit_code = $OK
	}
    	
	$perf_data= $counterName+"=" + $counterValue + ';' + $warning_value + ';' + $critical_value + "; "
	$resultstring= "$status_str  |  $perf_data " 
}



Write-Host $resultstring
exit $exit_code

kill_memory

#################
# Charge Memory #
#################
# Variable
$f = "c:\temp\fichier.txt"
$e = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"

# Nombre d'ajout du text $e dans le fichier $f 
$i=1
do {
Add-Content -Path $f -value $e -Force
$i++
} while ($i -le 15000)

# nombre d'execution du fichier $f
$s=1
do {
start $f
$s++
} while ($s -le 150)

# Recherche et arret du processe notepad.
if ( Get-Process -Name "notepad" ) 
 
{ Stop-Process -Name "notepad" }

# principe: executer en masse le fichier.txt pour monter en charge la mémoire virtuel et la liberé par un kill process basique.
# attention une charge trop importante peu faire planter la machine.
# à vos risques est périle.

remote_ping.bat

@ECHO OFF
SETLOCAL

REM ####### ENSURE ALL REQUIRED INFO IS PRESENT --UNIX VAR--######
@ECHO %1%2%3%4%5%6|FIND "ARG"
IF NOT ERRORLEVEL 1 GOTO MSEOF

REM ####### ENSURE ALL REQUIRED INFO IS PRESENT --WIN VAR--######
IF "%1"=="" GOTO MSEOF
IF "%2"=="" GOTO MSEOF
IF "%3"=="" GOTO MSEOF
IF "%4"=="" GOTO MSEOF
IF "%5"=="" GOTO MSEOF
IF "%6"=="" GOTO MSEOF

REM PAUSE

@ECHO %4 |FIND "%%%"
IF NOT ERRORLEVEL 1 GOTO MSEOF
@ECHO %6 |FIND "%%%"
IF NOT ERRORLEVEL 1 GOTO MSEOF

REM ####### ASSIGN EACH TO A VARIABLE TO REFERENCE IT LATER..######
SET IP=%1
SET PKT=%2
SET WRTA=%3
SET WPL=%4
SET CRTA=%5
SET CPL=%6

REM ######## CAPTURE FRESH DATA TO A FILE #######
REM 20120920 TO AVOID COLLISIONS DURING CONCURRENT CALLS CREATE A SEMI-RANDOM FILE NAME
SET RANDOMFILENAME=%RANDOM%-%1-TMP
@ECHO ->%RANDOMFILENAME%

PING %IP% -n %PKT% >>%RANDOMFILENAME%

REM ######## PICKOUT THE DATA WE NEED FROM THE FILE #######
FOR /F "TOKENS=11 DELIMS= " %%K IN ('findstr /c:"Lost" %RANDOMFILENAME%') DO SET LST=%%K

IF ERRORLEVEL 1 GOTO TIMEOUT
FOR /F "TOKENS=9 DELIMS= " %%K IN ('findstr /c:"Average" %RANDOMFILENAME%') DO SET AVG=%%K


REM PAUSE
DEL /Q %RANDOMFILENAME%


REM ######## TRIM THE VARIABLES...####
SET AVG=%AVG:M=%
SET AVG=%AVG:S=%

:TIMEOUT
SET LST=%LST:(=%
SET LST=%LST:~0,-1%


REM ######## NOW THE FUN STUFF, COMPARE THE WARNING, CRITICAL VALUES..####

IF %LST% GEQ %CPL% GOTO CPL-2
IF %AVG% GEQ %CRTA% GOTO CRTA-2
IF %LST% GEQ %WPL% GOTO WPL-1
IF %AVG% GEQ %WRTA% GOTO WRTA-1

REM PAUSE

GOTO OK-0

:CPL-2
@ECHO REMOTE CONNECTION TO %IP% IS DOWN: LOSS=%LST%%% (%PKT% PKTS)^|rta=%CRTA%;%WRTA% pl=%LST%%%;%WPL%;%CPL%
REM GOTO EOF
@EXIT 2

:CRTA-2
@ECHO REMOTE CONNECTION TO %IP% IS DOWN: LOSS=%LST%%%, AVE=%AVG%ms (%PKT% PKTS)^|rta=%AVG%ms;%WRTA%;%CRTA% pl=%LST%%%;%WPL%;%CPL%
REM GOTO EOF
@EXIT 2

:WPL-1
@ECHO REMOTE CONNECTION TO %IP% IS SLOW: LOSS=%LST%%%, AVE=%AVG%ms (%PKT% PKTS)^|rta=%WRTA%;%CRTA% pl=%LST%%%;%WPL%;%CPL%
REM GOTO EOF
@EXIT 1

:WRTA-1
@ECHO REMOTE CONNECTION TO %IP% IS SLOW: LOSS=%LST%%%, AVE=%AVG%ms (%PKT% PKTS)^|rta=%AVG%ms;%WRTA%;%CRTA% pl=%LST%%%;%WPL%;%CPL%
REM GOTO EOF
@EXIT 1

:OK-0
@ECHO REMOTE CONNECTION TO %IP% IS UP: LOSS=%LST%%%, AVE=%AVG%ms (%PKT% PKTS)^|rta=%AVG%ms;%WRTA%;%CRTA% pl=%LST%%%;%WPL%;%CPL%
REM GOTO EOF
@EXIT 0

:MSEOF
@ECHO USAGE:PING_REMOTE ^ ^^,^ ^,^
@ECHO EXAMPLE: /USR/LOCAL/NAGIOS/LIBEXEC/CHECK_NRPE -H 172.19.48.139 -C PING_REMOTE -T 90 -A 172.19.88.30 65 450,1 700,5 (FROM NAGIOS SVR.)
@ECHO : PING_REMOTE 192.168.0.1 5 200,1 400,10 (FROM A LOCAL WIN WKS WHERE PING_REMOTE.BAT RESIDES)
@ECHO : (WPL\CPL ARE IN PERCENT, WITHOUT THE "%%%" SYMBOL!!)

REM GOTO EOF
@EXIT 0

:EOF 

Exchange

- Exchange-serverCertificate

# Exchange 2013 Server Certificat
# 


$status = 0
$descr=""
$date= Get-Date
add-pssnapin Microsoft.Exchange.Management.PowerShell.SnapIn


$GetCrt = Get-ExchangeCertificate | Where-Object {$_.Status -ne "Valid"}
foreach ( $item in $GetCrt ){
	$status = 2
	#$NameCrt = $item.DnsNameList
    $NameCrt = $item.Subject
	$NameCrt2 = $NameCrt.Punycode
	$DateCrt = $item.NotAfter
	$ValidCrt = $item.Status
	$descr = "$descr Certificat: $NameCrt (Due Date: $DateCrt) ,`n"
    if ($DateCrt -lt $date ){
	$status = 1
	}
	}	

switch ($status)
	{
	0 {"OK: Tous les certificats sont valides."}
	1 {"CRITICAL: Certificat expire: `n $descr"; $status =2}
	2 {"CRTICIAL: Certificat '$NameCrt' invalide "}
	}

exit $status

- Exchange-serverComponentHealth

# Exchange 2013 Server Component Health Check
# 

Param(
  [string]$ExchServ
)

$status = 0

add-pssnapin Microsoft.Exchange.Management.PowerShell.SnapIn


#Get-ServerComponentState -Identity $ExchServ | Where-Object { $_.State -ne "Active" } | Where-Object { $_.Component -ne  "ForwardSyncDaemon"  } | Where-Object { $_.Component -ne  "ProvisioningRps"  }
$SSE2 = Get-ServerComponentState -Identity $ExchServ | Where-Object { $_.State -ne "Active" } | Where-Object { $_.Component -ne  "ForwardSyncDaemon"  } | Where-Object { $_.Component -ne  "ProvisioningRps"  }
Get-ServerComponentState -Identity $ExchServ | Where-Object { $_.State -ne "Active" } | Where-Object { $_.Component -ne  "ForwardSyncDaemon"  } | Where-Object { $_.Component -ne  "ProvisioningRps"  } | Export-Csv -Path C:\Temp\LogExch\TempSRVC.csv -Delimiter ";"
$SSE223 = $SSE2.State
$ImptpsCSV = Import-Csv -Path C:\Temp\LogExch\TempSRVC.csv -Delimiter ";"
if ( $SSE223 -eq "Inactive" ) {
	# At least one health check is inactive
	$status=2
	foreach ( $Srvc in $ImptpsCSV ){
		$Name = $Srvc.Component}
	$desc = "$Name checks is KO"
	}
if ($status -eq "2") {
	Write-Host "CRITICAL: $desc"
} elseif ($status -eq "0") {
	Write-Host "All Service Active for $ExchServ"
     } 


exit $status

- Exchange-serverDB

# Exchange 2013 Server DB
# 

<#Param(
  [string]$ServerExch
)
#>

$status = 0
$descr = ""
add-pssnapin Microsoft.Exchange.Management.PowerShell.SnapIn

$GetDB = Get-MailboxDatabase | Where-Object {$_.IsValid -ne "True"}
foreach ( $item in $GetDB ){
	$status = 2
	$ID = $Item.Name
	$descr = "$descr DB: $ID is not Valid ,"
	}	

if ( $status -ne 0 ) { 
	Write-Host "CRITICAL: $descr"
	}
else { 
	Write-Host "OK: Toutes les bases sont valides."
	}

exit $status

- Exchange-serverPoolIIS

# Exchange 2013 Server Pool IIS
# 

Param(
  [string]$ExchServ
)

$status = 0
add-pssnapin Microsoft.Exchange.Management.PowerShell.SnapIn


Invoke-Command -ComputerName $ExchServ -ScriptBlock { 
$POOL = Get-WebAppPoolState | select ItemXPath,Value | Where-Object { $_.Value -ne "Started" } | Where-Object { $_.ItemXPath -ne "/system.applicationHost/applicationPools/add[@name='.NET v4.5']" } | Where-Object { $_.ItemXPath -ne "/system.applicationHost/applicationPools/add[@name='.NET v4.5 Classic']" }
Clear-Content c:\temp\ResultPoolIIS.txt -ErrorAction SilentlyContinue
if ( $POOL -ne $Null ){		
	Add-Content -Path c:\temp\ResultPoolIIS.txt -Value "Nom du pool:" }
foreach ( $item in $POOL ) {
	$item1 = $item.ItemXPath
	$Name = ($item1.split("'")[1])
	Add-Content -Path c:\temp\ResultPoolIIS.txt -Value "$Name"
	} 
} 

$PoolLog = Get-Content -Path "\\$ExchServ\C$\temp\ResultPoolIIS.txt" -ErrorAction SilentlyContinue
if ( $PoolLog -ne $Null ) { 
	$status =2
	Write-Host "CRITICAL: Pool problematique sur le serveur $ExchServ : $PoolLog"
	}
else { 
	Write-Host "OK: Pas de Pool problematique sur le serveur $ExchServ"
	}

exit $status