Solved

Writing "Here-Strings" to event log in powershell

Posted on 2013-10-22
7
1,739 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

This article explains how to install and use the NTBackup utility that comes with Windows Server.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

751 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