PowerShell 将命名参数传递给 ArgumentList [英] PowerShell Pass Named parameters to ArgumentList

查看:40
本文介绍了PowerShell 将命名参数传递给 ArgumentList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 entire value to P3 only. My requirement is that P1 should contain 1, P2 should 2 and P3 should be assigned 3.

Invoke-Command -ComputerName server -FilePath "D:	est.ps1" -ArgumentList  {-P1 1 -P2 2 -P3 3}

Ihe below is script file code.

Param (
    [string]$P3,
    [string]$P2,
    [string]$P1
)
Write-Output "P1 Value :" $P1
Write-Output "P2 Value:" $P2
Write-Output "P3 Value :" $P3

解决方案

One option:

$params = @{
P1 = 1
P2 = 2 
P3 = 3
}

$ScriptPath = 'D:Test.ps1'

$sb = [scriptblock]::create(".{$(get-content $ScriptPath -Raw)} $(&{$args} @params)")

Invoke-Command -ComputerName server -ScriptBlock $sb

这篇关于PowerShell 将命名参数传递给 ArgumentList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆