Get-AzureRmWebApp:过滤结果有意外行为
Get-AzureRmWebApp: filtering results has unexpected behavior
我在 Powershell 中键入以下内容,以列出我所有的 Azure Web 应用程序的名称:
Get-AzureRmWebApp | % { $_.Name }
它输出:
coolum-exercise-web-app
practice-web-app
AzureSandbox
但是我想根据名称过滤此输出。我输入:
Get-AzureRmWebApp | ? { $_.Name -like "coolum-exercise-web-app" } | % { $_.Name }
我希望只看到一个输出。相反,我看到
coolum-exercise-web-app
practice-web-app
AzureSandbox
为什么没有应用名称过滤器?
如果我直接在 Get-AzureRmWebApp
上使用 -Name
参数,它会起作用:
Get-AzureRmWebApp -Name "coolum-exercise-web-app" | % { %_.Name }
输出:
coolum-exercise-web-app
但为什么 where-object
无法按预期应用过滤器?
这里有一些非常令人费解的行为:如果您将 Get-AzureRmWebApp
括在方括号中,过滤器将按您预期的方式工作。
(Get-AzureRmWebApp) | ? { $_.Name -like "coolum-exercise-web-app" } | % { $_.Name }
输出:
coolum-exercise-web-app
谁能解释一下这种行为?为什么将命令括在方括号中会使过滤起作用?
请尝试:(注意括号)
(Get-AzureRmWebApp) | ? { $_.Name -like 'cool*' }
看起来整个 where 子句被视为 Get-AzureRmWebApp 的默认命名参数。这就是为什么您必须用方括号将 CmdLet 与 where 子句分开。
实际上 Get-AzureRmWebApp returns 一个列表,而其他 CmdLet 像 Get-AzureRmVM returns 单个对象。
Get-AzureRmWebApp | gm
Get-AzureRmVM | gm
这是一个已知错误:#1544 Get-AzureRmWebApp - unable to pipe into select-object
Get-AzureRmWebApp
的结果是一个列表。您会期望列表中的每一项都通过管道 item-by-item 发送。相反,整个列表作为单个对象通过管道发送一次。
演示:
Get-AzureRmWebApp | % { $_.GetType().FullName }
显示器
System.Collections.Generic.List`1[[Microsoft.Azure.Management.WebSites.Models.Site, Microsoft.Azure.Management.Websites, Version=1.0.0.2, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]
同时
(Get-AzureRmWebApp) | % { $_.GetType().FullName }
显示器
Microsoft.Azure.Management.WebSites.Models.Site
Microsoft.Azure.Management.WebSites.Models.Site
Microsoft.Azure.Management.WebSites.Models.Site
错误发生是因为底层 C# 代码调用 WriteObject(sendToPipeline = list), when it should invoke WriteObject(sendToPipeline = list, enumerateCollection = true)
将调用包装在括号中的行为将返回的列表分配给本地临时对象。然后这个本地临时对象的行为就像一个普通列表。
我希望 Azure 团队解决这个问题,因为这会给倒霉的自动化脚本编写者带来意想不到的后果。
比如我原来的调用:
Get-AzureRmWebApp | ? { $_.Name -like "coolum-exercise-web-app" } | % { $_.Name }
被解释为 "If any of the values has a Name
like coolum-exercise-web-app
, then display all of the values."
编辑(2019 年 3 月)
我已经用 Azure Az Powershell Modules 测试了这个,我可以看到这个问题已经解决了。
我在 Powershell 中键入以下内容,以列出我所有的 Azure Web 应用程序的名称:
Get-AzureRmWebApp | % { $_.Name }
它输出:
coolum-exercise-web-app
practice-web-app
AzureSandbox
但是我想根据名称过滤此输出。我输入:
Get-AzureRmWebApp | ? { $_.Name -like "coolum-exercise-web-app" } | % { $_.Name }
我希望只看到一个输出。相反,我看到
coolum-exercise-web-app
practice-web-app
AzureSandbox
为什么没有应用名称过滤器?
如果我直接在 Get-AzureRmWebApp
上使用 -Name
参数,它会起作用:
Get-AzureRmWebApp -Name "coolum-exercise-web-app" | % { %_.Name }
输出:
coolum-exercise-web-app
但为什么 where-object
无法按预期应用过滤器?
这里有一些非常令人费解的行为:如果您将
Get-AzureRmWebApp
括在方括号中,过滤器将按您预期的方式工作。
(Get-AzureRmWebApp) | ? { $_.Name -like "coolum-exercise-web-app" } | % { $_.Name }
输出:
coolum-exercise-web-app
谁能解释一下这种行为?为什么将命令括在方括号中会使过滤起作用?
请尝试:(注意括号)
(Get-AzureRmWebApp) | ? { $_.Name -like 'cool*' }
看起来整个 where 子句被视为 Get-AzureRmWebApp 的默认命名参数。这就是为什么您必须用方括号将 CmdLet 与 where 子句分开。
实际上 Get-AzureRmWebApp returns 一个列表,而其他 CmdLet 像 Get-AzureRmVM returns 单个对象。
Get-AzureRmWebApp | gm
Get-AzureRmVM | gm
这是一个已知错误:#1544 Get-AzureRmWebApp - unable to pipe into select-object
Get-AzureRmWebApp
的结果是一个列表。您会期望列表中的每一项都通过管道 item-by-item 发送。相反,整个列表作为单个对象通过管道发送一次。
演示:
Get-AzureRmWebApp | % { $_.GetType().FullName }
显示器
System.Collections.Generic.List`1[[Microsoft.Azure.Management.WebSites.Models.Site, Microsoft.Azure.Management.Websites, Version=1.0.0.2, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]
同时
(Get-AzureRmWebApp) | % { $_.GetType().FullName }
显示器
Microsoft.Azure.Management.WebSites.Models.Site
Microsoft.Azure.Management.WebSites.Models.Site
Microsoft.Azure.Management.WebSites.Models.Site
错误发生是因为底层 C# 代码调用 WriteObject(sendToPipeline = list), when it should invoke WriteObject(sendToPipeline = list, enumerateCollection = true)
将调用包装在括号中的行为将返回的列表分配给本地临时对象。然后这个本地临时对象的行为就像一个普通列表。
我希望 Azure 团队解决这个问题,因为这会给倒霉的自动化脚本编写者带来意想不到的后果。
比如我原来的调用:
Get-AzureRmWebApp | ? { $_.Name -like "coolum-exercise-web-app" } | % { $_.Name }
被解释为 "If any of the values has a Name
like coolum-exercise-web-app
, then display all of the values."
编辑(2019 年 3 月)
我已经用 Azure Az Powershell Modules 测试了这个,我可以看到这个问题已经解决了。