Complete the code to catch errors using Try-Catch.
try { Get-Content 'file.txt' -ErrorAction Stop } catch [[1]] { Write-Error $_.Exception.Message }
The catch block uses Exception to capture error details.
Complete the code to write error messages to a log file.
try { Remove-Item 'data.csv' -ErrorAction Stop } catch { $_.Exception.Message | Out-File -FilePath [1] -Append }
Errors are logged to 'error.log' file using Out-File with -Append.
Fix the error in the code to properly log errors with timestamps.
$timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss' try { Get-Item 'missing.file' -ErrorAction Stop } catch { "$timestamp - Error: [1]" | Out-File -FilePath 'error.log' -Append }
The correct property to get the error message is Exception.Message.
Fill both blanks to create a function that logs errors with severity and message.
function Log-Error {
param($errorMessage)
$logEntry = "Severity: [1] - Message: [2]"
$logEntry | Out-File -FilePath 'error.log' -Append
}The severity is set to 'High' and the message uses the parameter $errorMessage.
Fill all three blanks to log errors with timestamp, severity, and message in a script.
$time = Get-Date -Format 'HH:mm:ss' $severity = [1] $errorMsg = [2] "[$time] Severity: $severity - Error: $errorMsg" | [3] -FilePath 'error.log' -Append
Severity is set to 'Critical', error message is from Exception.Message, and Out-File writes to the log.
