Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Writing "Here-Strings" to event log in powershell

Posted on 2013-10-22
7
Medium Priority
?
1,877 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 2000 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 2000 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

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

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

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

704 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