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 ...
Feb 23, 2023
No worries, and ok... What does the $activenodeOut variable contain?
johnmsch
Feb 28, 2023Copper Contributor
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.
- Mar 07, 2023
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. - johnmschMar 06, 2023Copper Contributor
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 01, 2023Could you share the part where you query the SQL Server Listener?
- johnmschMar 01, 2023Copper Contributor
Yes, as I mentioned above, this is such a simple little thing but has been driving me nuts why it doesn't work!
- Feb 28, 2023The ItemArray should work on that Type.. So strange 😞