This commit is contained in:
Madzone
2026-10-13 15:13:34 +02:00
parent 3bc149a3e6
commit c286614cfd
4 changed files with 98 additions and 50 deletions
+38 -27
View File
@@ -13,7 +13,8 @@ function Initialize-Configuration {
$script:ScriptFolder = 'C:\SCAN\SCRIPT'
$script:SourcePath = 'C:\SCAN\NDTRACK'
$script:LinuxSourcePath = 'C:\SCAN\NDTRACK'
$script:DestinationPath = '' # Leer = Systemlaufwerk des jeweiligen Zielservers + \ndtrack
$script:DestinationPath = '' # Leer = Systemlaufwerk automatisch per WMI/DCOM ermitteln
$script:ServerDestinationPaths = @{} # Optional: @{ 'sql01.firma.example' = 'D:\ndtrack' }
$script:TaskName = 'NDTRACK'
$script:WindowsLauncher = 'RunNdTrackWindows_Local.cmd'
$script:LinuxLauncher = 'RunNdTrackLinuxUnix_Local.sh'
@@ -131,18 +132,31 @@ function GetLinuxServer {
function Resolve-WindowsFolder {
param([string]$Server)
Assert-ServerName $Server
Invoke-Command -ComputerName $Server -ErrorAction Stop -ScriptBlock {
param($configured)
$ErrorActionPreference = 'Stop'
$folder = $configured
if (-not $folder) {
if ($env:SystemDrive -notmatch '^[A-Za-z]:$') { throw 'Systemlaufwerk nicht ermittelbar.' }
$folder = $env:SystemDrive + '\ndtrack'
$folder = $script:DestinationPath
$overrides = Get-Variable -Name ServerDestinationPaths -Scope Script -ErrorAction SilentlyContinue
if ($overrides -and $overrides.Value.ContainsKey($Server)) { $folder = $overrides.Value[$Server] }
if (-not $folder) {
$cimSession = $null
try {
$options = New-CimSessionOption -Protocol Dcom -ErrorAction Stop
$cimSession = New-CimSession -ComputerName $Server -SessionOption $options -ErrorAction Stop
$os = Get-CimInstance -CimSession $cimSession -ClassName Win32_OperatingSystem -Property SystemDrive -ErrorAction Stop
$drive = [string]$os.SystemDrive
if ($drive -notmatch '^[A-Za-z]:$') { throw 'Kein gueltiges Systemlaufwerk vom Zielserver erhalten.' }
$folder = $drive + '\ndtrack'
} catch {
throw "Systemlaufwerk von '$Server' konnte per WMI/DCOM nicht ermittelt werden: $($_.Exception.Message)"
} finally {
if ($null -ne $cimSession) { Remove-CimSession -CimSession $cimSession -ErrorAction SilentlyContinue }
}
if ($folder -notmatch '^[A-Za-z]:\\[^"\r\n]+$' -or $folder -match '(?:^|\\)\.\.?(?:\\|$)') { throw 'Ungueltiger lokaler NDTrack-Zielpfad.' }
if (-not (Test-Path -LiteralPath ($folder.Substring(0,3)) -PathType Container)) { throw "Ziellaufwerk fehlt: $folder" }
$folder.TrimEnd('\')
} -ArgumentList $script:DestinationPath
}
if ($folder -notmatch '^[A-Za-z]:\\[^"\r\n]+$' -or $folder -match '(?:^|\\)\.\.?(?:\\|$)') { throw 'Ungueltiger lokaler NDTrack-Zielpfad.' }
$folder.TrimEnd('\')
}
function Get-WindowsSharePath {
param([string]$Server)
$folder = Resolve-WindowsFolder $Server
'\\{0}\{1}$\{2}' -f $Server,$folder.Substring(0,1),$folder.Substring(3)
}
function CheckDirectory {
$paths = @($script:ScriptFolder,$script:SourcePath,$script:LinuxSourcePath,
@@ -183,26 +197,23 @@ function CopyFile {
param([string[]]$Servers)
Test-SourceFiles -Path $script:SourcePath -Launcher $script:WindowsLauncher
foreach ($server in $Servers) {
$session = $null
try {
$remote = Resolve-WindowsFolder $server
$session = New-PSSession -ComputerName $server -ErrorAction Stop
# Kein Ueberschreiben waehrend eines bekannten Scans/Tasks.
Invoke-Command -Session $session -ErrorAction Stop -ScriptBlock {
param($folder,$taskName)
$ErrorActionPreference = 'Stop'
if (Test-Path -LiteralPath (Join-Path $folder '.ndtrack-running')) { throw 'Scan-Sperre vorhanden; laufenden Scan bzw. verwaiste Sperre pruefen.' }
$task = Get-ScheduledTask | Where-Object { $_.TaskName -eq $taskName -and $_.TaskPath -eq '\' }
if ($task -and $task.State -in @('Running','Queued')) { throw 'NDTrack-Task ist aktiv.' }
New-Item -Path $folder -ItemType Directory -Force | Out-Null
} -ArgumentList $remote,$script:TaskName
$folder = Resolve-WindowsFolder $server
$remote = '\\{0}\{1}$\{2}' -f $server,$folder.Substring(0,1),$folder.Substring(3)
$share = '\\{0}\{1}$' -f $server,$folder.Substring(0,1)
if (-not (Test-Path -LiteralPath $share -PathType Container -ErrorAction Stop)) {
throw "Freigabe nicht erreichbar: $share. Laufwerk, Freigabe und Berechtigungen pruefen."
}
if (Test-Path -LiteralPath ($remote + '\.ndtrack-running') -ErrorAction Stop) {
throw 'Scan-Sperre vorhanden; laufenden Scan bzw. verwaiste Sperre pruefen.'
}
New-Item -Path $remote -ItemType Directory -Force -ErrorAction Stop | Out-Null
Get-ChildItem -LiteralPath $script:SourcePath -Force -ErrorAction Stop |
Where-Object { $_.Name -notin @('inventory','Result','Ergebnisse','.ndtrack-running') } |
Copy-Item -Destination $remote -ToSession $session -Recurse -Force -ErrorAction Stop
Write-Host "KOPIERT: $server"
Copy-Item -Destination $remote -Recurse -Force -ErrorAction Stop
Write-Host "KOPIERT: $server -> $remote"
$server
} catch { Write-Warning "${server}: $($_.Exception.Message)" }
finally { if ($null -ne $session) { Remove-PSSession -Session $session } }
}
}
function CreateScheduledTask {