Author Posts

July 19, 2014 at 9:11 pm

this is my code
write-eventlog -computername $env:COMPUTERNAME -logname Application -source ShmeibarCheck -eventID 32 -message "Check Status of $Object.Server Status $Object.PortOpen"

and the event-log look like this
Check Status of @{Server=192.168.1.1;

How can I remove the hash and get my prop in the string ?

July 20, 2014 at 2:19 am

Hello Itamar,

Option 1: Tell PowerShell you want $Object.Server or $Object.PortOpen to be evaluated inside the string using the $( any expression which can be evaluated or executed ) syntax.

Write-Eventlog -ComputerName $env:COMPUTERNAME -LogName Application -Source ShmeibarCheck -EventId 32 -Message "Check Status of $( $Object.Server ) Status $( $Object.PortOpen )"

Option 2: Use Composite Formatting (http://preview.tinyurl.com/oef3n5o) which I usually prefer because the string can be saved in external files, databases, etc.

Write-Eventlog -ComputerName $env:COMPUTERNAME -LogName Application -Source ShmeibarCheck -EventId 32 -Message ("Check Status of {0} Status {1}" -f $Object.Server, $Object.PortOpen)

Best,
Daniel

July 20, 2014 at 2:42 am

You can loop through the elements of the $Object.Server array and write them individually to the log. That would post several event-32s in the log.

Alternatively, you can create a temp string variable and concatenate the $Object.Server array elements into it, and use it in your write-eventlog statement. Something like:

$Temp = $null; ForEach ($Item in $Object.Server) {$Temp += [string]$Item + " -"}
write-eventlog -computername $env:COMPUTERNAME -logname Application -source esent -eventID 103 -message "Check Status of $Temp Status $Object.PortOpen"

Can you post printout of how your $Object looks like?
Type in

 $Object | Select * 

and post the output here please..

July 20, 2014 at 2:48 am

Daniel Krebs – Tnx
this is what I'm looking

Tnx
Itamar