Write-Output/Error is streamlined

and will send output to the console
This commit is contained in:
2023-10-05 14:09:27 -04:00
parent bb18c3436d
commit 8962142931
+4 -14
View File
@@ -59,17 +59,15 @@ Function Setup-LogFile {
# Start-Process "cmd.exe" -ArgumentList '/C echo y | some-command'
Function Write-Output {
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$Message)
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$LogEntry = "${Timestamp} - ${Message}"
Add-Content -Path $Global:LogFile -Value $LogEntry
Add-Content -Path $Global:LogFile -Value "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") - ${Message}"
Microsoft.Powershell.Utility\Write-Output $Message
}
# Log an error to the log file
Function Write-Error {
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$Message)
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$LogEntry = "${Timestamp} - Error: ${Message}"
Add-Content -Path $Global:LogFile -Value $LogEntry
Add-Content -Path $Global:LogFile -Value "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") - Error: ${Message}"
Microsoft.Powershell.Utility\Write-Error $Message
}
# Function to determine if the current user context is an Administrator
@@ -155,14 +153,6 @@ Function Run-Script {
}
}
}
$LogFileContent = (Get-Content $LogFile)
# Write the log file output to the console
If ( $LogFileContent ) { Write-Output $LogFileContent }
# Delete the log file if it's empty
Else { Remove-Item $LogFile -Force }
}
# Return a Powershell version object from a string