PsObject in a String

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Itamar Tziger Itamar Tziger 2 years, 4 months ago.

  • Author
    Posts
  • #17315
    Profile photo of Itamar Tziger
    Itamar Tziger
    Participant

    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 ?

  • #17316
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    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

  • #17318
    Profile photo of Sam Boutros
    Sam Boutros
    Participant

    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
    Profile photo of Itamar Tziger
    Itamar Tziger
    Participant

    Daniel Krebs – Tnx
    this is what I'm looking

    Tnx
    Itamar

You must be logged in to reply to this topic.