Powershell cmdlet 忽略数组参数

Powershell cmdlet ignores an array parameter

我正在尝试使用 PowerShell cmdlet 在 Azure 云中创建资源:

$Gateway = New-AzApplicationGateway `
    -Name $GatewayName `
    -ResourceGroupName $ResourceGroupName `
    -Location $Location `
    -Sku $GatewaySku `
    -GatewayIPConfigurations $GatewayIPconfig `
    -FrontendIPConfigurations $FrontendIpConfig `
    -FrontendPorts $FrontEndPort `
    -Probes $HealthProbe `
    -BackendAddressPools $PlatformBackendPool, $ApiBackendPool `
    -BackendHttpSettingsCollection $PoolSettings `
    -Force

然而,这结束于:

cmdlet New-AzApplicationGateway at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
GatewayIPConfigurations[0]:

$GatewayIPconfig.GetType() 产量

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    PSApplicationGatewayIPConfiguration      Microsoft.Azure.Commands.Network.Models.PSChildResource

并且 cmdlet 的 documentation 指出签名是

New-AzApplicationGateway
   ...
   -GatewayIPConfigurations <PSApplicationGatewayIPConfiguration[]>
   ...

这不是将数组参数传递给 cmdlet 的正确方法吗?

这只是一个猜测,但您可能在 -Sku $GatewaySku ' 之后有一个杂散的 space 或制表符?这可能会产生您所描述的行为。基本上这将被解释为:

$Gateway = New-AzApplicationGateway `
    -Name $GatewayName `
    -ResourceGroupName $ResourceGroupName `
    -Location $Location `
    -Sku $GatewaySku
# (the rest of the arguments will be missing)

以这种方式使用反引号时,这是一个常见的陷阱。通常建议不要这样做。因为反引号不是延续而是一个escape字符,所以后面的anything都会被转义。当像你一样使用它时,它是被转义的line-break,所以你可以将参数放在单独的行中,但是如果还有其他白色space in-between,这将中断。

最好的做法是将所有内容写在一行中,或者如果参数太多而难以阅读,您可以使用 splatting:

$params = @{
    Name = $GatewayName
    ResourceGroupName = $ResourceGroupName
    Location = $Location
    Sku = $GatewaySku
    GatewayIPConfigurations = $GatewayIPconfig
    FrontendIPConfigurations = $FrontendIpConfig
    FrontendPorts = $FrontEndPort
    Probes = $HealthProbe
    BackendAddressPools = $PlatformBackendPool, $ApiBackendPool
    BackendHttpSettingsCollection = $PoolSettings
    Force = $true
}
$Gateway = New-AzApplicationGateway @params