我想从存储在变量中的参数的其他脚本中启动script1.ps1.
$para = "-Name name -GUI -desc ""this is the description"" -dryrun"
. .\script1.ps1 $para
我在script1.ps1中得到的args看起来像:
args [0]: - 名称-GUI -desc"这是描述"-dryrun
所以这不是我想要的.有谁知道如何解决这个问题?
thx lepi
PS:不确定变量将包含多少个参数以及它们将如何排序.
你需要使用splatting操作符.查看powershell团队博客或访问stackoverflow.com.
这是一个例子:
@'
param(
[string]$Name,
[string]$Street,
[string]$FavouriteColor
)
write-host name $name
write-host Street $Street
write-host FavouriteColor $FavouriteColor
'@ | Set-Content splatting.ps1
# you may pass an array (parameters are bound by position)
$x = 'my name','Corner'
.\splatting.ps1 @x
# or hashtable, basically the same as .\splatting -favouritecolor blue -name 'my name'
$x = @{FavouriteColor='blue'
Name='my name'
}
.\splatting.ps1 @x
Run Code Online (Sandbox Code Playgroud)
在你的情况下你需要像这样调用它:
$para = @{Name='name'; GUI=$true; desc='this is the description'; dryrun=$true}
. .\script1.ps1 @para
Run Code Online (Sandbox Code Playgroud)
使用Invoke-Expression是另一种选择:
$para = '-Name name -GUI -desc "this is the description" -dryrun'
Invoke-Expression -Command ".\script1.ps1 $para"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3646 次 |
| 最近记录: |