Forum Discussion
johnmsch
Feb 07, 2023Copper Contributor
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 ...
Varun_Ghildiyal
Mar 07, 2023Iron Contributor
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.
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.