Feb 07 2023 01:26 PM
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?
Feb 07 2023 02:38 PM
Feb 07 2023 03:06 PM
Feb 08 2023 04:01 AM
Feb 08 2023 10:41 AM - edited Feb 08 2023 10:43 AM
PS C:\Users\jschneider> $activenode.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False DataRow System.Object
Feb 08 2023 10:46 AM
Feb 08 2023 01:02 PM
PS C:\Users\jschneider> $activenode.tostring()
System.Data.DataRow
This is driving me nuts! It makes no sense.
Feb 08 2023 01:52 PM
Feb 09 2023 12:04 PM - edited Feb 10 2023 01:36 PM
Did that work out for you?
Feb 13 2023 10:42 AM
Feb 23 2023 12:59 PM
@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:
Feb 23 2023 01:02 PM
Feb 28 2023 01:09 PM
Feb 28 2023 01:26 PM
Mar 01 2023 10:55 AM - edited Mar 01 2023 10:56 AM
Yes, as I mentioned above, this is such a simple little thing but has been driving me nuts why it doesn't work!
Mar 01 2023 11:07 AM
Mar 06 2023 01:45 PM
@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
Mar 07 2023 02:11 AM
@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.
Mar 07 2023 02:21 AM