Looping through a hash, or using an array in PowerShell

后端 未结 7 1649
暗喜
暗喜 2020-12-22 21:13

I\'m using this (simplified) chunk of code to extract a set of tables from SQL Server with BCP.

$OutputDirectory = \"c:\\junk\\\"
$ServerOption =   \"-SServe         


        
7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-22 21:36

    Christian's answer works well and shows how you can loop through each hash table item using the GetEnumerator method. You can also loop through using the keys property. Here is an example how:

    $hash = @{
        a = 1
        b = 2
        c = 3
    }
    $hash.Keys | % { "key = $_ , value = " + $hash.Item($_) }
    

    Output:

    key = c , value = 3
    key = a , value = 1
    key = b , value = 2
    

提交回复
热议问题