Powershell cmdlet忽略数组参数



我正在尝试使用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的文档说明签名是

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

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

这只是一个猜测,但-Sku $GatewaySku ' 之后是否有多余的空格或制表符?这可能会产生你所描述的行为。基本上,这将被解释为:

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

当以这种方式使用backtick时,这是一个常见的陷阱。通常建议不要这样做。这是因为backtick不是延续,而是转义字符,所以之后的任何都将被转义。当您这样使用它时,转义的是换行符,因此您可以将参数放在单独的行上,但如果中间有其他空白,这将中断。

最佳实践是将所有内容都写在一行中,或者如果有太多的参数无法保持可读性,可以使用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

最新更新