Forum Discussion

johnmsch's avatar
johnmsch
Copper Contributor
Feb 07, 2023

Write array values to Event Viewer?

Trying to debug an old PowerShell script that contains a small array, containing 5 -7 entries, each about 10-15 bytes long).  I'm trying to write the contents of the array ($activeNode) to the Event Viewer.  I tried this:

 

 

Write-EventLog –LogName Application –Source AppServer –EntryType Information –EventID 920 –Message "Active Node List:$activeNode"

 

 

 but the entry in Event Viewer comes out like this:

 

 

Active Node List:System.Data.DataRow

 

 

I know I'm missing something obvious.

How do I get the actual contents of the $activeNode array to show up?

  • I think changing "Active Node List:$activeNode" to "Active Node List:$($activeNode)" might help?
    • johnmsch's avatar
      johnmsch
      Copper Contributor
      Thanks for the quick reply, but still getting the same result in EV:
      Active Node List:System.Data.DataRow
  • Tested this with a [array]$activenode=get-content d:\temp\test.txt variable which give me an Event with this as Event Data

    <Data>Active Node List:********************** PowerShell transcript start Start time: 20220129124958 Username: T14\HarmV RunAs User: T14\HarmV Configuration Name: Machine: T14 (Microsoft Windows NT 10.0.22000.0) Host Application: C:\Program Files\PowerShell\7\pwsh.dll -file c:\temp\x2.ps1 Process ID: 24136 PSVersion: 7.2.1 PSEdition: Core GitCommitId: 7.2.1 OS: Microsoft Windows 10.0.22000 Platform: Win32NT PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.10032.0, 6.0.0, 6.1.0, 6.2.0, 7.0.0, 7.1.0, 7.2.1 PSRemotingProtocolVersion: 2.3 SerializationVersion: 1.1.0.1 WSManStackVersion: 3.0 ********************** Transcript started, output file is c:\temp\x.txt ********************** PowerShell transcript end End time: 20220129124958 **********************</Data>

    If you do a $activenode.gettype(), what does that give you?
    • johnmsch's avatar
      johnmsch
      Copper Contributor

       

      PS C:\Users\jschneider>  $activenode.gettype()
      
      IsPublic IsSerial Name                                     BaseType
      -------- -------- ----                                     --------
      True     False    DataRow                                  System.Object
  • The issue with your PowerShell script is that you are trying to output an array as a string, which is causing it to display as "System.Data.DataRow" in the Event Viewer. To display the actual contents of the array, you will need to convert the array to a string first.

    One way to do this is by using the "-join" operator in PowerShell. This operator joins the elements of an array into a single string, with a specified delimiter. For example, if your array is named $activeNode, you could convert it to a comma-separated string like this:

    $activeNodeString = $activeNode -join ", "

    This will create a string that contains all the elements of the $activeNode array, separated by a comma and a space.

    Then, you can use the $activeNodeString variable in your Write-EventLog command to output the contents of the array:

    Write-EventLog –LogName Application –Source AppServer –EntryType Information –EventID 920 –Message "Active Node List: $activeNodeString"

    This should output the actual contents of the $activeNode array in the Event Viewer.

Resources