<# .SYNOPSIS Testet SFTP-Endpunkte und Fileshares, schreibt Ergebnisse in ein Tageslog. .DESCRIPTION Dieses Skript überprüft die Erreichbarkeit von SFTP-Servern (über WinSCP) und prüft, ob bestimmte Fileshare-Dateien vorhanden sind. Es schreibt alle Logs in ein Tageslog (neuer Log pro Tag). .NOTES - Läuft unter Windows Server 2019 PowerShell - WinSCP muss installiert sein: https://winscp.net/eng/download.php - Keine Dateien werden auf den Endpunkten erstellt oder verändert. #> # ------------------- Konfiguration ------------------- $LogFolder = "C:\Logs\SFTP_FS_Check" # Ordner für Logfiles $SftpServers = @( @{Host="sftp.example.com"; Port=22; User="username"; Password="password"}, @{Host="sftp2.example.com"; Port=22; User="user2"; Password="secret2"} ) $FileShares = @( "\\server1\share\*.csv", "\\server2\reports\*.txt" ) # Erstelle Log-Ordner, falls nicht vorhanden if (!(Test-Path $LogFolder)) { try { New-Item -Path $LogFolder -ItemType Directory -Force | Out-Null } catch { Write-Error "Konnte Log-Ordner $LogFolder nicht erstellen: $_" exit 1 } } # Logdatei für den aktuellen Tag $LogFile = Join-Path $LogFolder ("CheckLog_" + (Get-Date -Format "yyyy-MM-dd") + ".log") function Write-Log { param( [string]$Message, [string]$Level = "INFO" ) $TimeStamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $Entry = "$TimeStamp [$Level] $Message" Write-Output $Entry try { Add-Content -Path $LogFile -Value $Entry } catch { Write-Error "Fehler beim Schreiben ins Logfile: $_" } } # ------------------- SFTP-Test ------------------- function Test-SftpServer { param( [string]$Host, [int]$Port = 22, [string]$User, [string]$Password ) $WinScpPath = "C:\Program Files (x86)\WinSCP\WinSCP.exe" if (!(Test-Path $WinScpPath)) { Write-Log "WinSCP nicht gefunden unter $WinScpPath" "ERROR" return $false } # WinSCP-Skript temporär erstellen $SftpScript = @" option batch abort option confirm off open sftp://$User:$Password@$Host:$Port/ -hostkey=* ls exit "@ $TempScriptPath = [System.IO.Path]::GetTempFileName() try { Set-Content -Path $TempScriptPath -Value $SftpScript -Force $Process = Start-Process -FilePath $WinScpPath -ArgumentList "/script=`"$TempScriptPath`"" -Wait -PassThru -NoNewWindow -ErrorAction Stop if ($Process.ExitCode -eq 0) { Write-Log "SFTP $Host erreichbar." "INFO" return $true } else { Write-Log "SFTP $Host nicht erreichbar. ExitCode=$($Process.ExitCode)" "ERROR" return $false } } catch { Write-Log "Fehler beim Test von SFTP $Host: $_" "ERROR" return $false } finally { # Temp-Datei löschen Remove-Item $TempScriptPath -ErrorAction SilentlyContinue } } # ------------------- Fileshare-Test ------------------- function Test-FileShare { param( [string]$PathPattern ) try { $Files = Get-ChildItem -Path $PathPattern -ErrorAction Stop if ($Files.Count -gt 0) { Write-Log "Files gefunden für $PathPattern: $($Files.Count)" "INFO" return $true } else { Write-Log "Keine Files gefunden für $PathPattern" "WARNING" return $false } } catch { Write-Log "Fehler beim Zugriff auf $PathPattern: $_" "ERROR" return $false } } # ------------------- Hauptlogik ------------------- Write-Log "------------------- Starte Prüfung -------------------" # SFTP-Server prüfen foreach ($sftp in $SftpServers) { Test-SftpServer -Host $sftp.Host -Port $sftp.Port -User $sftp.User -Password $sftp.Password } # Fileshares prüfen foreach ($share in $FileShares) { Test-FileShare -PathPattern $share } Write-Log "------------------- Prüfung beendet -------------------"