Private/Helpers/Write-Log.ps1
|
function Write-Log { [CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Message, [ValidateSet('Info', 'Warning', 'Error')] [string]$Level = 'Info', [string]$LogPath = (Join-Path $script:Config.LogPath "ADInventory.log") ) $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $logMessage = "$timestamp [$Level] $Message" # Ensure log directory exists if (-not (Test-Path (Split-Path $LogPath))) { New-Item -ItemType Directory -Path (Split-Path $LogPath) -Force } # Write to log file Add-Content -Path $LogPath -Value $logMessage # Also write to console with appropriate color switch ($Level) { 'Error' { Write-Host $logMessage -ForegroundColor Red } 'Warning' { Write-Host $logMessage -ForegroundColor Yellow } 'Info' { Write-Host $logMessage -ForegroundColor Green } } } |