Write array values to Event Viewer?

Copper Contributor

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?

19 Replies
I think changing "Active Node List:$activeNode" to "Active Node List:$($activeNode)" might help?
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?

 

PS C:\Users\jschneider>  $activenode.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    DataRow                                  System.Object
Ok... And what does $activenode.tostring() give you? (If possible?)

@Harm_Veenstra 

 

PS C:\Users\jschneider> $activenode.tostring()
System.Data.DataRow

This is driving me nuts!  It makes no sense.

https://stackoverflow.com/questions/12944824/powershell-parse-system-data-datarow-to-string , In your case, something like

$activenodeOut = $activenodeOut+ ($activenode[$i].ItemArray -join ",")

Did that work out for you?

Sorry, haven't had a chance to test this yet, but should by tomorrow.
Any update?

@Harm_Veenstra Sorry for the delay.

Changed the code to this:

$activenodeOut = $activenodeOut+ ($activenode[$i].ItemArray -join ",")
Write-EventLog –LogName Application –Source eRxAppServer –EntryType Information –EventID 920 –Message "Active Node List:$activenodeOut"

Now all I see is this line in Event Viewer:

Active Node List:

 

No worries, and ok... What does the $activenodeOut variable contain?
I do a query of a SQL Server Listener and it returns the names of the nodes, with the active node first in the list.
The ItemArray should work on that Type.. So strange :(

Yes, as I mentioned above, this is such a simple little thing but has been driving me nuts why it doesn't work!

Could you share the part where you query the SQL Server Listener?

@Harm_Veenstra Here's the PS code I've been using for the past 6-7 years for the query:

 

#
#	SQL statement to find the active primary node
#
$sqlQuery = "Select distinct ags.primary_replica
From sys.dm_hadr_availability_replica_states     ars
Inner Join sys.dm_hadr_availability_group_states  ags On ags.group_id = ars.group_id
Where ars.role_desc = 'PRIMARY'"

#
#	Run the SQL query against the listener
#
$SourceDatabase = "master"
Push-Location
$activeNode = invoke-sqlcmd -ServerInstance $AOListener -Database $SourceDatabase -Query $sqlQuery
Pop-Location

@johnmsch Found a nice module which could help, https://www.powershellgallery.com/packages/ConvertFrom-DataRow/0.9.2 . Run Install-Module -Name ConvertFrom-DataRow and Import-Module -Name ConvertFrom-DataRow. You can now convert your variable using that to a hashtable or an object. I converted it to a hashtable and then you can use:

function hashToArray($hash){
$arr=@();
[string[]] $arr = ($hash | Out-String -Stream) -ne '' | select -Skip 2
return $arr
}

to convert it to a array which you can use in your write-eventlog.

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.