# Scripted by Jörn Walter 02.01.2017
# https://www.windows-papst.de
write-host “”
write-host “Scripted by Jörn Walter 02.01.2017”
write-host “https://www.windows-papst.de”
write-host “”
Write-Host “Lese .ini Datei ein” -ForegroundColor Green;
Get-Content “$PSScriptRoot\service.ini” | ForEach-Object -Begin {$settings=@{}} -Process {$store = [regex]::split($_,’=’); if(($store[0].CompareTo(“”) -ne 0) -and ($store[0].StartsWith(“[“) -ne $True) -and ($store[0].StartsWith(“#”) -ne $True)) {$settings.Add($store[0], $store[1])}}
Write-Host “Verarbeite Parameter…” -ForegroundColor Green;
Write-Host “”
$Param1 = $settings.Get_Item(“Service”)
$Param2 = $settings.Get_Item(“Mail”)
$Param3 = $settings.Get_Item(“MailFrom”)
$Param4 = $settings.Get_Item(“SMTP”)
$Param5 = $settings.Get_Item(“Subject”)
$Servername = $env:computername
#$Displayname = Get-Service $Param1 | select -ExpandProperty Displayname
Write-Host “Service: $Param1” -ForegroundColor Yellow;
Write-Host “Mail: $Param2” -ForegroundColor Yellow;
Write-Host “MailFrom: $Param3” -ForegroundColor Yellow;
Write-Host “SMTP: $Param4” -ForegroundColor Yellow;
Write-Host “Subject: $Param5`n” -ForegroundColor Yellow;
if ((Get-Service | Where-Object {$_.Name -eq $Param1}).length -eq 0) {
Write-Host “Der Dienst $Param1 ist nicht vorhanden.” -ForegroundColor Red
Send-MailMessage -From $Param3 -To $Param2 -Subject $Param5 -Body “Der zu überwachende Dienst auf Server $Servername ist nicht vorhanden” -BodyAsHTML -SmtpServer $Param4 -encoding ([System.Text.Encoding]::UTF8)
exit}
$running = Get-Service -Name $Param1
if ($running.status -eq “Running”) {
write-host “Der $Param1 Service läuft doch 😉 …”
exit
} else {
Start-Service -Name $Param1 }
write-host “Der $Param1 Service wurde automatisiert nach gestartet”
Send-MailMessage -From $Param3 -To $Param2 -Subject $Param5 -Body “Der $Param1 Service wurde automatisiert nach gestartet” -BodyAsHTML -SmtpServer $Param4 -encoding ([System.Text.Encoding]::UTF8)
.ini Datei:
[Param1] Service=8Man-Service
[Param2] Mail=joern@ndsedv
[Param3] MailFrom=service@ndsedv.de
[Param4]
SMTP=172.18.32.44
[Param5] Subject=Service-Überwachung