I have the following snippet of code in my powershell script
$sqlHDR = "SELECT * FROM OPENROWSET ('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=" + $dir + $m + ".xls;HDR=NO','SELECT * FROM [" + $y + "`$B1:Z1]')"
$doHDR = invoke-sqlcmd -query $sqlHDR -ServerInstance $sqlserver -Database $db
$doHDR | get-member -membertype properties
This is part of a loop that cycles through hundreds of Excel spreadsheets. The result is a dynamically changing set of columns from F1 through to F*n* (n being the unknown final column number) which have only one row, with a single value in each row.
I wondered if I could use get-member to somehow loop through the columns in the dataset to gain access to the values in field? Or am I going about this completely backwards, is there a better way to loop through the values in a single datarow?
Alternatively is there a way to loop through the property names themselves?
Many thanks!
Does this help any?
$doHDR.psobject.properties |
where {$_.name -like 'something*'} |
foreach {set-variable -Name $_.name -Value $_.value}
Update with example foreach