I have a powershell script that needs to read from a config file with a simple Get-Content cmdlet:
foreach ($s in Get-Content $config_file)
If the config file is not where it is supposed to be I want to stop the script and log the error.
Is it possible to use the -ErrorAction with a cusom logging function with in the script?
foreach($s in Get-Content $config_file -ErrorAction mdie('Could not find config file')
mdie is a logging function that logs the error and exits the script.
Or is there a better way?
-G
Use
-ErrorAction Stopandtry..catchpattern. In thecatchblock an error object is represented by the variable$_.