如何调用PowerShell脚本从PowerShell脚本中获取命名参数?
foo.ps1:
param(
[Parameter(Mandatory=$true)][String]$a='',
[Parameter(Mandatory=$true)][ValidateSet(0,1)][int]$b,
[Parameter(Mandatory=$false)][String]$c=''
)
#stuff done with params here
Run Code Online (Sandbox Code Playgroud)
bar.ps1
#some processing
$ScriptPath = Split-Path $MyInvocation.InvocationName
$args = "-a 'arg1' -b 2"
$cmd = "$ScriptPath\foo.ps1"
Invoke-Expression $cmd $args
Run Code Online (Sandbox Code Playgroud)
错误:
Invoke-Expression : A positional parameter cannot be found that accepts
argument '-a MSFT_VirtualDisk (ObjectId =
"{1}\\YELLOWSERVER8\root/Microsoft/Windo...).FriendlyName -b 2'
Run Code Online (Sandbox Code Playgroud)
这是我最近的尝试 - 我尝试过googling的多种方法似乎没有用.
如果我从shell终端运行foo.ps1,因为./foo.ps1 -a 'arg1' -b 2它按预期工作.
powershell ×1