简体   繁体   English

Powershell cmdlet 忽略数组参数

[英]Powershell cmdlet ignores an array parameter

I am trying to use a PowerShell cmdlet to create a resource in Azure cloud:我正在尝试使用 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

This, however, ends with:然而,这以:

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

$GatewayIPconfig.GetType() yields $GatewayIPconfig.GetType()产生

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

and the documentation for the cmdlet states that the signature is并且 cmdlet 的文档指出签名是

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

Is this not the proper way to pass an array argument to a cmdlet?这不是将数组参数传递给 cmdlet 的正确方法吗?

It's just a guess, but do you maybe have a stray space or tab after -Sku $GatewaySku ' ?这只是一个猜测,但您是否在-Sku $GatewaySku '之后有一个杂散的空间或标签? That might produce exactly the behavior you described.这可能会产生您所描述的行为。 Basically that would be interpreted as:基本上这将被解释为:

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

It's a common pitfall when using the backtick that way.以这种方式使用反引号时,这是一个常见的陷阱。 It is often recommended not to do it.通常建议不要这样做。 It's because the backtick is not a continuation but an escape character, so anything after it will be escaped.这是因为反引号不是延续而是转义字符,所以它后面的任何内容都会被转义。 When using it as you do, it's the line-break that is escaped, so you can put the arguments on separate lines, but if there's other whitespace in-between, this will break.像您一样使用它时,会转义换行符,因此您可以将参数放在单独的行上,但如果中间有其他空格,则会中断。

Best practice is to write everything in one line, or if there are too many arguments to keep it readable, you can use splatting :最佳做法是将所有内容都写在一行中,或者如果有太多参数无法保持可读性,您可以使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM