Solved

Writing "Here-Strings" to event log in powershell

Posted on 2013-10-22
7
1,698 Views
Last Modified: 2013-10-23
I am trying to get a multi-line variable to write to the event log in its entirety. This is the test strring:
$test = @"
Test
~~~~
TEst.
"@

Open in new window

When you echo out $test, it comes out just fine but when it is used in the below command i get an error (both are below). How can I get a multi-line variable (aka Here-String) to write to the event log?
Write-EventLog –LogName "Application" –Source "test" –EntryType "Error" –EventID 1 –Message $test

Open in new window

At C:\Scripts\test.ps1:9 char:112
+ ... –EventID 1 –Message $test
+                    ~~~~~~~~~~~~~~
The string is missing the terminator: ".
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
0
Comment
Question by:netshops
  • 4
  • 3
7 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 39592627
I don't see nay issue for adding the message using Here-Strings.. Probably some thing went wrong in copy paste.. :-) .. Can you attach the C:\Scripts\test.ps1?
0
 

Author Comment

by:netshops
ID: 39592669
Yea, shortly after posting this, i realized it was an encoding issue. I needed ANSI instead of UTF8. So the below works now:

$test2 = @"
Test
~~~~
TEst.
"@

#$test2 = "test\n\rtest3"

Write-EventLog -LogName "Application" -Source "test" -EntryType "Error" -EventID 1 -Message $test2

Open in new window


But for the actual script usage, I am trying to convert the global error variable to a here-string so that I can get more that one line in the event viewer:

} Catch {
	$fullError = @"
	$error[0]
	"@
	LogMsg "ERROR: $fullError" "Error"
	Exit "0x1"
}

Open in new window


Results In:
+     "@
+     ~~
White space is not allowed before the string terminator.
At C:\Scripts\ServiceRestart.ps1:156 char:9
+ } Catch {
+         ~
Missing closing '}' in statement block.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : WhitespaceBeforeHereStringFooter
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39592701
If you are trying to write the Exception message, the you can try with..

LogMsg "ERROR: $($error[0].Exception.Message)" "Error"

Open in new window


or

LogMsg "ERROR: $($_.Exception.Message)" "Error"

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 500 total points
ID: 39592725
White space is not allowed before the string terminator.
BTB, in your code, as the error says try by removing the White space before the string terminator.

} Catch {
	$fullError = @"
	$error[0]
"@
	LogMsg "ERROR: $fullError" "Error"
	Exit "0x1"
}

Open in new window

0
 

Author Comment

by:netshops
ID: 39594527
Is there a way to capture the entire error?

When i do this:
$error[0].Exception.Message

Open in new window

I get this:
The term 'ThisCmdlet-DoesNotExist' is not recognized as the name of a cmdlet, function, script file, or operable progra
m. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Open in new window


But the full error is actually:
ThisCmdlet-DoesNotExist : The term 'ThisCmdlet-DoesNotExist' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
At line:1 char:1
+ ThisCmdlet-DoesNotExist
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (ThisCmdlet-DoesNotExist:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Open in new window


How can i get the whole thing?
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39594545
Try..
$($error[0] | Out-string)

Open in new window

0
 

Author Comment

by:netshops
ID: 39594563
Winner winner chicken dinner! Thanks for the help.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
A brief introduction to what I consider to be the best editor for PowerShell.
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question