Skip to content
Advertisement

Powershell how to use SQL syntax on system.data.datatable foreach variable lookup?

I am using a data table in Powershell ISE on 2 large csv files. The second file is used to lookup various facilities by ID number found in File1. So, File2 has either 1 or many facilities depending on where they go. I’m trying to do a standard foreach loop, but not sure how to populate where the number variable would go, since it’s SQL syntax (is my understanding). $Script:MappingTable2 has at least one matching ID, but sometimes many. I have loaded these into data tables and can’t figure out how to add the PowerShell equivalent of $_ for lookup. So, where 2017 will pull all the facilities that have 2017 as the ID, I would like to look these up per $Code in File1. $Script:MappingTable2.select(“PRACT_ID = ‘$Code.ID'”) won’t work, but if just the number is there (like below) it grabs all the rows related to the ID. Or just one if only a one to one match. Hoping to add the array as a single combined string to a new 3rd csv cell in a new column, which is supposed to be a merging of the 2 files. Thx

foreach ($Code in $File1) {
$Script:MappingTable2.select("PRACT_ID = '2017'")}

Advertisement

Answer

I figured it out! The $Code.gettype() was BaseType – System.Object Name – DataRow. When I set it to a [string] it works. So this works and is very fast.

foreach ($Code in $File1) {
[string]$ID = $Code.PRACT_ID
$Script:MappingTable2.select("PRACT_ID = '$ID'")}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement