在一行中运行两个PowerShell命令时,为什么更改了输出格式? [英] Why is the output format changed when running two PowerShell commands in one line?

查看:58
本文介绍了在一行中运行两个PowerShell命令时,为什么更改了输出格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行两个以分号分隔的PowerShell命令时,我得到了意外的结果.第二个命令的输出更改.如果以相反的顺序运行它们,则看不到第二个命令输出.

I'm getting unexpected results when executing two PowerShell commands separated by a semicolon. The output of the second command changes. If I run them in reverse order, I don't see the second command output.

在这里,我只是想获得一个时间戳和一个用户属于AD的组列表(单行).

Here I'm simply trying to get a time stamp and a list of groups a user belongs to in AD, as a one-liner.

如果我运行此行,则会得到以下输出:

If I run this line, i get the following output:

Get-ADPrincipalGroupMembership username | Select-Object name

name
----
Domain Users
CL-Inventory-Group
...

但是,如果我运行以下命令,此行为将发生变化:

However if I run the following, this behavior changes:

get-date; Get-ADPrincipalGroupMembership username | Select-Object name

Wednesday, April 3, 2019 2:31:35 PM

name : Domain Users


name : CL-Inventory-Group


...

还很陌生,如果我反向运行,这意味着我在第一个命令后说了get-date,则列出组之后,日期戳永远不会显示.

Stranger yet, If i run the in reverse, meaning i say get-date after the first command, the date stamp never shows up after the groups are listed.

我不正确地分隔命令吗?

Am I improperly separating commands?

推荐答案

tl; dr :

在提示符下(交互地)提交多个; 分隔的命令仍将其输出发送到单个管道(您可以将提交的每个命令行都视为隐式脚本)文件).

Submitting multiple ;-separated commands at the prompt (interactively) still sends their output to a single pipeline (you can think of each command line submitted as an implicit script file).

简而言之:在您的情况下,第一个命令输出的自动显示格式也确定了第二个命令的显示格式,因此哪个命令最重要

In short: in your case, the first command output's automatic display formatting also determined the display formatting for the 2nd command, so which command comes first matters:

  • 获取日期;Get-ADPrincipalGroupMembership用户名|选择对象名称

  • 锁定了 Get-Date 之后所有输出的 Format-List 隐式使用,这解释了 Get-ADPrincipalGroupMembership ... 命令.
  • locked in implicit use of Format-List for all output following Get-Date, which explains the each-property-on-its-own line output from the Get-ADPrincipalGroupMembership ... command.

如果我执行相反的操作,这意味着我在第一个命令后说了get-date,则列出组之后,日期戳永远不会显示.

If i run the reverse, meaning i say get-date after the first command, the date stamp never shows up after the groups are listed.

    类型为 [pscustomobject]
  • Select-Object 输出实例,由于在这种情况下它们只有1个属性,因此它们被锁定在表格显示,即隐式使用 Format-Table ,并将所选属性作为唯一列,即此处只是 Name .由于 Get-Date 输出的 [datetime] 类型没有 Name 属性,因此 Get-Date 的输出实际上是 invisible .
    • Select-Object output instances of type [pscustomobject], which, due to their having just 1 property in this case, locked in tabular display, i.e., implicit use of Format-Table, with the selected property as the only column, i.e., just Name here. Since the [datetime] type output by Get-Date doesn't have a Name property, Get-Date's output was effectively invisible.
    • 请继续阅读以获取背景信息和完整规则.

      Read on for background information and the complete rules.

      PowerShell的默认显示格式已针对相同类型的对象进行了优化,这是典型的情况.

      PowerShell's default display formatting is optimized for objects of the same type, as that is the typical case.

      如果管道包含类型的 mix ,则默认情况下产生的特定格式取决于:

      If a pipeline contains a mix of types, the specific formatting that results by default depends on:

      • 管道中对象的 order
      • 及其默认格式设置行为

      有关详细信息,请参见下一部分.

      See the next section for details.

      您可以使用显式的 Format-* 调用来控制格式;您可以在第二个命令上使用 Format-Table 强制表格输出:

      You can use explicit Format-* calls to control the formatting; in your case, you can use Format-Table on your 2nd command to force tabular output:

      Get-Date; Get-ADPrincipalGroupMembership username | Select name | Format-Table
      

      注意事项: Format-* cmdlet 的输出是格式化说明,而不是原始数据,使此输出不适合进行进一步的程序处理 .

      Caveat: The output from Format-* cmdlets are formatting instructions rather than the original data, which makes this output unsuitable for further programmatic processing.

      在没有显式格式命令( Format-Table Format-List ,...)的情况下, PowerShell自动选择合适的显示格式基于给定对象的 type :

      In the absence of explicit formatting commands (Format-Table, Format-List, ...), PowerShell automatically chooses a suitable display format, based on a given object's type:

      • 如果存在给定类型,PowerShell将使用预定义的格式说明(请参阅
查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆