I am writing a PowerShell script and one of the requirements is to store error messages in a SQL server table.
The problem here is that error messages can come in different forms. In this particular case, the error is as follows.
"Exception calling "open" with "0" argument(s): "login failed for user 'DOMAIN\USER'."
I have managed to store this in a PowerShell variable, the issue is how can I then insert it into a SQL Server table without errors. The double quote is resulting in errors with the insert.
I have tried to escape the double quotes by adding "$the_error
". This did not help.
The way in which the error is generated is as follows.
$return_Value = PsFunction
PsFunction returns a custom object, of which error_message is one of the members together with other values.
Now, here is what I am doing
$query = "insert into table (error_message) values ('{0}') " -f $return_value.error_message
I am having to replace the single and double quotes, feels like a fudge, however would be keen to insert the errors as it appears during execution.