# --- KONFIGURATION --- $SourcePath = "\\DeinDC\DeploymentShare\NetSNMP_Static" # <-- HIER ANPASSEN $InstallBase = "C:\usr" $BinDir = "$InstallBase\bin" $ConfigDir = "$InstallBase\etc\snmp" $MibDir = "$InstallBase\share\snmp\mibs" $LogFile = "C:\Windows\Temp\netsnmp_install.log" $ServiceName = "Net-SNMP Agent" Start-Transcript -Path $LogFile -Append Try { # 1. Prüfen: Existiert der Dienst schon? $Service = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue if (-not $Service) { Write-Host "$ServiceName wird installiert..." # 2. Ordnerstruktur erstellen New-Item -Path $BinDir -ItemType Directory -Force | Out-Null New-Item -Path $ConfigDir -ItemType Directory -Force | Out-Null New-Item -Path $MibDir -ItemType Directory -Force | Out-Null # 3. Dateien kopieren # EXE Copy-Item -Path "$SourcePath\snmpd.exe" -Destination "$BinDir\snmpd.exe" -Force # Config (snmpd.conf) Copy-Item -Path "$SourcePath\snmpd.conf" -Destination "$ConfigDir\snmpd.conf" -Force # MIBs if (Test-Path "$SourcePath\mibs") { Copy-Item -Path "$SourcePath\mibs\*" -Destination $MibDir -Force } # 4. Dienst erstellen (via sc.exe) $BinPath = "$BinDir\snmpd.exe -service" # Wir nutzen sc.exe create $ScArgs = 'create "{0}" binPath= "{1}" start= auto DisplayName= "{0}"' -f $ServiceName, $BinPath Start-Process -FilePath "sc.exe" -ArgumentList $ScArgs -Wait -NoNewWindow # Kurze Pause, damit der Service Manager die Registrierung abschließen kann Start-Sleep -Seconds 2 # 5. Dienst starten (via sc.exe start) Write-Host "Versuche Dienst via sc.exe zu starten..." $StartArgs = 'start "{0}"' -f $ServiceName Start-Process -FilePath "sc.exe" -ArgumentList $StartArgs -Wait -NoNewWindow Write-Host "Installation erfolgreich abgeschlossen." } else { # Config Update Logik $SourceHash = Get-FileHash "$SourcePath\snmpd.conf" $LocalHash = Get-FileHash "$ConfigDir\snmpd.conf" -ErrorAction SilentlyContinue if ($SourceHash.Hash -ne $LocalHash.Hash) { Write-Host "Konfiguration aktualisiert. Dienst wird neu gestartet (Stop/Start via sc.exe)." # Neue Config kopieren Copy-Item -Path "$SourcePath\snmpd.conf" -Destination "$ConfigDir\snmpd.conf" -Force # --- Neustart Logik via SC --- # 1. Stop $StopArgs = 'stop "{0}"' -f $ServiceName Start-Process -FilePath "sc.exe" -ArgumentList $StopArgs -Wait -NoNewWindow # Wichtig: Warten, bis der Prozess wirklich beendet ist Start-Sleep -Seconds 3 # 2. Start $StartArgs = 'start "{0}"' -f $ServiceName Start-Process -FilePath "sc.exe" -ArgumentList $StartArgs -Wait -NoNewWindow } else { Write-Host "Dienst ist bereits aktuell installiert." } } } Catch { Write-Error "Kritischer Fehler bei der Installation: $_" } Stop-Transcript