--- Original/NDTrack.ps1 +++ Angepasst/NDTrack.ps1 @@ -1,14 +1,17 @@ # ========================= # Konfigurationsparameter # ========================= -$global:SourcePath = "$env:USERPROFILE\Documents\Windows" # Pfad zur Quelldatei die verteilt werden soll +$DocumentsPath = [Environment]::GetFolderPath('MyDocuments') +$global:SourcePath = Join-Path $DocumentsPath 'Windows' $global:DestinationPath = "C:\ndtrack" # Zielpfad auf den Remote Servern $global:DestinationPath_RDS = "\RDSFIXV001\workfolders$\$env:USERPROFILE\Documents\windows" $global:TaskName = "NDTrackTask" #$global:TestServers = "GARRDSV003","GARRDSV002" -$global:Dest_folder = "\\$f_server\c$\ndtrack" -$global:logfile= "$env:USERPROFILES\Documents\" -$global:Ergebnisordner= "$env:USERPROFILES\Documents\Ergebnisse" +$global:logfile = Join-Path $DocumentsPath 'NDTrack-Logs' +$global:Ergebnisordner = Join-Path $DocumentsPath 'Ergebnisse' + +New-Item -Path $logfile, $Ergebnisordner -ItemType Directory ` + -Force -ErrorAction Stop | Out-Null Import-Module RemoteDesktop @@ -31,7 +34,8 @@ Write-Host "Option 7: NDTRACK von Systemen entfernen. (Linux|Textdatei)" Write-Host "Option 8: RDS Server mittels NDTRACK scannen" Write-Host "===============================================================================" - $f_Choice = Read-Host -Prompt "Auswahl Variante (1-8)" + Write-Host "Option 0: Beenden" + $f_Choice = Read-Host -Prompt "Auswahl Variante (0-8)" Write-Host return $f_Choice @@ -81,22 +85,22 @@ try{ # Zielordner prüfen - Invoke-Command -ComputerName $f_Server -ScriptBlock { - param($Dest_folder) # Anpassung an wirklichen Ordner - - #$a = !(Test-Path "\\$f_Server\c$") # TRUE, ohne ! immer false $f_Server anstatt $DestinationPath, asonsten keine valide Abfrage - #write-host $a # Ausgabe zur kontrolle - - if ((Test-Path "\\$f_Server\c$")){ - New-Item -Path $DestinationPath -ItemType Directory #-Force #unbedingtes Schreiben ausgeschaltet - } - } -ArgumentList $DestinationPath - - #Datei auf Remote Server kopieren - Copy-Item -Path "$SourcePath\*" -Destination "\\$f_Server\c$\ndtrack" -Recurse -Force # Schleife für das Abfange der Meldungen + $RemoteFolder = "\\$f_Server\c$\ndtrack" + + if (-not (Test-Path -LiteralPath "$SourcePath\RunNdTrackWindows_Local.cmd" ` + -PathType Leaf -ErrorAction Stop)) { + throw "Startdatei fehlt in $SourcePath." + } + + New-Item -Path $RemoteFolder -ItemType Directory ` + -Force -ErrorAction Stop | Out-Null + + Copy-Item -Path "$SourcePath\*" -Destination $RemoteFolder ` + -Recurse -Force -ErrorAction Stop # Meldung geben Write-Host "ERFOLG: $f_Server konfiguriert." -ForegroundColor Green + Write-Output $f_Server } catch {Write-Host "FEHLER: $f_Server fehlgeschlagen. - $_" -ForegroundColor Red write-host $Error} @@ -112,7 +116,34 @@ Write-Host "Prozessiere $f_Server ..." -ForegroundColor Cyan try{ - schtasks.exe /Create /S $f_Server /TN $TaskName /TR "$DestinationPath\RunNdTrackWindows_Local.cmd" /SC "Once" /ST (Get-Date).AddMinutes(5).ToShortTimeString() /RU $User /RP $Password /RL "HIGHEST" /F + Invoke-Command -ComputerName $f_Server -ScriptBlock { + param($DestinationPath, $TaskName, [pscredential]$Credential) + + $ErrorActionPreference = 'Stop' + $CmdPath = Join-Path $DestinationPath 'RunNdTrackWindows_Local.cmd' + + if (-not (Test-Path -LiteralPath $CmdPath -PathType Leaf)) { + throw "Startdatei fehlt: $CmdPath" + } + + $Action = New-ScheduledTaskAction ` + -Execute "$env:SystemRoot\System32\cmd.exe" ` + -Argument ('/d /s /c ""{0}""' -f $CmdPath) ` + -WorkingDirectory $DestinationPath + + $Trigger = New-ScheduledTaskTrigger ` + -Once -At (Get-Date).AddMinutes(5) + + $Settings = New-ScheduledTaskSettingsSet -StartWhenAvailable + + Register-ScheduledTask -TaskName $TaskName ` + -Action $Action -Trigger $Trigger -Settings $Settings ` + -User $Credential.UserName ` + -Password $Credential.GetNetworkCredential().Password ` + -RunLevel Highest -Force | Out-Null + + } -ArgumentList $DestinationPath, $TaskName, $ServiceAccount ` + -ErrorAction Stop # Meldung geben Write-Host "ERFOLG: $f_Server konfiguriert." -ForegroundColor Green } @@ -213,18 +244,28 @@ try{ # Zielordner prüfen - Invoke-Command -ComputerName $f_Server -ScriptBlock { - param($Dest_folder) #Dest_folder=\\$f_server\c$\ndtrack - - if (Test-Path $Dest_folder) { - Write-Host "Es wurde kein Inventory Ordner gefunden." -ForegroundColor Red - - } - } -ArgumentList $Dest_folder - - # Datei vom Remote Server kopieren - #Copy-Item -Path "\\$f_Server\c$\ndtrack\inventory\*" -Destination "$SourcePath\Result" -Recurse -Force - Copy-Item -Path "\\$f_Server\c$\ndtrack\inventory\*" -Destination "c:\\users\qsadmin03\Documents\Ergebnisse" -Recurse -Force -PassThru #| Out-File -FilePath "$Source_Path\Result.txt" -Append + $InventoryPath = "\\$f_Server\c$\ndtrack\inventory" + $TargetPath = Join-Path $Ergebnisordner $f_Server + + if (-not (Test-Path -LiteralPath $InventoryPath ` + -PathType Container -ErrorAction Stop)) { + Write-Warning "${f_Server}: Kein Inventory-Ordner gefunden." + continue + } + + $Files = @(Get-ChildItem -LiteralPath $InventoryPath ` + -File -Recurse -ErrorAction Stop) + + if ($Files.Count -eq 0) { + Write-Warning "${f_Server}: Keine Inventardateien vorhanden." + continue + } + + New-Item -Path $TargetPath -ItemType Directory ` + -Force -ErrorAction Stop | Out-Null + + Copy-Item -Path "$InventoryPath\*" -Destination $TargetPath ` + -Recurse -Force -ErrorAction Stop # Meldung geben Write-Host "ERFOLG: $f_Server Ergebnis geholt." -ForegroundColor Green @@ -404,13 +445,14 @@ Write-Host "Variante $_ ausgewählt..." -ForegroundColor Green $ServiceAccount = Get-Credential -Message "User für Scheduled Task eingeben" - $User = $ServiceAccount.UserName - $Password = $ServiceAccount.GetNetworkCredential().Password $AllServers = GetADServer #$AllServers = $TestServers - CopyFile $AllServers - CreateScheduledTask $AllServers + $CopiedServers = @(CopyFile $AllServers) + + if ($CopiedServers.Count -gt 0) { + CreateScheduledTask $CopiedServers + } Stop-Transcript } # Windows|AD Scheduled Task @@ -470,9 +512,11 @@ Stop-Transcript } # RDS|Textdatei löschen + 0 { } + default { Write-Host "Das war wohl nix... Bitte versuche es erneut!" -ForegroundColor Red Start-Sleep 2 } } -} while ($Choice -notin (1,2,3,4,5,6,7))+} while ($Choice -ne '0')