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的documentation指出签名为

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)

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

最佳做法是将所有内容都写在一行中,或者如果有太多的参数使其难以阅读,则可以使用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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...