Today, we'll be discussing how to make your PowerShell custom actions log their output to a log file.
This involves modifying the custom action itself by adding the functions that will log, either a message or an error.
First of all, we will have to decide the log's location. For this, I will be using the %temp% location.
Code: Select all
$logFileLocation = $env:temp + "\CustomActionLog.txt"To do this, we can use the "test-path" cmdlet.
Code: Select all
if (-not(test-path $logFileLocation)){
new-item -itemtype File -path $logFileLocation
}Code: Select all
function Log {
    param ([string]$Message)
    $timeStamp = Get-Date -Format "HH:mm:ss.fff"
    $outMessage = $timeStamp + ": " + $Message
    $outMessage | Out-File -FilePath $logFileLocation -Append -Encoding UTF8
}Code: Select all
function LogError {
    param ([string]$Message)
    
	$ErrorMessage = "[Error] " + $Message
    Log($ErrorMessage)
}
Hope you'll find this useful.
 
 Best regards,
Catalin