<#
.SYNOPSIS
    <Overview of script>

.NOTES
    Version        : 1.0
    Author         : Hubert CORNET
    Creation Date  : <Date>
    Purpose/Change : <Initial script development>

.LINK
    https://www.tips-of-mine.fr

.EXEMPLE
    <Example goes here. Repeat this attribute for more than one example>

.DESCRIPTION
    <Brief description of script>

.PARAMETER <Parameter_Name>
    <Brief description of parameter input required. Repeat this attribute if required>

.INPUTS
    <Inputs if any, otherwise state None>

.OUTPUTS
    <Outputs if any, otherwise state None - example: Log file stored in C:\Windows\Temp\<name>.log>
#>

#---------------------------------------------------------[Initialisations]--------------------------------------------------------

[CmdletBinding()]
Param(
    [Parameter(Mandatory=$True,Position=1)]
    [int]$PortSearch
    )

# Définir l'action d'erreur pour continuer silencieusement
$ErrorActionPreference = "SilentlyContinue"

#----------------------------------------------------------[Declarations]----------------------------------------------------------
# Version Script
$sScriptVersion = "1.0"

#Log File Info
$sLogPath = "C:\Tmp"
$sLogName = "<script_name>.log"
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName

Start-Transcript -Path $sLogFile -NoClobber

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

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

#-----------------------------------------------------------[Functions]------------------------------------------------------------

#--------------------------------------------------------[Debut Du Script]---------------------------------------------------------

$ListPort = Get-NetTCPConnection

Foreach ($Port in $ListPort) {
    If ($Port.LocalPort -eq $PortSearch) {
        $DataTexte       = "Service avec $PortSearch fonctionnel"
        $bReturnCritical = $False
        break
    }
    Else {
        $bReturnCritical = $TRUE
        $strCritical     = "Critique"
        $DataTexte       = "Service avec $PortSearch non fonctionnel"
    }
}

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

#---------------------------------------------------------[Fin Du Script]----------------------------------------------------------

Stop-Transcript