From d45418681575d97f939d552a725c2c9ad3a9eb44 Mon Sep 17 00:00:00 2001 From: Johannes Rest Date: Fri, 30 Jan 2026 21:50:57 +0100 Subject: [PATCH] Add PS SFTP/Fileshare check script and git hygiene files --- .gitattributes | 8 +++ .gitignore | 31 ++++++++++ ps_check_sftp_fs.ps1 | 134 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 ps_check_sftp_fs.ps1 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..e7adce4 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,8 @@ +# Normalize line endings +* text=auto + +# PowerShell scripts: treat as text and ensure LF in repo +*.ps1 text eol=lf +*.psm1 text eol=lf +*.psd1 text eol=lf + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..09c126d --- /dev/null +++ b/.gitignore @@ -0,0 +1,31 @@ +# --- OS / Editor --- +.DS_Store +Thumbs.db + +# VS Code / JetBrains +.vscode/ +.idea/ +*.swp + +# --- PowerShell / temp --- +*.tmp +*.temp +*.bak + +# --- Logs & runtime output --- +*.log +Logs/ +logs/ + +# --- WinSCP / script output (falls du mal Output umleitest) --- +WinSCP.log + +# --- Windows build/artifacts --- +bin/ +obj/ + +# --- IMPORTANT: ensure PS scripts are not ignored by accident --- +!*.ps1 +!*.psm1 +!*.psd1 + diff --git a/ps_check_sftp_fs.ps1 b/ps_check_sftp_fs.ps1 new file mode 100644 index 0000000..0924d1f --- /dev/null +++ b/ps_check_sftp_fs.ps1 @@ -0,0 +1,134 @@ +<# +.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 -------------------"