🤔 Why Proper Logging Matters
We’ve all seen it—or done it ourselves. That innocent-looking Write-Host
scattered across scripts like confetti at a party. 🎉 But guess what? Write-Host
is not a log. It simply displays text to the screen and disappears into the void. 😱 If you’re running scripts in automation, remoting, or as scheduled tasks, those messages are gone. No history. No troubleshooting. Just sadness. 😢
So let’s fix that! 🚀
✅ The Right Way: Write-Output & Logging
Instead of Write-Host
, use Write-Output
. Unlike Write-Host
, Write-Output
actually sends data through the PowerShell pipeline, which means you can capture, store, and analyze it later.
Write-Output "Script started at $(Get-Date)"
This can be redirected to a file or stored in a variable.
Want to make it even better? Log it to a file:
"[$(Get-Date)] Script started." | Out-File -FilePath "C:\Logs\MyScript.log" -Append
📝 Start-Transcript: Your Best Friend
If you need a full session log, use Start-Transcript
. This command captures everything that happens in your session, making troubleshooting much easier.
Start-Transcript -Path "C:\Logs\MyScriptSession.log" -Append
At the end of your script, stop it:
Stop-Transcript
Now you have a full record of commands and outputs. 🧐
📂 Structuring Logs for Automation
Logs should be structured, so they’re easy to parse. Instead of just dumping raw text, use structured formats like CSV or JSON:
$logEntry = [PSCustomObject]@{
Timestamp = Get-Date
Message = "Something happened"
Level = "INFO"
}
$logEntry | ConvertTo-Json | Out-File -FilePath "C:\Logs\StructuredLog.json" -Append
This makes logs machine-readable and easy to analyze. 🤖
🖥️ Writing Logs to the Event Viewer
For system-wide logging, use the Windows Event Log. This is perfect for production environments where logs need to be centralized. Note: Writing to the event log requires administrator privileges.
Write-EventLog -LogName Application -Source "MyPowerShellScript" -EntryType Information -EventId 1001 -Message "Script executed successfully."
To use this, ensure your script registers an event source first:
New-EventLog -LogName Application -Source "MyPowerShellScript"
🛠️ A Handy Logging Function
Here’s a reusable function to log messages in a structured and readable way:
Function Write-Log {
Param (
[Parameter(Mandatory = $true)]
[string]$Message,
[ValidateSet("INFO", "WARNING", "ERROR")]
[string]$Level = "INFO",
[string]$LogFile = "C:\Logs\MyScript.log"
)
$logEntry = "[$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")] [$Level] $Message"
$logEntry | Out-File -FilePath $LogFile -Append
Write-Output $logEntry
}
# Example Usage:
Write-Log -Message "Script started successfully." -Level INFO
Write-Log -Message "Potential issue detected!" -Level WARNING
Write-Log -Message "Critical error encountered!" -Level ERROR
# Example with timestamped log file:
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
$logFileName = "C:\Logs\MyScript_$timestamp.log"
Write-Log -Message "Script started successfully with timestamped log." -Level INFO -LogFile $logFileName
This function ensures logs are structured and categorized for easier troubleshooting. 🔍
🚀 Centralized Logging with Ohlala SmartOps
Building logging infrastructure for your EC2 fleet can be time-consuming—but what if monitoring, logging, and troubleshooting were handled automatically in Microsoft Teams?
Ohlala SmartOps provides AI-powered infrastructure monitoring with built-in logging capabilities:
- Real-time alerts for errors and warnings directly in Teams
- Automatic log analysis and troubleshooting suggestions
- Query your infrastructure logs in natural language
- No need to maintain custom logging functions or parse log files manually
Instead of building complex logging pipelines, let SmartOps handle it—giving your team 24/7 AI-powered monitoring and troubleshooting assistance for just $199/month.
🎬 Conclusion
Logging is critical for troubleshooting and automation. Stop using Write-Host
, and start logging properly with:
✅ Write-Output
for standard messages
✅ Out-File
or Start-Transcript
for script logs
✅ ConvertTo-Json
for structured data
✅ Write-EventLog
for system-wide logging (requires admin privileges)
✅ A reusable Write-Log
function for structured messages
Future you (and your IT team) will thank you. 🙌
🔗 For more info, check out Microsoft’s official documentation:
Happy logging! 📜💻