523 lines
18 KiB
PowerShell
523 lines
18 KiB
PowerShell
# =========================
|
|
# Konfigurationsparameter
|
|
# =========================
|
|
$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: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
|
|
|
|
# =========================
|
|
# Funktionen
|
|
# =========================
|
|
function ShowMenu(){
|
|
param()
|
|
|
|
cls
|
|
Write-Host "================================================================================"
|
|
Write-Host "Dieses Script bietet verschiedene Optionen die Softwareinventur durchzuführen."
|
|
Write-Host
|
|
Write-Host "Option 1: Remote Ausführung von NDTRACK mittels Remoting. (Windows|AD)"
|
|
Write-Host "Option 2: Lokale Ausführung von NDTRACK mittels Scheduled Task. (Windows|AD)"
|
|
Write-Host "Option 3: Lokale Ausführung von NDTRACK mittels SSH. (Linux|Textdatei)"
|
|
Write-Host "Option 4: Inventory Dateien einsammeln. (Windows|AD)"
|
|
Write-Host "Option 5: Inventory Dateien einsammeln. (Linux|Textdatei)"
|
|
Write-Host "Option 6: NDTRACK und Scheduled Task von Systemen entfernen. (Windows|AD)"
|
|
Write-Host "Option 7: NDTRACK von Systemen entfernen. (Linux|Textdatei)"
|
|
Write-Host "Option 8: RDS Server mittels NDTRACK scannen"
|
|
Write-Host "==============================================================================="
|
|
Write-Host "Option 0: Beenden"
|
|
$f_Choice = Read-Host -Prompt "Auswahl Variante (0-8)"
|
|
Write-Host
|
|
|
|
return $f_Choice
|
|
}
|
|
|
|
function GetADServer(){
|
|
param()
|
|
|
|
Write-Host "Server aus dem Active Directory werden ausgelesen ..." -ForegroundColor Cyan
|
|
|
|
$f_DomainDN = ([ADSI]"").distinguishedName
|
|
$f_Searcher = New-Object adsisearcher
|
|
$f_Searcher.SearchRoot = "LDAP://$f_DomainDN" # Muss angepasst werden
|
|
$f_Searcher.Filter = "(&(objectClass=computer)(operatingSystem=*Server*))"
|
|
$f_Searcher.PageSize = 1000
|
|
$f_Servers = $f_Searcher.FindAll() | ForEach-Object {$_.Properties.name}
|
|
|
|
return $f_Servers
|
|
}
|
|
|
|
function GetRDSServer(){
|
|
param()
|
|
|
|
Write-Host "RDSServer aus dem Active Directory werden ausgelesen ..." -ForegroundColor Cyan
|
|
|
|
$f_DomainDN = ([ADSI]"").distinguishedName
|
|
$f_Searcher = New-Object adsisearcher
|
|
$f_Searcher.SearchRoot = "LDAP://$f_DomainDN" # Muss angepasst werden
|
|
$f_rds_Servers = Get-RDServer
|
|
|
|
return $f_rds_Servers
|
|
}
|
|
|
|
function CopyFile(){
|
|
param($f_AllServers)
|
|
|
|
Write-Host "Ordner werden angelegt und Dateien kopiert ..." -ForegroundColor Cyan
|
|
|
|
|
|
Foreach ($f_Server in $f_AllServers){
|
|
Write-Host "Prozessiere $f_Server ..." -ForegroundColor Cyan
|
|
#write-host $DestinationPath
|
|
if (-not (Test-Path "\\$f_server\c$")){
|
|
write-host "$f_server : c$ ist nicht verfügbar"
|
|
continue
|
|
}
|
|
|
|
try{
|
|
# Zielordner prüfen
|
|
$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}
|
|
}
|
|
}
|
|
|
|
function CreateScheduledTask(){
|
|
param($f_AllServers)
|
|
|
|
Write-Host "Scheduled Tasks werden angelegt ..." -ForegroundColor Cyan
|
|
|
|
Foreach ($f_Server in $f_AllServers){
|
|
Write-Host "Prozessiere $f_Server ..." -ForegroundColor Cyan
|
|
|
|
try{
|
|
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
|
|
}
|
|
catch {Write-Host "FEHLER: $f_Server fehlgeschlagen - $_" -ForegroundColor Red}
|
|
}
|
|
}
|
|
|
|
function DeleteFile(){
|
|
param($f_AllServers)
|
|
|
|
Write-Host "Dateien werden von den Servern gelöscht ..." -ForegroundColor Cyan
|
|
|
|
Foreach ($f_Server in $f_AllServers){
|
|
Write-Host "Lösche Datei von $f_Server ..." -ForegroundColor Cyan
|
|
|
|
try{
|
|
# Zielordner prüfen
|
|
Invoke-Command -ComputerName $f_Server -ScriptBlock {
|
|
param($DestinationPath)
|
|
|
|
if (Test-Path $DestinationPath){
|
|
Remove-Item -Path $DestinationPath -Recurse -Force
|
|
Write-Output "Datei gelöscht."
|
|
}
|
|
else{
|
|
Write-Output "Datei nicht gefunden."
|
|
}
|
|
} -ArgumentList $DestinationPath -ErrorAction Stop
|
|
|
|
# Meldung geben
|
|
Write-Host "ERFOLG: $f_Server entfernt." -ForegroundColor Green
|
|
}
|
|
catch {Write-Host "FEHLER: $f_Server Entfernen fehlgeschlagen. - $_" -ForegroundColor Red}
|
|
}
|
|
}
|
|
|
|
function DeleteScheduledTask(){
|
|
param($f_AllServers)
|
|
|
|
Write-Host "Scheduled Tasks werden gelöscht ..." -ForegroundColor Cyan
|
|
|
|
Foreach ($f_Server in $f_AllServers){
|
|
Write-Host "Prozessiere $f_Server ..." -ForegroundColor Cyan
|
|
|
|
try{
|
|
Invoke-Command -ComputerName $f_Server -ScriptBlock {
|
|
param($TaskName)
|
|
|
|
if (Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue){
|
|
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
|
|
Write-Output "Scheduled Task entfernt."
|
|
}
|
|
else{
|
|
Write-Output "Scheduled Task nicht gefunden."
|
|
}
|
|
} -ArgumentList $TaskName -AsJob -ErrorAction Stop
|
|
|
|
# Meldung geben
|
|
Write-Host "ERFOLG: $f_Server gelöscht." -ForegroundColor Green
|
|
}
|
|
catch {Write-Host "FEHLER: $f_Server Löschung gescheitert - $_" -ForegroundColor Red}
|
|
}
|
|
}
|
|
|
|
function RunNDTRACKRemotely(){
|
|
param($f_AllServers)
|
|
|
|
Write-Host "NDTRACK wird ausgeführt ..." -ForegroundColor Cyan
|
|
|
|
Foreach ($f_Server in $f_AllServers){
|
|
Write-Host "Prozessiere $f_Server ..." -ForegroundColor Cyan
|
|
|
|
try{
|
|
Invoke-Command -ComputerName $f_Server -ScriptBlock {
|
|
param($DestinationPath)
|
|
|
|
&"$DestinationPath\RunNdTrackWindows_Local.cmd"
|
|
} -ArgumentList $DestinationPath -AsJob -ErrorAction Stop
|
|
|
|
# Meldung geben
|
|
Write-Host "ERFOLG: $f_Server konfiguriert." -ForegroundColor Green
|
|
}
|
|
catch {Write-Host "FEHLER: $f_Server fehlgeschlagen - $_" -ForegroundColor Red}
|
|
}
|
|
}
|
|
|
|
function GetInventoryFiles(){
|
|
param($f_AllServers)
|
|
|
|
Write-Host "Inventory Dateien werden eingesammelt ..." -ForegroundColor Cyan
|
|
|
|
Foreach ($f_Server in $f_AllServers){
|
|
Write-Host "Prozessiere $f_Server ..." -ForegroundColor Cyan
|
|
if (-not (Test-Path "\\$f_server\c$")){
|
|
write-host "$f_server : c$ ist nicht verfügbar"
|
|
continue
|
|
}
|
|
try{
|
|
# Zielordner prüfen
|
|
|
|
$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
|
|
|
|
}
|
|
catch {Write-Host "FEHLER: $f_Server fehlgeschlagen. - $_" -ForegroundColor Red}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
function GetLinuxServer(){
|
|
param()
|
|
|
|
Do {
|
|
$f_FileLocation = Read-Host -Prompt "Bitte den vollständigen Pfad zur Textdatei eingeben"
|
|
} while (!(Test-Path $f_FileLocation))
|
|
|
|
Write-Host "Linux-Server aus Textdatei werden ausgelesen ..." -ForegroundColor Cyan
|
|
$f_Servers = Get-Content -Path $f_FileLocation
|
|
|
|
return $f_Servers
|
|
}
|
|
|
|
function CopyFileLinux(){
|
|
param($f_AllServers)
|
|
|
|
Write-Host "Ordner werden angelegt und Dateien kopiert ..." -ForegroundColor Cyan
|
|
|
|
Foreach ($f_Server in $f_AllServers){
|
|
Write-Host "Prozessiere $f_Server ..." -ForegroundColor Cyan
|
|
|
|
try{
|
|
# Befehl erzeugen
|
|
$f_Command = "mkdir /home/$User/ndtrack"
|
|
|
|
# Ordner erstellen
|
|
ssh "$User@$f_Server" $f_Command
|
|
|
|
# Datei auf Remote Server kopieren
|
|
scp -r "$SourcePath\*" "$User@$f_Server`:/home/$User/ndtrack"
|
|
|
|
# Meldung geben
|
|
Write-Host "ERFOLG: $f_Server konfiguriert." -ForegroundColor Green
|
|
}
|
|
catch {Write-Host "FEHLER: $f_Server fehlgeschlagen. - $_" -ForegroundColor Red}
|
|
}
|
|
}
|
|
|
|
function RunNDTRACKRemotelyLinux(){
|
|
param($f_AllServers)
|
|
|
|
Write-Host "NDTRACK wird ausgeführt ..." -ForegroundColor Cyan
|
|
|
|
Foreach ($f_Server in $f_AllServers){
|
|
Write-Host "Prozessiere $f_Server ..." -ForegroundColor Cyan
|
|
|
|
try{
|
|
# Befehl erzeugen
|
|
$f_Command = "/home/$User/ndtrack/RunNdTrackLinuxUnix_Local.sh"
|
|
|
|
# NDTrack ausführen
|
|
ssh "$User@$f_Server" $f_Command
|
|
|
|
# Meldung geben
|
|
Write-Host "ERFOLG: $f_Server konfiguriert." -ForegroundColor Green
|
|
}
|
|
catch {Write-Host "FEHLER: $f_Server fehlgeschlagen - $_" -ForegroundColor Red}
|
|
}
|
|
}
|
|
|
|
function GetInventoryFilesLinux(){
|
|
param($f_AllServers)
|
|
|
|
Write-Host "Inventory Dateien werden eingesammelt ..." -ForegroundColor Cyan
|
|
|
|
Foreach ($f_Server in $f_AllServers){
|
|
Write-Host "Prozessiere $f_Server ..." -ForegroundColor Cyan
|
|
|
|
try{
|
|
# Nutzer einlesen
|
|
$f_User = Read-Host -Prompt "Nutzer in Form User@Domain eingeben"
|
|
|
|
# Datei vom Remote Server kopieren
|
|
scp "$f_User@$f_Server`:/home/$f_User/ndtrack/inventory/*" "$SourcePath\Result"
|
|
|
|
# Meldung geben
|
|
Write-Host "ERFOLG: $f_Server konfiguriert." -ForegroundColor Green
|
|
}
|
|
catch {Write-Host "FEHLER: $f_Server fehlgeschlagen. - $_" -ForegroundColor Red}
|
|
}
|
|
}
|
|
|
|
function DeleteFileLinux(){
|
|
param($f_AllServers)
|
|
|
|
Write-Host "Dateien werden von den Servern gelöscht ..." -ForegroundColor Cyan
|
|
|
|
Foreach ($f_Server in $f_AllServers){
|
|
Write-Host "Lösche Datei von $f_Server ..." -ForegroundColor Cyan
|
|
|
|
try{
|
|
# Nutzer einlesen
|
|
$f_User = Read-Host -Prompt "Nutzer in Form User@Domain eingeben"
|
|
|
|
# Befehl erzeugen
|
|
$f_Command = "rm -r /home/$f_User/ndtrack"
|
|
|
|
# Ordner und Dateien löschen
|
|
ssh "$f_User@$f_Server" $f_Command
|
|
|
|
# Meldung geben
|
|
Write-Host "ERFOLG: $f_Server konfiguriert." -ForegroundColor Green
|
|
}
|
|
catch {Write-Host "FEHLER: $f_Server fehlgeschlagen. - $_" -ForegroundColor Red}
|
|
}
|
|
}
|
|
function CheckDirectory() {
|
|
# test if folder exists
|
|
if (Test-Path -Path $Ergebnisordner) {
|
|
Write-Host "Ergebnis Ordner besteht bereits" -ForegroundColor yellow
|
|
} else {
|
|
"Verzeichnis existiert nicht. Wird erzeugt"
|
|
if (New-Item -ItemType Directory -Force -Path $Ergebnisordner) {
|
|
"Ergebniss Verzeichnis Erzeugt"
|
|
} else {
|
|
exit
|
|
}
|
|
}
|
|
}
|
|
|
|
function Scan_RDS_SINA () {
|
|
# special routine for KdoIT-Bws gehärtete SINA RDS Clients
|
|
param($f_AllServers)
|
|
|
|
Write-Host "NDTRACK wird ausgeführt ..." -ForegroundColor Cyan
|
|
|
|
Foreach ($f_Server in $f_AllServers){
|
|
Write-Host "Prozessiere $f_Server ..." -ForegroundColor Cyan
|
|
|
|
try{
|
|
Invoke-Command -ComputerName $f_Server -ScriptBlock {
|
|
param($DestinationPath_RDS)
|
|
|
|
&"$DestinationPath_RDS\RunNdTrackWindows_Local_RDSSINA.cmd"
|
|
} -ArgumentList $DestinationPath_RDS -AsJob -ErrorAction Stop
|
|
|
|
# Meldung geben
|
|
Write-Host "ERFOLG: $f_Server konfiguriert." -ForegroundColor Green
|
|
}
|
|
catch {Write-Host "FEHLER: $f_Server fehlgeschlagen - $_" -ForegroundColor Red}
|
|
}
|
|
}
|
|
|
|
|
|
# =========================
|
|
# Hauptprogramm
|
|
# =========================
|
|
Do {
|
|
$Choice = ShowMenu
|
|
|
|
Switch ($Choice){
|
|
1 {
|
|
Write-Host "Variante $_ ausgewählt..." -ForegroundColor Green
|
|
write-host $Ergebnisordner
|
|
CheckDirectory
|
|
Start-Transcript -Path "$logfile\logfile-$(Get-Date -format dd-MM-yyyy-hhmm).log" -IncludeInvocationHeader -force -append
|
|
$AllServers = GetADServer
|
|
#$AllServers = $TestServers
|
|
CopyFile $AllServers
|
|
RunNDTRACKRemotely $AllServers
|
|
Stop-Transcript
|
|
} # Windows|AD Remoting
|
|
|
|
2 {
|
|
Start-Transcript -Path "$logfile\logfile-$(Get-Date -format dd-MM-yyyy-hhmm).log" -IncludeInvocationHeader -force -append
|
|
Write-Host "Variante $_ ausgewählt..." -ForegroundColor Green
|
|
|
|
$ServiceAccount = Get-Credential -Message "User für Scheduled Task eingeben"
|
|
|
|
$AllServers = GetADServer
|
|
#$AllServers = $TestServers
|
|
$CopiedServers = @(CopyFile $AllServers)
|
|
|
|
if ($CopiedServers.Count -gt 0) {
|
|
CreateScheduledTask $CopiedServers
|
|
}
|
|
Stop-Transcript
|
|
} # Windows|AD Scheduled Task
|
|
|
|
3 {
|
|
Write-Host "Variante $_ ausgewählt..." -ForegroundColor Green
|
|
Start-Transcript -Path "$logfile\logfile-$(Get-Date -format dd-MM-yyyy-hhmm).log" -IncludeInvocationHeader -force -append
|
|
$AllServers = GetLinuxServer
|
|
|
|
# Nutzer einlesen
|
|
$User = Read-Host -Prompt "Nutzer in Form User@Domain eingeben"
|
|
|
|
CopyFileLinux $AllServers
|
|
RunNDTRACKRemotelyLinux $AllServers
|
|
Stop-Transcript
|
|
} # Linux|Textdatei SSH
|
|
|
|
4 {
|
|
Write-Host "Variante $_ ausgewählt..." -ForegroundColor Green
|
|
Start-Transcript -Path "$logfile\logfile-$(Get-Date -format dd-MM-yyyy-hhmm).log" -IncludeInvocationHeader -force -append
|
|
$AllServers = GetADServer
|
|
#$AllServers = $TestServers
|
|
GetInventoryFiles $AllServers
|
|
Stop-Transcript
|
|
} # Windows|AD einsammeln
|
|
|
|
5 {
|
|
Write-Host "Variante $_ ausgewählt..." -ForegroundColor Green
|
|
Start-Transcript -Path "$logfile\logfile-$(Get-Date -format dd-MM-yyyy-hhmm).log" -IncludeInvocationHeader -force -append
|
|
$AllServers = GetLinuxServer
|
|
GetInventoryFilesLinux $AllServers
|
|
Stop-Transcript
|
|
} # Linux|Textdatei einsammeln
|
|
|
|
6 {
|
|
Write-Host "Variante $_ ausgewählt..." -ForegroundColor Green
|
|
Start-Transcript -Path "$logfile\logfile-$(Get-Date -format dd-MM-yyyy-hhmm).log" -IncludeInvocationHeader -force -append
|
|
$AllServers = GetADServer
|
|
#$AllServers = $TestServers
|
|
DeleteFile $AllServers
|
|
DeleteScheduledTask $AllServers
|
|
Stop-Transcript
|
|
} # Windows|AD löschen
|
|
|
|
7 {
|
|
Write-Host "Variante $_ ausgewählt..." -ForegroundColor Green
|
|
Start-Transcript -Path "$logfile\logfile-$(Get-Date -format dd-MM-yyyy-hhmm).log" -IncludeInvocationHeader -force -append
|
|
$AllServers = GetLinuxServer
|
|
DeleteFileLinux $AllServers
|
|
Stop-Transcript
|
|
} # Linux|Textdatei löschen
|
|
|
|
8 {
|
|
Write-Host "Variante $_ ausgewählt..." -ForegroundColor Green
|
|
Start-Transcript -Path "$logfile\logfile-$(Get-Date -format dd-MM-yyyy-hhmm).log" -IncludeInvocationHeader -force -append
|
|
$AllServers = GetRDSServer
|
|
DeleteFileRDS $AllServers
|
|
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 -ne '0')
|