Function QuerySQLServer([string]$DBServer, [string]$DBName, [string]$Query) {
    Try {
        $ErrorActionPreference = "Stop"
   
        $resultsDataTable = New-Object System.Data.DataTable

        $cn = new-object System.Data.SqlClient.SqlConnection("Data Source=$DBServer;Integrated Security=SSPI;Initial Catalog=$DBName")
        $cn.open()
 
        $cmd = new-object "System.Data.SqlClient.SqlCommand" ($Query , $cn)
        $reader = $cmd.ExecuteReader()

        $resultsDataTable.Load($reader)

        $cn.Close()

        return $resultsDataTable
    }
    Catch {
        write-host $_.Exception.Message
        $_.Exception.Message >> "d:\error.log"
    }
    Finally {
     $ErrorActionPreference = "Continue"
    }
}

cls

$bReturnOK = $TRUE
$bReturnCritical = $FALSE
$bReturnWarning = $FALSE
$returnStateOK = 0
$returnStateWarning = 1
$returnStateCritical = 2
$returnStateUnknown = 3

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

$ComputerName = $env:COMPUTERNAME

$Liste = QuerySQLServer "$ComputerName" "dhb_prd" "SELECT wait_type ,wait_time_ms ,CONVERT(DECIMAL(7,4),100.0 * wait_time_ms/SUM(wait_time_ms) OVER()) AS wait_percent FROM sys.dm_os_wait_stats WHERE wait_type NOT IN ('CLR_SEMAPHORE','LAZYWRITER_SLEEP','RESOURCE_QUEUE','SLEEP_TASK' ,'SLEEP_SYSTEMTASK','SQLTRACE_BUFFER_FLUSH','WAITFOR', 'LOGMGR_QUEUE','CHECKPOINT_QUEUE' ,'REQUEST_FOR_DEADLOCK_SEARCH','XE_TIMER_EVENT','BROKER_TO_FLUSH','BROKER_TASK_STOP', 'CLR_MANUAL_EVENT','CLR_AUTO_EVENT','DISPATCHER_QUEUE_SEMAPHORE', 'FT_IFTS_SCHEDULER_IDLE_WAIT', 'XE_DISPATCHER_WAIT', 'XE_DISPATCHER_JOIN')" # -- filtrage de quelques types d'attente ORDER BY wait_percent DESC GO"
$Liste | Format-Table | Out-String|% {Write-Host $_}

pause
$DataTexte = $Liste.waiting_tasks_count+$Liste.wait_time_ms+$Liste.max_wait_time_ms+$Liste.signal_wait_time_ms

If ($bReturnCritical) {
    write-output $strCritical
    write-output $strWarning "|" $DataTexte
    exit $returnStateCritical
}
Elseif ($bReturnWarning) {
    write-output $strWarning "|" $DataTexte
    exit $returnStateWarning
}
Else {
    write-output "OK - Nombre de requete en cours : $DataTexte | 'NbrequetesEnCours'=$DataTexte "
    exit $returnStateOK
}