286 lines
26 KiB
PowerShell
286 lines
26 KiB
PowerShell
# PowerShell 2.0 Script
|
||
# Datei finden und ausführen
|
||
|
||
# Digital Data
|
||
# Ludwig-Rinn-Strasse 16
|
||
# 35452 Heuchelheim
|
||
# Tel.: 0641 / 202360
|
||
# E-Mail: info@didalog.de
|
||
|
||
# Version 1.0
|
||
# Letzte Aktualisierung: 25.05.2014
|
||
|
||
# Mindestanforderung für dieses Skript:
|
||
# Microsoft Windows XP SP3 / Server 2008 R2 SP1 -> siehe KB976932
|
||
# Microsoft .NET Framework 4.5 -> siehe KB2858728
|
||
# Microsoft PowerShell 2.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 2.0
|
||
|
||
#-----------------------------------------------------------------------------------------------------
|
||
############################ Zusätzliche Assemblys hinzufügen bzw. laden. ############################
|
||
#-----------------------------------------------------------------------------------------------------
|
||
|
||
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
|
||
|
||
#-----------------------------------------------------------------------------------------------------
|
||
######################################## Variablen definieren. #######################################
|
||
#-----------------------------------------------------------------------------------------------------
|
||
|
||
New-Variable -Name SkriptName -Value $MyInvocation.MyCommand.Name -Scope global -Description "Name dieser Datei, wird verwendet, für Erstellung von Logs."
|
||
New-Variable -name timestamp -Value $(Get-Date -Format 'ddMMyyyy_hhmmss') -Scope global -Description "Datum wird in einem gewissen Format aufbereitet und dann Log-Dateien anhangen."
|
||
New-Variable -Name LogPath -Value ".\Logs" -Scope global -Description "Pfad für Log Dateien. Kann sich während der Laufzeit ändern!"
|
||
New-Variable -Name LogPathTest -Value $NULL -Scope local -Description "Pfad für Log Dateien. Kann sich während der Laufzeit ändern!"
|
||
New-Variable -name Logfile -Value "$SkriptName`_$timestamp.log" -Scope global -Description "Name für Log-Dateien."
|
||
New-Variable -name LogEntfernen -Value $((Get-Date).AddDays(-1)) -Scope global -Description "Zahlenwert, wie lange Log-Dateien aufbewahrt werden sollen z.B. -30 = 30 Tage aufbewahren."
|
||
New-Variable -Name Argument1 -Value $args[0] -Scope global -Description "Erster Aufrufparameter, der diesem Skript übergeben wurde"
|
||
New-Variable -Name Argument2 -Value $args[1] -Scope global -Description "Zweiter Aufrufparameter, der diesem Skript übergeben wurde"
|
||
New-Variable -Name Argument3 -Value $args[2] -Scope global -Description "Dritter Aufrufparameter, der diesem Skript übergeben wurde"
|
||
New-Variable -Name Fund -Value $NULL -Scope local -Description "Initialisiere Variable, für spätere Verwendung."
|
||
New-Variable -Name Abfrage -Value $NULL -Scope local -Description "Initialisiere Variable, für spätere Verwendung."
|
||
New-Variable -Name Path -Value $NULL -Scope local -Description "Initialisiere Variable, für spätere Verwendung."
|
||
New-Variable -Name PathTest -Value $NULL -Scope local -Description "Initialisiere Variable, für spätere Verwendung."
|
||
New-Variable -Name PathError -Value $NULL -Scope global -Description "Initialisiere Variable, für spätere Verwendung."
|
||
New-Variable -Name logstring -Value $NULL -Scope local -Description "Initialisiere Variable, für spätere Verwendung."
|
||
New-Variable -Name Drive -Value $NULL -Scope global -Description "Initialisiere Variable, für spätere Verwendung."
|
||
New-Variable -Name LocalDrives -Value $NULL -Scope global -Description "Initialisiere Variable, für spätere Verwendung."
|
||
|
||
#-----------------------------------------------------------------------------------------------------
|
||
####################################### Funktionen definieren. #######################################
|
||
#-----------------------------------------------------------------------------------------------------
|
||
Write-Host $Array
|
||
# Funktion um einen Pfad zu überprüfen und ggf. neu anzulegen, inkl. Fehlerbehandlung.
|
||
Function CheckPath
|
||
{
|
||
Param
|
||
(
|
||
[Parameter(Mandatory=$true)]
|
||
[String]$Path
|
||
)
|
||
|
||
$PathTest = Test-Path -PathType Container $Path
|
||
Write-Host "Prüfe ob Pfad bereits angelegt ist:" $Path
|
||
IF ($PathTest -eq "True")
|
||
{
|
||
Write-Host "Pfad: " $Path " ist bereits angelegt."
|
||
Set-Variable -Name PathError -Value 0 -scope global
|
||
#get-acl $Path
|
||
return
|
||
}
|
||
ELSE
|
||
{
|
||
Write-Host "Pfad: " $Path " muss angelegt werden."
|
||
New-Item -Path $Path -ItemType directory -ErrorVariable $PathError -ErrorAction SilentlyContinue | Out-Null
|
||
$PathTest = Test-Path -PathType Container $Path
|
||
Write-Host "Prüfe ob Pfad erfolgreich angelegt werden konnte."
|
||
IF ($PathTest -eq "True")
|
||
{
|
||
Write-Host "Pfad: " $Path " wurde erfolgreich angelegt."
|
||
Set-Variable -Name PathError -Value 0 -scope global
|
||
return
|
||
}
|
||
ELSE
|
||
{
|
||
Write-Host "Pfad: " $Path " konnte nicht angelegt werden."
|
||
Set-Variable -Name Path -Value $env:TEMP -scope global
|
||
Write-Host $env:TEMP "wird nun verwendet."
|
||
Set-Variable -Name PathError -Value 1 -scope global
|
||
return
|
||
}
|
||
}
|
||
}
|
||
|
||
# Funktion um Logdateien zu schreiben.
|
||
Function Write-LogFile
|
||
{
|
||
Param
|
||
(
|
||
#[Parameter(Mandatory=$true)]
|
||
[string]$logstring
|
||
)
|
||
IF ($PathError -eq $Null)
|
||
{
|
||
CheckPath -Path $LogPath
|
||
return
|
||
}
|
||
ELSEIF ($PathError -eq 1)
|
||
{
|
||
Set-Variable -Name LogPath -value $Path -scope global
|
||
return
|
||
}
|
||
Add-content $LogPath\$Logfile -value $logstring
|
||
}
|
||
|
||
# Funktion für eine VBScript-typische Messagebox.
|
||
Function Show-MsgBox
|
||
{
|
||
Param
|
||
(
|
||
[Parameter(Mandatory=$true)]
|
||
[String]$Text,
|
||
[String]$Title = 'Message',
|
||
[String]$Icon = 'YesNo,Information'
|
||
)
|
||
|
||
# Add-Type -AssemblyName 'Microsoft.VisualBasic'
|
||
Write-LogFile -logstring "MessageBox-Inhalt: $Text"
|
||
[Microsoft.VisualBasic.Interaction]::MsgBox($text, $icon, $title)
|
||
}
|
||
|
||
# Funktion für eine VBScript-typische Inputbox.
|
||
Function Show-Inputbox
|
||
{
|
||
Param
|
||
(
|
||
[Parameter(Mandatory=$true)]
|
||
[String]$Text,
|
||
[String]$Example,
|
||
[String]$Title
|
||
)
|
||
Write-LogFile -logstring "InputBox-Inhalt: $Text"
|
||
# Add-Type -AssemblyName 'Microsoft.VisualBasic'
|
||
[Microsoft.VisualBasic.Interaction]::Inputbox($Text, $Title, $Example)
|
||
}
|
||
|
||
#-----------------------------------------------------------------------------------------------------
|
||
####################################### Vorbereitende Arbeiten. ######################################
|
||
#-----------------------------------------------------------------------------------------------------
|
||
|
||
# Erster Aurrufparameter wird überprüft.
|
||
IF ($Argument1 -eq $NULL)
|
||
{
|
||
Show-MsgBox ("Es wurde kein Aufrufparameter mitgegeben! $([System.Environment]::NewLine)Der Aufruf muss wie folgt aussehen: $([System.Environment]::NewLine) $([System.Environment]::NewLine) powershell.exe $SkriptName 'GesuchtesProgramm.exe' 'Pfad\zum\Programm'") -Title 'Aufrufparameter fehlt!' -Icon 'OKOnly,Critical'
|
||
$Abfrage=Show-MsgBox -Text 'Wollen Sie nun einen eingeben?' -Title 'Aufrufparameter fehlt!' -Icon 'YesNo,Question'
|
||
#Write-LogFile 'Es wurde kein Aufrufparameter mitgegeben, wollen Sie nun einen eingeben?'
|
||
IF ($Abfrage -eq "YES" )
|
||
{
|
||
$Argument1 = Show-Inputbox -Text 'Bitte das gesuchte Programm eingeben:' -Example 'Beispielprogramm.exe' -Title 'Aufrufparameter eingeben!'
|
||
IF (($Argument1 -eq '') -OR ($Argument1 -like '*Beispielprogramm.exe*'))
|
||
{
|
||
Show-MsgBox -Text 'Die Eingabe war ungültig, Programm wird geschlossen.' -Title 'Aufrufparameter ungültig!' -Icon 'OKOnly,Critical'
|
||
Write-LogFile "Die Eingabe war ungültig, Programm wird geschlossen."
|
||
EXIT
|
||
}
|
||
ELSE
|
||
{
|
||
Write-LogFile "Der erste Aufrufparameter wurde manuell eingegeben und lautet nun: $Argument1"
|
||
}
|
||
}
|
||
ELSE
|
||
{
|
||
Show-MsgBox -Text 'Programm wird beendet, bitte Aufrufparameter kontrollieren!' -Title 'Aufruffehler:' -Icon 'OKOnly,Information'
|
||
Write-LogFile "Programm wird aufgrund fehlender Aufrufparameter beendet."
|
||
EXIT
|
||
}
|
||
}
|
||
ELSE
|
||
{
|
||
Write-LogFile "Der erste Aufrufparameter lautet: $Argument1"
|
||
}
|
||
|
||
#-----------------------------------------------------------------------------------------------------
|
||
####################################### Hauptprogramm starten. #######################################
|
||
#-----------------------------------------------------------------------------------------------------
|
||
|
||
|
||
# Zweiter Aufrufparameter wird überprüft.
|
||
IF ($Argument2 -eq $NULL)
|
||
{
|
||
Write-LogFile "Der zweite Aufrufparameter fehlt, lokale Laufwerke werden durchsucht."
|
||
# Ermittle lokale Laufwerke
|
||
$LocalDrives = [System.IO.DriveInfo]::getdrives() | Where-Object {$_.DriveType -eq 'Fixed'}
|
||
FOREACH ($Drive in $LocalDrives)
|
||
{
|
||
# Suche Datei in Ordner (und Unterordnern) und führe sie aus.
|
||
$Fund = Get-ChildItem –Path $Drive"\*" -recurse –Include $Argument1 -force -ErrorAction SilentlyContinue
|
||
IF ($Fund -like "*$Argument1")
|
||
{
|
||
Write-LogFile "Datei gefunden unter: $Fund"
|
||
IF ($Argument3 -eq $null)
|
||
{
|
||
# Starte Programm ohne zusätlichen Aufrufparameter.
|
||
start-process -FilePath $Fund
|
||
}
|
||
ELSE
|
||
{
|
||
# Starte Programm mit zusätlichen Aufrufparameter.
|
||
start-process -FilePath $Fund -Argumentlist $Argument3
|
||
}
|
||
|
||
Write-LogFile "Programm wurde gestartet!"
|
||
return
|
||
}
|
||
ELSE
|
||
{
|
||
Show-MsgBox -Text "Die angebene Datei '$Argument1' konnte in/auf '$Drive' nicht gefunden werden, Programm wird beendet!" -Title 'Aufruffehler:' -Icon 'OKOnly,Information'
|
||
Write-LogFile "Die angebene Datei '$Argument1' konnte in/auf '$Drive' nicht gefunden werden, Programm wird beendet!"
|
||
}
|
||
}
|
||
}
|
||
ELSE
|
||
{
|
||
Write-LogFile "Der zweite Aufrufparameter lautet: $Argument2"
|
||
# Suche Datei in Ordner (und Unterordnern) und führe sie aus.
|
||
$Fund = Get-ChildItem –Path $Argument2"\*" -recurse –Include $Argument1 -force -ErrorAction SilentlyContinue
|
||
IF ($Fund -like "*$Argument1")
|
||
{
|
||
Write-LogFile "Datei gefunden unter: $Fund"
|
||
IF ($Argument3 -eq $null)
|
||
{
|
||
# Starte Programm ohne zusätlichen Aufrufparameter.
|
||
Start-Process -FilePath $Fund
|
||
}
|
||
ELSE
|
||
{
|
||
# Starte Programm mit zusätlichen Aufrufparameter.
|
||
Start-Process -FilePath $Fund -Argumentlist $Argument3
|
||
}
|
||
Write-LogFile "Programm wurde gestartet!"
|
||
return
|
||
}
|
||
ELSE
|
||
{
|
||
Write-LogFile "Die angebene Datei '$Argument1' konnte in/auf '$Argument2' nicht gefunden werden, lokale Laufwerke werden nun durchsucht!"
|
||
# Ermittle lokale Laufwerke
|
||
$LocalDrives = [System.IO.DriveInfo]::getdrives() | Where-Object {$_.DriveType -eq 'Fixed'}
|
||
FOREACH ($Drive in $LocalDrives)
|
||
{
|
||
# Suche Datei in Ordner (und Unterordnern) und führe sie aus.
|
||
$Fund = Get-ChildItem –Path $Drive"\*" -recurse –Include $Argument1 -force -ErrorAction SilentlyContinue
|
||
IF ($Fund -like "*$Argument1")
|
||
{
|
||
Write-LogFile "Datei gefunden unter: $Fund"
|
||
IF ($Argument3 -eq $null)
|
||
{
|
||
# Starte Programm ohne zusätlichen Aufrufparameter.
|
||
Start-Process -FilePath $Fund
|
||
}
|
||
ELSE
|
||
{
|
||
# Starte Programm mit zusätlichen Aufrufparameter.
|
||
Start-Process -FilePath $Fund -Argumentlist $Argument3
|
||
}
|
||
Write-LogFile "Programm wurde gestartet!"
|
||
return
|
||
}
|
||
ELSE
|
||
{
|
||
Show-MsgBox -Text "Die angebene Datei '$Argument1' konnte in/auf '$Drive' nicht gefunden werden, Programm wird beendet!" -Title 'Aufruffehler:' -Icon 'OKOnly,Information'
|
||
Write-LogFile "Die angebene Datei '$Argument1' konnte in/auf '$Drive' nicht gefunden werden, Programm wird beendet!"
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
#-----------------------------------------------------------------------------------------------------
|
||
####################################### Abschließende Arbeiten. ######################################
|
||
#-----------------------------------------------------------------------------------------------------
|
||
|
||
# Löschen alter Log-Dateien.
|
||
Write-Host "test ende"
|
||
Write-LogFile "Folgende Dateien werden gelöscht: "(get-childitem “$LogPath\$SkriptName*.log” | where {$_.lastwritetime -lt $LogEntfernen -and -not $_.psiscontainer} | Format-List )
|
||
get-childitem “$LogPath\$SkriptName*.log” | where {$_.lastwritetime -lt $LogEntfernen -and -not $_.psiscontainer} |% {remove-item $_.fullname -force -verbose } |