I'm using this (simplified) chunk of code to extract a set of tables from SQL Server with BCP.
$OutputDirectory = "c:\junk\" $ServerOption = "-SServerName" $TargetDatabase = "Content.dbo." $ExtractTables = @( "Page" , "ChecklistItemCategory" , "ChecklistItem" ) for ($i=0; $i -le $ExtractTables.Length – 1; $i++) { $InputFullTableName = "$TargetDatabase$($ExtractTables[$i])" $OutputFullFileName = "$OutputDirectory$($ExtractTables[$i])" bcp $InputFullTableName out $OutputFullFileName -T -c $ServerOption }
It works great, but now some of the tables need to be extracted via views, and some don't. So I need a data structure something like this:
"Page" "vExtractPage" , "ChecklistItemCategory" "ChecklistItemCategory" , "ChecklistItem" "vExtractChecklistItem"
I was looking at hashes, but I'm not finding anything on how to loop through a hash. What would be the right thing to do here? Perhaps just use an array, but with both values, separated by space?
Or am I missing something obvious?
PowerShell array is a collection of the single datatype or the multiple different datatypes values and looping through the array means scanning each value one by one using different available loops in PowerShell like While, For, Foreach, Do-While, etc and performing the operation on each item if required and this loop ...
In the first method, the one that I prefer, you can use the GetEnumerator method of the hash table object. In the second method, instead of iterating over the hash table itself, we loop over the Keys of the hash table. Both of these methods produce the same output as our original version.
An array, which is sometimes referred to as a collection, stores a list of items. A hash table, which is sometimes called a dictionary or an associative array, stores a paired list of items.
GetEnumerator. A hash table is a single PowerShell object, to sort, filter or work with the pipeline you can unwrap this object into it's individual elements with the GetEnumerator() method.
Shorthand is not preferred for scripts; it is less readable. The %{} operator is considered shorthand. Here's how it should be done in a script for readability and reusability:
PS> $hash = @{ a = 1 b = 2 c = 3 } PS> $hash Name Value ---- ----- c 3 b 2 a 1
Note: personal preference; syntax is easier to read
The GetEnumerator() method would be done as shown:
foreach ($h in $hash.GetEnumerator()) { Write-Host "$($h.Name): $($h.Value)" }
Output:
c: 3 b: 2 a: 1
The Keys method would be done as shown:
foreach ($h in $hash.Keys) { Write-Host "${h}: $($hash.$h)" }
Output:
c: 3 b: 2 a: 1
Be careful sorting your hashtable...
Sort-Object may change it to an array:
PS> $hash.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Hashtable System.Object PS> $hash = $hash.GetEnumerator() | Sort-Object Name PS> $hash.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array
This and other PowerShell looping are available on my blog.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With