8
0
Skriptentwickung/archive/ServiceController/ServiceController1.ps1
2024-01-24 16:42:38 +01:00

537 lines
47 KiB
PowerShell
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# PowerShell 4.0 Script
# EMC² QuickScan Job Script zur automatischen
# Abarbeitung von Scan Dateien.
# Digital Data
# Ludwig-Rinn-Strasse 16
# 35452 Heuchelheim
# Tel.: 0641 / 202360
# E-Mail: info@didalog.de
# Version 1.0
# Letzte Aktualisierung: 05.08.2014
# Mindestanforderung für dieses Skript:
# Microsoft Windows 7 SP1 / Server 2008 R2 SP1 -> siehe KB976932
# Microsoft .NET Framework 4.5 -> siehe KB2858728
# Microsoft PowerShell 4.0 -> siehe KB2819745
# WICHTIG: Falls sich dieses Skript nicht ausführen lässt,
# muss dieser PS-Befehl noch mit administrativen Rechten ausgeführt werden:
# set-executionpolicy unrestricted
#Requires Version 4.0
#-----------------------------------------------------------------------------------------------------
############################ Zusätzliche Assemblys hinzufügen bzw. laden. ############################
#-----------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------
######################################## Variablen definieren. #######################################
#-----------------------------------------------------------------------------------------------------
Set-Variable -Name ScriptName -Value (($MyInvocation.MyCommand.Name) -split "\.")[0].ToString() -Scope script
Set-Variable -Name ScriptPath -Value (Split-Path ($MyInvocation.MyCommand.Path)) -Scope script
Set-Variable -Name ConfigFile -Value "$ScriptPath\$ScriptName`_Settings.ini" -Scope script
Set-Variable -Name KonfigWerte -Value $NULL -Scope script
Set-Variable -Name ZeitStempel1 -Value $(Get-Date -Format 'ddMMyyyy_HHmmss') -Scope script
Set-Variable -Name Fehler -Value $NULL -Scope local
Set-Variable -Name LogDatei -Value "$ScriptName`_$ZeitStempel1.log" -Scope script
Set-Variable -Name LogEintrag -Value $NULL -Scope local
Set-Variable -Name LogPathListe -Value $NULL -Scope script
Set-Variable -Name LogPath -Value $NULL -Scope script
Set-Variable -Name LogPathEintrag -Value $NULL -Scope script
Set-Variable -Name LogKeepTime -Value 60 -Scope script
Set-Variable -Name ProzessTest -Value $NULL -Scope local
#-----------------------------------------------------------------------------------------------------
####################################### Funktionen definieren. #######################################
#-----------------------------------------------------------------------------------------------------
# Funktion um einen Pfad zu überprüfen und ggf. neu anzulegen, inkl. Fehlerbehandlung.
# Ruft Funktion "Func-Write-LogDatei -LogEintrag" auf, um Aktivitäten zu protokollieren.
Function Func-Pathüberprüfung
{
Param
(
[String]$Path
)
$PathTest = Test-Path -PathType Container $Path
Func-Write-LogDatei -LogEintrag "Checking Path $Path for existence."
IF ($PathTest -eq "True")
{
Func-Write-LogDatei -LogEintrag "Path $Path is already existence and can be used."
}
ELSE
{
Try
{
Func-Write-LogDatei -LogEintrag "Path $Path has to been created."
New-Item -Path $Path -ItemType directory -force -ErrorAction Stop
}
Catch
{
Func-Write-LogDatei -LogEintrag "ERROR: Unable to create Path."
Func-Write-LogDatei -LogEintrag "INFO: Maybe there is an access or rights Problem."
Func-Write-LogDatei -LogEintrag "Application was unplannd terminated."
EXIT
}
}
}
# Funktion um Logdateien zu schreiben.
Function Func-Write-LogDatei
{
Param
(
[string]$LogEintrag
)
# Der Fehlerindikator ($?) muss bereits zu Anfang abgefragt werden,
# da er von JEDEM funktionierenden Befehl wieder auf True gesetzt wird.
IF ($? -ne 'True')
{
Set-Variable -Name Fehler -Value 1
}
IF ($LogPath -eq $NULL)
{
Set-Variable -Name LogPathListe -Value @($LogPathListe)
Set-Variable -Name LogPathListe -Value ($LogPathListe += "$SkriptPath\Logs","$env:temp")
FOREACH ($LogPathEintrag in $LogPathListe)
{
$PathTest = Test-Path -PathType Container "$LogPathEintrag"
Write-Host "Pathüberprüfung: Prüfe ob LogPath bereits angelegt ist: $LogPathEintrag"
IF ($PathTest -eq "True")
{
Write-Host "Pathüberprüfung: LogPath $LogPathEintrag ist bereits angelegt!"
Set-Variable -Name LogPath -Value $LogPathEintrag
break
}
ELSE
{
Write-Host "Pathüberprüfung: LogPath $LogPathEintrag muss angelegt werden."
New-Item -Path $LogPathEintrag -ItemType directory -force -ErrorAction SilentlyContinue | Out-Null
$PathTest = Test-Path -PathType Container $LogPathEintrag
Write-Host "Pathüberprüfung: Prüfe ob Path erfolgreich angelegt werden konnte."
IF ($PathTest -eq "True")
{
Write-Host "Pathüberprüfung: Path $LogPathEintrag wurde erfolgreich angelegt."
Set-Variable -Name LogPath -Value $LogPathEintrag
break
}
ELSE
{
Write-Host "Pathüberprüfung: Path $LogPathEintrag konnte nicht angelegt werden."
Set-Variable -Name LogPath -Value $LogPathEintrag
}
}
}
}
ELSEIF ($LogPath -eq $env:temp)
{
Write-Warning "FEHLER: LogPath nicht zugreifbar oder nicht korrekt konfiguriert!"
Write-Warning "INFO: $LogPath - wird nun verwendet!"
Write-Warning "Programm wird trotzdem fortgesetzt."
}
Set-Variable -Name LogPath -Value $LogPath -scope script
Write-Host $LogEintrag
Add-content $LogPath\$LogDatei -value "$(Get-Date -Format 'dd.MM.yyyy')-$(Get-Date -Format 'HH:mm:ss'): $LogEintrag"
IF ($Fehler -eq 1)
{
Write-Host "Fehlermeldung: $error"
Add-content $LogPath\$LogDatei -value "$(Get-Date -Format 'dd.MM.yyyy')-$(Get-Date -Format 'HH:mm:ss'): Fehlermeldung: $error"
# Setze Fehlerspeicher zurück
$error.clear()
}
}
# Funktion um eine Textdatei (ASCII), komplett einzulesen.
Function Func-ReadConfigFile
{
Param
(
[String]$ConfigFile
)
Write-Host ""
Write-Host "Prüfe ob Konfigurationsdatei: $ConfigFile vorhanden ist."
$DateiTest = Test-Path -PathType Leaf $ConfigFile
IF ($DateiTest -eq "True")
{
Write-Host "Konfigurationsdatei ist vorhanden, fahre fort."
}
ELSE
{
Func-Write-LogDatei -LogEintrag "FEHLER: Konfigurationsdatei ist nicht vorhanden!"
Func-Write-LogDatei -LogEintrag "Programm wird ungeplant beendet."
EXIT
}
Write-Host "Konfigurationsdatei wird nun eingelesen."
Set-Variable -Name KonfigWerte -Value (Select-String -path $ConfigFile -pattern "=" | where {-not($_-match "#")})
IF ($KonfigWerte -eq $Null)
{
Write-Host "Keine gültigen Werte in Konfigurationsdatei erkannt!"
}
ELSE
{
Write-Host "Es wurden"$KonfigWerte.count"Zeilen aus der Konfigurationsdatei eingelesen."
#Write-Host "Folgende Werte wurden eingelesen:"
#Write-Host "$KonfigWerte"
}
Set-Variable -Name KonfigWerte -Value $KonfigWerte -Scope script
}
# Funktion um eine Textdatei (ASCII), komplett einzulesen.
# Gibt immer nur einen Wert zurück!
Function Func-ReadConfigValue
{
Param
(
[String]$KonfigBezeichner
)
$Funde = ($KonfigWerte.line | select-string -pattern "$KonfigBezeichner")
IF ($Funde -eq $NULL)
{
Write-Host "Der gesuchte Bezeichner ($KonfigBezeichner) konnte nicht gefunden werden!"
Write-Host "Standardwert wird verwendet!"
$KonfigBezeichner = (Get-Variable -Name $KonfigBezeichner -ValueOnly)
return $KonfigBezeichner
}
ELSEIF ($Funde.GetType() -like "*Microsoft.PowerShell.Commands.MatchInfo*" -or $Funde.GetType() -like "*String*")
{
$Funde = ($Funde -split "=")
$Funde = @($Funde)
$Funde = $Funde[1]
$Funde = ($Funde.TrimStart())
$Funde = ($Funde.TrimEnd())
return $Funde
}
ELSEIF ($Funde -is [Array])
{
return $funde
}
}
Function Func-Dateien-zusammensuchen
{
Param
(
[String]$SuchPath,
[Array]$SuchWhiteList,
[Array]$SuchBlackList
)
Set-Variable -Name SuchWhiteList -Value ($SuchWhiteList -Replace " ","")
Set-Variable -Name SuchWhiteList -Value ($SuchWhiteList -Split ",")
Set-Variable -Name SuchWhiteList -Value ($SuchWhiteList -Split ";")
Set-Variable -Name SuchBlackList -Value ($SuchBlackList -Replace " ","")
Set-Variable -Name SuchBlackList -Value ($SuchBlackList -Split ",")
Set-Variable -Name SuchBlackList -Value ($SuchBlackList -Split ";")
$Funde = Get-ChildItem -Path "$SuchPath" -include $SuchWhiteList -exclude $SuchBlackList
IF ($Funde -eq $NULL)
{
Func-Write-LogDatei -LogEintrag "Could not find any File(s) in Path: $SuchPath (regard on White and Black Lists)."
}
ELSE
{
Func-Write-LogDatei -LogEintrag "Could find some File(s) in Path: $SuchPath (regard on White and Black Lists) ..."
FOREACH ($Fund in $Funde)
{
Func-Write-LogDatei -LogEintrag "...one Found is: $Fund"
}
return $Funde
}
}
#-----------------------------------------------------------------------------------------------------
####################################### Vorbereitende Arbeiten. ######################################
#-----------------------------------------------------------------------------------------------------
# Lösche evtl. anzeigen.
Clear-Host
# Konfigurationsdatei komplett einlesen.
Func-ReadConfigFile -ConfigFile $ConfigFile
# Werte aus Konfigurationsdatei bereitstellen.
$LogPathListe = (Func-ReadConfigValue -KonfigBezeichner LogPath)
$LogKeepTime = (Func-ReadConfigValue -KonfigBezeichner LogKeepTime)
$SCStartListe = (Func-ReadConfigValue -KonfigBezeichner SCStart)
$SCRestartListe = (Func-ReadConfigValue -KonfigBezeichner SCRestart)
$SCStopListe = (Func-ReadConfigValue -KonfigBezeichner SCStop)
FOREACH ($SCStartZeile in $SCStartListe)
{
Write-Host ""
Write-Host "Folgende Zeilen wurden ausgelesen: --> $SCStartZeile <--"
$Zähler++
IF ($SCStartZeile -is [String])
{
$SCStartZeile = @($SCStartZeile)
$SCStartZeile = ($SCStartZeile -split ";")
$SCStartZeile = ($SCStartZeile -split ",")
}
ELSE
{
$SCStartZeile = @($SCStartZeile)
$SCStartZeile = ($SCStartZeile -Split "=" )
$SCStartZeile = ($SCStartZeile[1])
$SCStartZeile = ($SCStartZeile -split ";")
$SCStartZeile = ($SCStartZeile -split ",")
}
[System.Collections.ArrayList]$SCStartWerte = @()
# Werte in Array werden auseinandergenommen und neu zusammengefügt, um Leerzeichen zu entfernen.
FOREACH ($SCStartWert in $SCStartZeile)
{
$SCStartWert = ($SCStartWert.TrimStart())
$SCStartWert = ($SCStartWert.TrimEnd())
$SCStartWerte.Add($SCStartWert)
}
IF ($SCStartWerte -eq $NULL)
{
Write-Host "Der erwarteter Wert ist leer!"
Write-Host "Standardwert wird eingetragen!"
Remove-Variable $SCStartWerte -Force
Remove-Variable $SCStartZeile -Force
#Set-Variable -Name ("OutputRegel"+$Zähler) -value "Standard" -Scope script
}
ELSE
{
Write-Host ("SCStart"+$Zähler) wurde als Variable gesetzt und hat folgende Werte bekommen: $SCStartWerte
Set-Variable -Name ("SCStart"+$Zähler) -value $SCStartWerte -Scope script
}
}
FOREACH ($SCRestartZeile in $SCRestartListe)
{
Write-Host ""
Write-Host "Folgende Zeilen wurden ausgelesen: --> $SCStopZeile <--"
$Zähler++
IF ($SCRestartZeile -is [String])
{
$SCRestartZeile = @($SCRestartZeile)
$SCRestartZeile = ($SCRestartZeile -split ";")
$SCRestartZeile = ($SCRestartZeile -split ",")
}
ELSE
{
$SCRestartZeile = @($SCRestartZeile)
$SCRestartZeile = ($SCRestartZeile -Split "=" )
$SCRestartZeile = ($SCRestartZeile[1])
$SCRestartZeile = ($SCRestartZeile -split ";")
$SCRestartZeile = ($SCRestartZeile -split ",")
}
[System.Collections.ArrayList]$SCRestartWerte = @()
# Werte in Array werden auseinandergenommen und neu zusammengefügt, um Leerzeichen zu entfernen.
FOREACH ($SCRestartWert in $SCRestartZeile)
{
$SCRestartWert = ($SCRestartWert.TrimStart())
$SCRestartWert = ($SCRestartWert.TrimEnd())
$SCRestartWerte.Add($SCRestartWert)
}
IF ($SCRestartWerte -eq $NULL)
{
Write-Host "Der erwarteter Wert ist leer!"
Write-Host "Standardwert wird eingetragen!"
Remove-Variable $SCRestartWerte -Force
Remove-Variable $SCRestartZeile -Force
#Set-Variable -Name ("OutputRegel"+$Zähler) -value "Standard" -Scope script
}
ELSE
{
Write-Host ("SCRestart"+$Zähler) wurde als Variable gesetzt und hat folgende Werte bekommen: $SCRestartWerte
Set-Variable -Name ("SCRestart"+$Zähler) -value $SCRestartWerte -Scope script
}
}
FOREACH ($SCStopZeile in $SCStopListe)
{
Write-Host ""
Write-Host "Folgende Zeilen wurden ausgelesen: --> $SCStopZeile <--"
$Zähler++
IF ($SCRestartZeile -is [String])
{
$SCStopZeile = @($SCStopZeile)
$SCStopZeile = ($SCStopZeile -split ";")
$SCStopZeile = ($SCStopZeile -split ",")
}
ELSE
{
$SCStopZeile = @($SCStopZeile)
$SCStopZeile = ($SCStopZeile -Split "=" )
$SCStopZeile = ($SCStopZeile[1])
$SCStopZeile = ($SCStopZeile -split ";")
$SCStopZeile = ($SCStopZeile -split ",")
}
[System.Collections.ArrayList]$SCRestartWerte = @()
# Werte in Array werden auseinandergenommen und neu zusammengefügt, um Leerzeichen zu entfernen.
FOREACH ($SCStopWert in $SCStopZeile)
{
$SCStopWert = ($SCStopWert.TrimStart())
$SCStopWert = ($SCStopWert.TrimEnd())
$SCStopWerte.Add($SCStopWert)
}
IF ($SCStopRestartWerte -eq $NULL)
{
Write-Host "Der erwarteter Wert ist leer!"
Write-Host "Standardwert wird eingetragen!"
Remove-Variable $SCStopWerte -Force
Remove-Variable $SCStopZeile -Force
#Set-Variable -Name ("OutputRegel"+$Zähler) -value "Standard" -Scope script
}
ELSE
{
Write-Host ("SCStop"+$Zähler) wurde als Variable gesetzt und hat folgende Werte bekommen: $SCStopWerte
Set-Variable -Name ("SCStop"+$Zähler) -value $SCStopWerte -Scope script
}
}
#-----------------------------------------------------------------------------------------------------
####################################### Hauptprogramm starten. #######################################
#-----------------------------------------------------------------------------------------------------
Write-Host ""
Func-Write-LogDatei -LogEintrag "*******************************************************************************************"
Func-Write-LogDatei -LogEintrag "Program Startup: $ScriptName on $env:computername from Account $env:USERDOMAIN\$env:USERNAME."
Func-Write-LogDatei -LogEintrag "*******************************************************************************************"
IF ($($SCStartListe.count) -gt 0)
{
Func-Write-LogDatei -LogEintrag "There are $($SCStartListe.count) out of max. 9 SCStart Entrys, processing by now."
# Zähler zurücksetzen
$Zähler = $NULL
# Schleife für jede Eintrag bzw. Zeile in der Konfigurationsdatei wird eingeleitet.
FOREACH ($SCStart in $(get-variable -Name SCStart?))
{
$Zähler++ | out-null
Func-Write-LogDatei -LogEintrag ""
Func-Write-LogDatei -LogEintrag "--> Verarbeitung von $((Get-Variable -name ("SCStart"+$Zähler)).name) wird gestartet."
Func-Write-LogDatei -LogEintrag "Folgende Werte wurden übergeben: $(get-variable -name ("SCStart"+$Zähler) -ValueOnly)."
Try
{
$ProcessTest = Get-Service -Name $(get-variable -name ("SCStart"+$Zähler) -ValueOnly)
IF ($Processtest.Status -eq "Running")
{
Func-Write-LogDatei -LogEintrag "Service - $($ProcessTest.Name) - is currently Running."
Func-Write-LogDatei -LogEintrag "Can not start it because it is already running."
}
ELSEIF ($Processtest.Status -eq "Stopped")
{
Func-Write-LogDatei -LogEintrag "Service - $($ProcessTest.Name) - is currently stopped."
Func-Write-LogDatei -LogEintrag "Starting Service now."
Start-Service $ProcessTest.Start()
}
ELSE
{
Func-Write-LogDatei -LogEintrag "Can not deal with Service - $($Processtest.Status)."
}
}
Catch
{
Func-Write-LogDatei -LogEintrag "Service does not exist or access denied!"
}
}
}
#-----------------------------------------------------------------------------------------------------
####################################### Abschließende Arbeiten. ######################################
#-----------------------------------------------------------------------------------------------------
# Löschen alter Log-Dateien.
Func-Write-LogDatei -LogEintrag ""
Func-Write-LogDatei -LogEintrag "-------------------------------------------------------------------------------------------"
Func-Write-LogDatei -LogEintrag "Checking for expiered Log Files."
Func-Write-LogDatei -LogEintrag "-------------------------------------------------------------------------------------------"
IF ($LogKeepTime -gt 0)
{
Func-Write-LogDatei -LogEintrag "Log Files should be removed which are older than $LogKeepTime Day(s)."
$Funde = (Func-Dateien-zusammensuchen -SuchPath "$LogPath\*" -SuchWhiteList *.log | where {$_.Name -like "*$SkriptName*" -and $_.lastwritetime -lt $((Get-Date).AddDays(-$LogKeepTime)) -and -not $_.psiscontainer})
IF ($Funde -eq $null)
{
Func-Write-LogDatei -LogEintrag "Keine zu löschenden Log-Dateien vorhanden."
}
ELSE
{
Func-Write-LogDatei -LogEintrag "Dateien gefunden, folgende Log-Dateien werden aufgund ihres alters gelöscht:"
FOREACH ($Fund in $Funde)
{
Func-Write-LogDatei -LogEintrag "Log-Datei: $Fund wird entfernt."
Remove-Item -Path $Fund -Force -Verbose
}
}
}
ELSE
{
Func-Write-LogDatei -LogEintrag "Sie haben in der Konfigurationsdatei angegeben, das Log Dateien nicht automatisch gelöscht werden sollen!"
}
Func-Write-LogDatei -LogEintrag ""
Func-Write-LogDatei -LogEintrag "*******************************************************************************************"
Func-Write-LogDatei -LogEintrag "Program Finish: $ScriptName on $env:computername from Account $env:USERDOMAIN\$env:USERNAME."
Func-Write-LogDatei -LogEintrag "*******************************************************************************************"
# Definierte Variablen wieder löschen, damit sie nicht im Arbeitsspeicher verbleiben.
$error.clear()