PowerShell 5.1 使用 ExpandedProperty,但未获得所需的输出



Given:
PowerShell 5.1

如果我运行以下内容:

Get-Service -ComputerName WKSP000D1E3F -DisplayName *bit*

我明白这个:

Status   Name               DisplayName                           
------   ----               -----------                           
Stopped  BDESVC             BitLocker Drive Encryption Service    
Stopped  Check Point Bit... Check Point Endpoint Security Bitlo...

但是我想同时扩展"名称"和"显示名称",所以我这样做:

Get-Service -ComputerName WKSP000D1E3F -DisplayName *bit* | Select -ExpandProperty Name 

并得到这个:

BDESVC
Check Point Bitlocker Management

如何获取所有带有列标题的列,就像上面的第一个输出一样,除了没有省略号?

Status   Name               DisplayName                           
------   ----               -----------                           
Stopped  BDESVC             BitLocker Drive Encryption Service    
Stopped  Check Point Bit... Check Point Endpoint Security Bitlo...
Get-Service -DisplayName *wmi*
Status   Name               DisplayName
------   ----               -----------
Stopped  wmiApSrv           WMI Performance Adapter
Running  WMIRegistrationSe… Intel(R) Management Engine WMI Provid…

如果没有显示所需的输出,我发现自己使用| Out-GridView

Get-Service -DisplayName *wmi* | 
Out-GridView

外格视图结果

或者,如果您未设置为使用表视图,则可以使用| Format-List

Get-Service -DisplayName *wmi* | 
Format-List
Name                : wmiApSrv
DisplayName         : WMI Performance Adapter
Status              : Stopped
DependentServices   : {}
ServicesDependedOn  : {}
CanPauseAndContinue : False
CanShutdown         : False
CanStop             : False
ServiceType         : Win32OwnProcess
Name                : WMIRegistrationService
DisplayName         : Intel(R) Management Engine WMI Provider Registration
Status              : Running
DependentServices   : {}
ServicesDependedOn  : {}
CanPauseAndContinue : False
CanShutdown         : False
CanStop             : True
ServiceType         : Win32OwnProcess

如果您只需要Status,Name,DisplayName标头,

Get-Service -DisplayName *wmi* | 
Select-Object -Property Status, Name, DisplayName | 
Format-List
Status      : Stopped
Name        : wmiApSrv
DisplayName : WMI Performance Adapter
Status      : Running
Name        : WMIRegistrationService
DisplayName : Intel(R) Management Engine WMI Provider Registration

...只是一个显示问题(数据完整存在)。

如果使用Select-Object-ExpandProperty参数,则根据定义,您只会获取属性,并且由于这些值在您的情况下是字符串,因此这就是您得到的全部(尽管没有截断)。

若要防止显示截断,请通过管道传输到Format-Table-AutoSize,但请注意,这要求 PowerShell 在开始显示输出之前先从输入命令收集所有输出(有关替代方法,请参阅下一节)。

Get-Service -ComputerName WKSP000D1E3F -DisplayName *bit* | Format-Table AutoSize

注意:由于控制台(终端)窗口宽度始终是限制因素,因此-AutoSize可能导致显示的列减少

如果可能,您可以通过-Property仅指定那些不感兴趣的列来排除不感兴趣的列(您可以省略此参数名称,因为它是位置隐含的);例如:

Get-Service -ComputerName WKSP000D1E3F -DisplayName *bit* |
Format-Table -Property Status, Name -AutoSize

避免-AutoSize及其收集一切优先行为的替代方案

使用
  • -Wrap,它不会截断,但使用(人为的)换行符不适合列的值分散到多行中。

    Get-Service -ComputerName WKSP000D1E3F -DisplayName *bit* | Format-Table -Wrap
    
  • 如果您提前知道感兴趣列的最大宽度,则可以使用计算属性来规定列的宽度;例如:

    Get-Service -ComputerName WKSP000D1E3F -DisplayName *bit* |
    Format-Table Status, 
    @{ Expression='Name'; Width=40 },
    DisplayName
    

最新更新