I am trying to create a script that connects to multiple HANA db’s, runs a query, then closes the connection/sends me the results. I am struggling on the connection piece, as it only seems to pull the query from one system, even though it says multiple connections were opened when I run the foreach loop to connect. Below is what I currently have for the script:
$password=[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR((($Credential.Password))))
$allServers = get-content C:\Users\Me\AD.txt #contents are listed as servername:portID ex: server1:30015
# loop through the servers array and collect the output in variable $result
$result = foreach($servername in $allServers) {
# Connect HANA Server
$conn = new-object System.Data.Odbc.OdbcConnection
$conn.connectionstring = "Driver={HDBODBC};SERVERNODE={$servername};UID={$env:USERNAME};PWD=$password}"
$conn.Open()
Write-Output $conn
$CurrentDate = "%" + ((get-date).AddDays(-1)).ToString('MM-dd') + "%"
$Query = "Query Details Here"
$cmd = New-object System.Data.Odbc.OdbcCommand($Query,$conn)
$dtab = New-Object System.Data.DataTable
$adap = New-Object System.Data.Odbc.OdbcDataAdapter($cmd)
$nrr = $adap.fill($dtab)
#Write-Output "Records selected: ${nrr}"
Write-Output $dtab}
$result | Out-GridView
I run into the same problem with both scripts. The trouble is when I run the first foreach loop, it returns connections for both DB’s, but when I check $conn, it only lists server 2. When I check the log info from the query, it only returns information from server 2. Is there something I am missing in order to get server 1 info as well, or is it not possible to do what I am asking?
Of course. You assign the variable inside the loop. That’s why you only get the status of the last assignment.
If I got it right … if you want to catch the output of all $nodes you have to assign the output of the loop to a variable or you have to add/collect the output inside the loop to a variable.
This:
Thanks for the assistance so far. I want to make sure I understand what you are saying. You are saying to add the $Result code above before the $conn information? This is still only connecting to server2, so I probably misunderstood. What we have below will pass both nodes to the $Result variable. Do I need another ForEach for the connectionstring? If I run this as it is, $Result will return the contents of the nodes.txt file, but when I check $conn, it still shows server2. I need it to connect to server1 and server2 and subsequently run the Query statement.