changed: logging

changed: sort and deduplicate output
changed: "GruppenVerwaltungNEU..." output
This commit is contained in:
Edmond Gebara 2026-07-06 06:50:50 +02:00
parent b84b5a73a1
commit a5e28a7f52
12 changed files with 785 additions and 1280 deletions

128
Process-ADObjects-Utility.ps1 Executable file → Normal file
View file

@ -20,25 +20,106 @@ function getTimeStamp
return $now.ToString($formatString)
}
function openLog
{
param([string] $Path = $LogFileName)
closeLog
$parentDir = Split-Path -Parent $Path
if ($parentDir -and -not (Test-Path $parentDir))
{
New-Item -ItemType Directory -Path $parentDir -Force | Out-Null
}
$Global:LogWriter = [System.IO.StreamWriter]::new(
$Path, $true, [System.Text.UTF8Encoding]::new($false))
$Global:LogWriter.AutoFlush = $true
$Global:LogWriterPath = $Path
}
function closeLog
{
if ($Global:LogWriter)
{
$Global:LogWriter.Flush()
$Global:LogWriter.Dispose()
$Global:LogWriter = $null
$Global:LogWriterPath = $null
}
}
function outLogVerbose
{
if (-not $Config.Verbose)
{
return
}
outLog @args -ToScreen $false
}
function outLog
{
[CmdletBinding(PositionalBinding = $false)]
param
(
[Parameter(ValueFromRemainingArguments = $true)]
[object[]] $Messages,
[bool] $ToScreen = $true
)
$timestamp = Get-Date
$outString = $timestamp.ToString("HH:mm:ss.mmm") + ": "
for ($i = 0; $i -lt $args.count; $i++)
$outString = $timestamp.ToString("HH:mm:ss.fff") + ": "
if ($Messages)
{
$j = $i + 1
$outString += $args[$i]
$outString += ($Messages -join [Environment]::NewLine)
}
if ($previuosTime)
{
$outString += ($end - $start)
$outString += [Environment]::NewLine + ($end - $start)
}
if (-not $Global:LogWriter -or $Global:LogWriterPath -ne $LogFileName)
{
openLog $LogFileName
}
$Global:LogWriter.WriteLine($outString)
if ($ToScreen)
{
Write-Host $outString
}
$outString | Tee-Object -FilePath $LogFileName -Append | Write-Host
}
function removeDuplicatesFromSortedList
{
param
(
[Parameter(Mandatory = $true)]
[System.Collections.Generic.List[string]] $List,
[Parameter(Mandatory = $false)]
[string] $ListName = "list"
)
if ($List.Count -lt 2)
{
return
}
$unique = [System.Collections.Generic.List[string]]::new($List.Count)
$previous = $null
foreach ($entry in $List)
{
if ($entry -cne $previous)
{
$unique.Add($entry)
$previous = $entry
}
}
$removedCount = $List.Count - $unique.Count
if ($removedCount -gt 0)
{
$List.Clear()
$List.AddRange($unique)
}
outLog ($ListName + ": removed " + $removedCount + " duplicate lines, " + $List.Count + " remaining")
}
function outScreen
{
$timestamp = Get-Date
$outString = $timestamp.ToString("HH:mm:ss.mmm") + ": "
$outString = $timestamp.ToString("HH:mm:ss.fff") + ": "
for ($i = 0; $i -lt $args.count; $i++)
{
$j = $i + 1
@ -57,28 +138,32 @@ function checkInput
$spk = $spk.ToUpperInvariant()
if ([string]::IsNullOrEmpty($spk))
{
outLogVerbose 'if ([string]::IsNullOrEmpty($spk)) -> true'
if (isProbablyDevelopmentUser)
{
Write-Host "no given Institute (parameter '-sparkasse'), using default" $Config.DefaultInstitute
outLogVerbose 'if (isProbablyDevelopmentUser) -> true'
$spk = $Config.DefaultInstitute.ToUpperInvariant()
}
else
{
outLogVerbose 'else -> true (if/elseif (isProbablyDevelopmentUser) -> false)'
while ($true)
{
outLogVerbose 'while ($true) -> true'
$spk = $(Read-Host "please enter Institute Parameter (parameter '-sparkasse'), 'Ctrl-C for exit'" ).ToUpperInvariant()
if ([string]::IsNullOrEmpty($spk))
{
outLogVerbose 'if ([string]::IsNullOrEmpty($spk)) -> true'
continue
}
elseif (checkInstitute($spk))
{
outLogVerbose 'elseif (checkInstitute($spk)) -> true'
break
}
else
{
Write-Host "this institute is not in our list of wellknown institutes: "
Write-Host ($Config.SPK | Format-Table | Out-String)
outLogVerbose 'else -> true (if/elseif (checkInstitute($spk)) -> false)'
continue
}
}
@ -86,9 +171,7 @@ function checkInput
}
elseif (-not (checkInstitute($spk)))
{
# Write-Host "edmond"
Write-Host "this institute is not in our list of wellknown institutes: "
Write-Host ($Config.SPK | Format-Table | Out-String)
outLogVerbose 'elseif (-not (checkInstitute($spk))) -> true'
exit
}
return $spk
@ -98,12 +181,12 @@ function checkInputSourceFiles
{
if (-not $InstitutsSourcePath)
{
Write-Host "Source file does not exist: " $InstitutsSourcePath
outLogVerbose 'if (-not $InstitutsSourcePath) -> true'
return $false
}
if (-not $InstitutsSourcePathKURS)
{
Write-Host "Source file does not exist: " $InstitutsSourcePathKURS
outLogVerbose 'if (-not $InstitutsSourcePathKURS) -> true'
return $false
}
return $true
@ -114,36 +197,43 @@ function checkInputDataFiles
$ok = $true
if (-not $ADObjectsFilePath)
{
outLogVerbose 'if (-not $ADObjectsFilePath) -> true'
outLog "no ADObjectsFile found !"
$ok = $false
}
if (-not $ADObjectMembershipsFilePath)
{
outLogVerbose 'if (-not $ADObjectMembershipsFilePath) -> true'
outLog "no ADObjectsFile found !"
$ok = $false
}
if (-not $ADApplicationsInFilePath)
{
outLogVerbose 'if (-not $ADApplicationsInFilePath) -> true'
outLog "no ADApplicationsInFile found !"
$ok = $false
}
if (-not $ADDirectoriesInFilePath)
{
outLogVerbose 'if (-not $ADDirectoriesInFilePath) -> true'
outLog "no ADDirectoriesInFile found !"
$ok = $false
}
if (-not $SecurityInFilePath)
{
outLogVerbose 'if (-not $SecurityInFilePath) -> true'
outLog "no SecurityInFile found !"
$ok = $false
}
if (-not $KURSPositionsInFilePath)
{
outLogVerbose 'if (-not $KURSPositionsInFilePath) -> true'
outLog "no KURSPositionsInFile found !"
$ok = $false
}
if (-not $KURSEmployeesInFilePath)
{
outLogVerbose 'if (-not $KURSEmployeesInFilePath) -> true'
outLog "no KURSEmployeesInFile found !"
$ok = $false
}
@ -151,6 +241,7 @@ function checkInputDataFiles
$ADObjectMembershipsFileTimeStamp = (Get-ChildItem $ADObjectMembershipsFilePath).BaseName.Split("_")[2]
if ($ADObjectsFileTimeStamp -ne $ADObjectMembershipsFileTimeStamp)
{
outLogVerbose 'if ($ADObjectsFileTimeStamp -ne $ADObjectMembershipsFileTimeStamp) -> true'
outLog "TimeStamps of latest input files found are not equal: ADObjectsFileTimeStamp:" $ADObjectsFileTimeStamp "ADObjectMembershipsFileTimeStamp: " $ADObjectMembershipsFileTimeStamp ", stopping process !"
$ok = $false
}
@ -161,6 +252,7 @@ function checkTestMode
{
if ($Config.TestMode -gt 0)
{
outLogVerbose 'if ($Config.TestMode -gt 0) -> true'
outLog "entering TestMode, reading the first" $Config.TestMode "ADObject-entries for every roletype"
return $true
}
@ -173,12 +265,12 @@ function isProbablyDevelopmentUser
$userName = [Environment]::UserName
if ($userName -ieq "edmond")
{
Write-Host "developer" $userName "found"
outLogVerbose 'if ($userName -ieq "edmond") -> true'
return $true
}
elseif ($userName -ieq "burgwink")
{
Write-Host "developer" $userName "found"
outLogVerbose 'elseif ($userName -ieq "burgwink") -> true'
return $true
}
return $false
@ -245,6 +337,7 @@ function moveToInstituteTmpPath
$tmpFilePath = $InstituteRootPathTmp + "/" + $fileName
if ($force)
{
outLogVerbose 'if ($force) -> true'
Remove-Item -Path $tmpFilePath -ErrorAction Ignore
}
Move-Item -Path $filePath -Destination $InstituteRootPathTmp
@ -288,6 +381,7 @@ function writeExceptionInfo
if ($Rethrow)
{
outLogVerbose 'if ($Rethrow) -> true'
throw $ErrorRecord
}
}