Passing a hashtable as an argument to a function in PowerShell

后端 未结 2 1862
悲&欢浪女
悲&欢浪女 2020-12-11 02:33

I have a problem in a PowerShell script:

When I want to pass a Hashtable to a function, this hashtable is not recognized as a hashtable.

function get         


        
2条回答
  •  误落风尘
    2020-12-11 03:00

    I'm not really sure what to comment here, it seems self-explanatory. If not, leave a comment and I'll clarify.

    $ExampleHashTable = @{
        "one" = "the loneliest number"
        "two" = "just as bad as one"
    }
    
    Function PassingAHashtableToAFunctionTest {
        param(
            [hashtable] $PassedHashTable,
            [string] $AHashTableElement
        )
    
        Write-Host "One is ... " 
        Write-Host $PassedHashTable["one"]
        Write-Host "Two is ... " 
        Write-Host $AHashTableElement
    }
    
    PassingAHashtableToAFunctionTest -PassedHashTable $ExampleHashTable `
        -AHashTableElement $ExampleHashTable["two"]
    

    Output:

    One is ... 
    the loneliest number
    Two is ... 
    just as bad as one
    

提交回复
热议问题