I am working on an array of files that match the names of tables in our SQL Server database. I want to query a subset of those tables, and use those table names to provide commands to their corresponding files.
Example, there is a customer table in SQL Server, so I have a customer.dat datafile.
I have queried 3 tables from SQL Server, and would like to use them to give commands to their 3 corresponding datafiles, but this is the output I get:
$Tables = Invoke-Sqlcmd -Inputfile $inputfile `
-ServerInstance $server `
-Database $Database `
-OutputAs DataRows
$Tables.ForEach({Write-Host $Tables.Item(0)})
For some reason, the output for all 3 tables comes out as 'System.Data.Datarow'. How do I get the values from this data?
I eventually want to put the desired output into a string for a dynamic filepath. Something like:
$Tables.ForEach({Get-ChildItem -Path $filepath\$_.dat})
But it gives me an error with a similar problem:
Get-ChildItem : Cannot find path 'filepath\System.Data.DataRow.dat' because it does not exist.
You see, it is inserting 'System.Data.DataRow' into the file name instead of the intended table name. Any help, or overview of the concepts I am attempting to use would be greatly appreciated!
Thank you


DataRowinstances then$Tables.ForEach({Write-Host $Tables.Item(0)})should just be$Tables.ForEach({ $_ })to enumerate each row and$Tables.ForEach({ Get-ChildItem -Path "$filepath\$($_.dat)" })for the other part, assming.datis an actual columnToString()result of the first DataRow. If you changed$Tables.Item(0)to$_you would see the same text, but at least then it would be the impliedToString()result of each row.