首页 > 解决方案 > 无法在 PowerShell 中组合 2 个命令

问题描述

我正在尝试使以下 PowerShell 命令工作但失败了。任何人都知道如何让这个工作?
我知道 Expression 标签中有问题,但不知道是什么。

Get-BrokerDesktop -MaxRecordCount 5000 | Where{($_.LastConnectionUser -eq $null) -and ($_.AssociatedUserNames -ne $null)} | Select MachineName,@{ Name = 'CreationDate';  Expression = {Get-ProvVM -MaxRecordCount 5000 -Filter "VMName -eq $_.HostedMachineName" | Select CreationDate}} | ft -AutoSize

它只给出第一列输出。显示第二列名称但没有数据。
如果我如下分别运行第二个命令,我将得到输出。

Get-ProvVM -MaxRecordCount 5000 -Filter "VMName -eq 'ComputerName'" | Select CreationDate

HostedMachineName是 的一个属性Get-BrokerDesktop
VMName是 的一个属性Get-ProvVM

标签: powershellpowershell-3.0powershell-4.0

解决方案


@Theo,谢谢,您的解决方案有效,但我必须根据需要添加-ExpandProperty。这是我的最终命令,它完全按预期工作。

Get-BrokerDesktop -MaxRecordCount 5000 | Where{($_.LastConnectionUser -eq $null) -and ($_.AssociatedUserNames -ne $null)} | Select MachineName,@{ Name = 'CreationDate';  Expression = {Get-ProvVM -MaxRecordCount 5000 -Filter "VMName -eq '$($_.HostedMachineName)'" | Select CreationDate -ExpandProperty CreationDate}} | ft -AutoSize

推荐阅读