Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through a hash, or using an array in PowerShell

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?

like image 739
Sylvia Avatar asked Jan 26 '12 07:01

Sylvia


People also ask

How do I loop through an array in PowerShell?

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 ...

How do I iterate through a Hashtable in PowerShell?

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.

What are differences between an array and hash in PowerShell?

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.

What is GetEnumerator in PowerShell?

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.


1 Answers

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:

Variable Setup

PS> $hash = @{     a = 1     b = 2     c = 3 } PS> $hash  Name                           Value ----                           ----- c                              3 b                              2 a                              1 

Option 1: GetEnumerator()

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 

Option 2: Keys

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 

Additional information

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.

like image 71
VertigoRay Avatar answered Oct 08 '22 18:10

VertigoRay