PowerShell Pass Named parameters to ArgumentList

前端 未结 7 1958
没有蜡笔的小新
没有蜡笔的小新 2021-02-20 08:14

I have a PowerShell script that accepts 3 named parameters. Please let me know how to pass the same from command line. I tried below code but same is not working. It assigns the

7条回答
  •  被撕碎了的回忆
    2021-02-20 08:52

    Use a hashtable, indeed!

    #TestPs1.ps1
    Param (
        [string]$P3,
        [string]$P2,
        [string]$P1
    )
    Write-Output "P1 Value :" $P1
    Write-Output "P2 Value:" $P2
    Write-Output "P3 Value :" $P3
    
    $params = @{
        P3 = 3
        P2 = 2
    }
    #(just to prove it doesn't matter which order you put them in)
    $params["P1"] = 1;
    #Trhough the use of the "Splat" operator, we can add parameters directly onto the module
    & ".\TestPs1.ps1" @params
    

    outputs:

    P1 Value :
    1
    P2 Value:
    2
    P3 Value :
    3
    

提交回复
热议问题