Solved

Writing "Here-Strings" to event log in powershell

Posted on 2013-10-22
7
1,619 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Try..
$($error[0] | Out-string)

Open in new window

0
 

Author Comment

by:netshops
Comment Utility
Winner winner chicken dinner! Thanks for the help.
0

Featured Post

Why spend so long doing email signature updates?

Do you spend loads of your time carrying out email signature updates? Not very interesting are they? Don’t let signature updates get you down. Let Exclaimer Cloud - Signatures for Office 365 make managing email signatures a breeze.

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now