From 896214293161672b04e81d35dbc2337fda6efe6a Mon Sep 17 00:00:00 2001 From: David Yoder Date: Thu, 5 Oct 2023 14:09:27 -0400 Subject: [PATCH] Write-Output/Error is streamlined and will send output to the console --- Tools.ps1 | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/Tools.ps1 b/Tools.ps1 index 6df9f3d..b14bd6f 100644 --- a/Tools.ps1 +++ b/Tools.ps1 @@ -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