Solved

Writing "Here-Strings" to event log in powershell

Posted on 2013-10-22
7
1,650 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
Do email signature updates give you a headache?

Are you constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
A brief introduction to what I consider to be the best editor for PowerShell.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

929 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

10 Experts available now in Live!

Get 1:1 Help Now