<#
.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]--------------------------------------------------------

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

#Import ServerManger Module (adds Add-WindowsFeature cmdlet)
Import-Module ServerManager

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

#Log File Info
$sLogPath = "C:\Tmp"
$sLogName = "Install-Service-SNMP.log"
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName

Start-Transcript -Path $sLogFile -NoClobber

$pmanagers = @("10.0.4.57","centreon.tips-of-mine.local") # ADD YOUR MANAGER(s) in format @("manager1","manager2")
$CommString = @("Public","SUP-MON-PRIV") # ADD YOUR COMM STRING(s) in format @("Community1","Community2")

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

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

# Verifiez si les services SNMP sont deja installes (il doit y en avoir entre 2 et 3 services)
$CheckSNMPServices = Get-WindowsFeature -Name *SNMP*

#Si il y a au moins un service alors nous allons controler l'ensemble des services
If ($checkSNMPServices.Installed -eq "True") {
    Write-Output "Les services SNMP ont été trouvés sur ce systéme. Vérification de l'installation des sous-composants."

    #Vérifier si le service SNMP est installé (note : "Service SNMP" et non "Services")
    $CheckSNMPService = Get-WindowsFeature | Where-Object { $_.Name -eq "SNMP-Service" }

    If ($CheckSNMPService.Installed -ne "True") {
		
        #Install/activation SNMP Service
        Write-output "Le service SNMP n'est pas actuellement installé : Installation"
        Install-WindowsFeature SNMP-Service -IncludeAllSubFeature -IncludeManagementTools | Out-Null
    }
    Else {
		Write-Output "Service SNMP non nécessaire"
	}
   
    #Check If SNMP-WMI-Provider is Installed
    $CheckWMIProvider = Get-WindowsFeature | Where-Object { $_.Name -eq "SNMP-WMI-Provider" }

    If ($CheckWMIProvider.Installed -ne "True") {
		
        #Installation/Activation des services SNMP
        Write-output "SNMP WMI Provider n'est pas actuellement installé : Installation"
        Install-WindowsFeature SNMP-WMI-Provider -IncludeAllSubFeature -IncludeManagementTools | Out-Null
    }
    Else {
		Write-Output "SNMP WMI Provider non nécessaire"
	}

    $check = Get-WindowsFeature -Name SNMP-Service

    ##Verify Windows Services Are Enabled
    If($check.Installed -eq "True") {
        Write-Host "Configuring SNMP Services..."

        #Set SNMP Permitted Manager(s) ** WARNING : This will over write current settings **
        reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\PermittedManagers" /v 1 /t REG_SZ /d localhost /f | Out-Null

        #Set SNMP Traps and SNMP Community String(s) - *Read Only*
        Foreach ($String in $CommString) {
            reg add ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\" + $String) /f | Out-Null
            # Set the Default value to be null
            reg delete ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\" + $String) /ve /f | Out-Null
            reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\ValidCommunities" /v $String /t REG_DWORD /d 4 /f | Out-Null
            $i = 2
            Foreach ($Manager in $PManagers) {
                reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\PermittedManagers" /v $i /t REG_SZ /d $manager /f | Out-Null
                reg add ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\" + $String) /v $i /t REG_SZ /d $manager /f | Out-Null
                $i++
            }
        }
    }
}
#Sinon nous installatons l'ensemble des composants
Else {
    Write-Output "Aucun service SNMP n'a été trouvé installé sur ce systéme. Installation de SNMP et de ses sous-composants"
   
    #Installation/Activation des services SNMP
    Install-WindowsFeature SNMP-Service -IncludeAllSubFeature -IncludeManagementTools | Out-Null
}

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

Stop-Transcript