function Get-VeeamConfig {

<#
        .SYNOPSIS
            Lädt zentrale Veeam Konfiguration aus JSON.
    #>

    param(

        [string]$Path = (Join-Path ( Split-Path $PSScriptRoot -Parent )"Config\VeeamConfig.json")

    )

    if(!(Test-Path $Path))
    {

        throw `

            "Konfigurationsdatei nicht gefunden: $Path"

    }

    $Content =

        Get-Content `

            -Path $Path `

            -Raw

    $Configuration =

        $Content |

        ConvertFrom-Json

    return $Configuration

}
This commit is contained in:
Madzone
2026-10-15 13:11:00 +02:00
parent 242c7c1253
commit 1b3e50cd32
3 changed files with 98 additions and 34 deletions
+52 -16
View File
@@ -1,5 +1,5 @@
#Requires -Version 5.1
# Skriptversion: 1.0.0
# Skriptversion: 1.0.1
# Versionsstand: 2026-10-15
<#
Korrigierte Kopie auf Basis des Originalskripts, mit denselben acht Optionen.
@@ -33,7 +33,7 @@ function Initialize-Configuration {
function ShowMenu {
Write-Host @'
NDTRACK - Version 1.0.0 (Stand: 2026-10-15)
NDTRACK - Version 1.0.1 (Stand: 2026-10-15)
1: Windows - NDTrack-Dateien auf Server kopieren
2: Windows - Task NDTRACK erstellen und sofort starten
@@ -220,27 +220,63 @@ function CopyFile {
} catch { Write-Warning "${server}: $($_.Exception.Message)" }
}
}
function Assert-TaskNotRunning {
param([string]$Server,[string]$Name)
$service=$null; $root=$null; $tasks=$null
try {
$service = New-Object -ComObject 'Schedule.Service' -ErrorAction Stop
$service.Connect($Server)
$root = $service.GetFolder('\')
$tasks = $root.GetTasks(1)
foreach ($task in $tasks) {
try {
if ($task.Name -ieq $Name -and $task.State -in @(2,4)) { throw 'NDTRACK-Task ist bereits aktiv.' }
} finally { [void][Runtime.InteropServices.Marshal]::ReleaseComObject($task) }
}
} finally {
foreach ($item in @($tasks,$root,$service)) {
if ($null -ne $item) { [void][Runtime.InteropServices.Marshal]::ReleaseComObject($item) }
}
}
}
function CreateScheduledTask {
param([string[]]$Servers,[pscredential]$Credential)
if ($null -eq $Credential) { throw 'Kein Task-Konto angegeben.' }
foreach ($server in $Servers) {
$xmlFile = $null
$stage = 'Zielpfad ermitteln'
try {
$folder = Resolve-WindowsFolder $server
Invoke-Command -ComputerName $server -ErrorAction Stop -ScriptBlock {
param($folder,$launcher,$name,[pscredential]$credential)
$ErrorActionPreference = 'Stop'
$path = Join-Path $folder $launcher
if (-not (Test-Path -LiteralPath $path -PathType Leaf)) { throw 'CMD fehlt.' }
$action = New-ScheduledTaskAction -Execute "$env:SystemRoot\System32\cmd.exe" -Argument ('/d /s /c ""{0}""' -f $path) -WorkingDirectory $folder
$existing = Get-ScheduledTask | Where-Object { $_.TaskName -eq $name -and $_.TaskPath -eq '\' }
if ($existing -and $existing.State -in @('Running','Queued')) { throw 'NDTRACK-Task ist bereits aktiv.' }
if (Test-Path -LiteralPath (Join-Path $folder '.ndtrack-running')) { throw 'Ein direkter Scan ist aktiv oder die Sperre ist verwaist.' }
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -MultipleInstances IgnoreNew
Register-ScheduledTask -TaskName $name -TaskPath '\' -Action $action -Settings $settings -User $credential.UserName -Password $credential.GetNetworkCredential().Password -RunLevel Highest -Force | Out-Null
Start-ScheduledTask -TaskName $name -TaskPath '\' -ErrorAction Stop
} -ArgumentList $folder,$script:WindowsLauncher,$script:TaskName,$Credential
$share = '\\{0}\{1}$\{2}' -f $server,$folder.Substring(0,1),$folder.Substring(3)
$stage = 'CMD und Scan-Sperre ueber SMB pruefen'
if (-not (Test-Path -LiteralPath ($share + '\' + $script:WindowsLauncher) -PathType Leaf -ErrorAction Stop)) { throw 'CMD fehlt; zuerst Option 1 ausfuehren.' }
if (Test-Path -LiteralPath ($share + '\.ndtrack-running') -ErrorAction Stop) { throw 'Scan-Sperre vorhanden.' }
$stage = 'Task-Zustand ueber Task-Scheduler-RPC pruefen'
Assert-TaskNotRunning -Server $server -Name $script:TaskName
# XML erhaelt das Arbeitsverzeichnis und verhindert einen zweiten zeitgesteuerten Start.
$escapedFolder = [Security.SecurityElement]::Escape($folder)
$escapedUser = [Security.SecurityElement]::Escape($Credential.UserName)
$arguments = [Security.SecurityElement]::Escape(('/d /s /c ""{0}\{1}""' -f $folder,$script:WindowsLauncher))
$xml = @"
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<Triggers />
<Principals><Principal id="Author"><UserId>$escapedUser</UserId><LogonType>Password</LogonType><RunLevel>HighestAvailable</RunLevel></Principal></Principals>
<Settings><MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy><DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries><StopIfGoingOnBatteries>false</StopIfGoingOnBatteries><AllowStartOnDemand>true</AllowStartOnDemand><Enabled>true</Enabled></Settings>
<Actions Context="Author"><Exec><Command>%SystemRoot%\System32\cmd.exe</Command><Arguments>$arguments</Arguments><WorkingDirectory>$escapedFolder</WorkingDirectory></Exec></Actions>
</Task>
"@
$xmlFile = [IO.Path]::GetTempFileName()
[IO.File]::WriteAllText($xmlFile,$xml,[Text.Encoding]::Unicode)
$stage = 'Task mit schtasks.exe registrieren'
# Aktuelles Konto verwaltet den Remote-Task; Credential ist nur das Ausfuehrungskonto.
# Passwort wie beim Original nur an /RP uebergeben, nie in XML oder Log schreiben.
Invoke-NativeChecked 'schtasks.exe' @('/Create','/S',$server,'/TN',$script:TaskName,'/XML',$xmlFile,'/RU',$Credential.UserName,'/RP',$Credential.GetNetworkCredential().Password,'/F')
$stage = 'Task mit schtasks.exe starten'
Invoke-NativeChecked 'schtasks.exe' @('/Run','/S',$server,'/TN',$script:TaskName)
Write-Host "START ANGEFORDERT: $server - Task NDTRACK erstellt und Start ausgeloest; Scan-Abschluss noch nicht bestaetigt."
} catch { Write-Warning "${server}: $($_.Exception.Message)" }
} catch { Write-Warning "${server}: $stage fehlgeschlagen: $($_.Exception.Message)" }
finally { if ($xmlFile -and (Test-Path -LiteralPath $xmlFile)) { Remove-Item -LiteralPath $xmlFile -Force -ErrorAction SilentlyContinue } }
}
}
function Invoke-WindowsScan {