I am trying to view the results of a sql query into a remote server. My issue is that on return i see the first value repeated for each of the other values.
Here is the code:
#Connect to VPN
cls
C:
cd "C:\Program Files (x86)\Cisco Systems\VPN Client"
& ".\vpnclient.exe" connect WWVPN1 user sceris pwd ******
$vendorNumber = "2130196"
$vendorName = ""
$invoiceNumber = "1362433"
$conn = New-Object System.Data.SqlClient.SqlConnection("Data Source=wwfinance; Initial Catalog=ScerIS; Integrated Security=False; uid=Peter; pwd=*****; MultipleActiveResultSets=true")
## Open DB Connection
$conn.Open()
$sqlText = "SELECT UdiValue1, UdiValue37, UdiValue38, UdiValue3
FROM ScerIS.dbo.indexedRangesView_4
WHERE (UdiValue37 like '%$vendorName%' OR UdiValue38 like '$VendorNumber') AND UdiValue3 = '$invoiceNumber'"
$cmd = New-Object System.Data.SqlClient.SqlCommand($sqlText, $conn)
$Reader = $cmd.ExecuteReader()
while ($Reader.Read()) {
$ArchiveDate = $Reader.GetValue($1)
$VendorNumber = $Reader.GetValue($2)
$VendorName = $Reader.GetValue($3)
$InvoiceNumber = $Reader.GetValue($4)
}
write-host $ArchiveDate
write-host $VendorNumber
write-host $VendorName
write-host $InvoiceNumber
$conn.close()
#Disconnect from VPN
cd "C:\Program Files (x86)\Cisco Systems\VPN Client"
& ".\vpnclient.exe" disconnect
The output will show the archive date 4 times once for each write-host. How can i successfully get the other values to display?
Sample Output
9/4/2015 12:00:00 AM 9/4/2015 12:00:00 AM 9/4/2015 12:00:00 AM 9/4/2015 12:00:00 AM
$ArchiveDate,$VendorNumber,$VendorName, and$InvoiceNumberare all going to have the value of the last iteration of the loop. Given what you're doing here, I might use a DataAdapter instead of a DataReader, but its not clear what you're trying to display or why.