PsObject in a String

This topic contains 3 replies, has 3 voices, and was last updated by  Itamar Tziger 3 years, 11 months ago.

  • Author
  • #17315

    Itamar Tziger

    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=;

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

  • #17316

    Daniel Krebs

    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 ( 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)


  • #17318

    Sam Boutros

    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..

  • #17319

    Itamar Tziger

    Daniel Krebs – Tnx
    this is what I'm looking


You must be logged in to reply to this topic.