PowerShell Pass Named parameters to ArgumentList

前端 未结 7 1951
没有蜡笔的小新
没有蜡笔的小新 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:49

    If you're willing to skip Invoke-Command altogether...

    Your script could look like this:

    ([string]$args).split('-') | %{ 
        if ($_.Split(' ')[0].ToUpper() -eq "P1") { $P1 = $_.Split(' ')[1] } 
        elseif ($_.Split(' ')[0].ToUpper() -eq "P2") { $P2 = $_.Split(' ')[1] }
        elseif ($_.Split(' ')[0].ToUpper() -eq "P3") { $P3 = $_.Split(' ')[1] } 
    }
    
    Write-Output "P1 Value :" $P1
    Write-Output "P2 Value :" $P2
    Write-Output "P3 Value :" $P3
    

    And you would call it like this:

    D:\test.ps1 -P1 1 -P2 2 -P3 3

提交回复
热议问题