【问题标题】:Powershell : multiple Select-Object statements returning 1st occurrence results onlyPowershell:多个 Select-Object 语句仅返回第一次出现的结果
【发布时间】:2021-07-09 16:34:14
【问题描述】:

我在 Windows 8.1 上运行 PowerShell 4.0 版

我有一个名为 flow1.xml 的 xml 文档(来自 nifi 流),如下所示:

<flowController encoding-version="1.3">
  <rootGroup>
    <id>123</id>
    <name>BigTime</name>
    <position x="0.0" y= "0.0"/>
    <processGroup>
      <id>456</id>
      <name>SmallTime</name>
      <position x="1000.0" y="2000.0"/>
      <processGroup>
        <id>789</id>
        <name>TinyTime</name>
        <position x="3000.0" y="4000.0"/>
      </processGroup>
    <processGroup/>
  </rootGroup>
</flowController>

我有一个 powerShell 脚本 test.ps1 如下:

$filePath='flow1.xml'
$xmlDocument = New-Object -TypeName XML
$xmlDocument.Load($filePath)

$xmlDocument.flowController.rootGroup.processGroup |
Where-Object { $_.name -eq 'SmallTime'} |
Select-Object -Property {$_.id}

$xmlDocument.flowController.rootGroup.processGroup.processGroup |
Where-Object { $_.name -eq 'TinyTime'} |
Select-Object -Property {$_.id}

我在 Windows Cmd 框中从 PowerShell 运行;即 PS C\Temp>test.ps 当我运行它时,我得到了预期的输出:

Output:
$_.id
-----
456
789

当我更改第二个 Select_Object 时:

$xmlDocument.flowController.rootGroup.processGroup.processGroup |
Where-Object { $_.name -eq 'TinyTime'} |
Select-Object -Property {$_.name}

我明白了

Output:
$_.id
-----
456

我只从第一个子句中得到 $_id,而不是从第二个子句中得到 $_name。一般情况是 - 如果两个 Select-Object 语句都选择相同的参数,则两者都正确执行。例如,如果两个 Select-Object 语句都是 Select-Object -Property {$_.name} 我得到输出:

Output:
$_.name
-----
SmallTime
TinyTime

如果 Select-Object 语句不同,它似乎只执行第一个语句。 我尝试在第二个 Select-Object 之前再次阅读 xmlDocument :

$filePath='flow1.xml'
$xmlDocument = New-Object -TypeName XML
$xmlDocument.Load($filePath)

$xmlDocument.flowController.rootGroup.processGroup |
Where-Object { $_.name -eq 'SmallTime'} |
Select-Object -Property {$_.id}

$xmlDocument = New-Object -TypeName XML
$xmlDocument.Load($filePath)
$xmlDocument.flowController.rootGroup.processGroup.processGroup |
Where-Object { $_.name -eq 'TinyTime'} |
Select-Object -Property {$_.id}

但同样的事情也会发生。我只得到第一个 Select-Object 语句的结果。

有人知道为什么会这样吗?

【问题讨论】:

  • 您的 Powershell 代码中有拼写错误 - .eq 可能应该是 -eq$ 可能应该是 $_ 在几个位置。仔细检查您的代码。
  • 很多个错别字,你是不是在发帖前不小心删除了所有_的?

标签: xml powershell powershell-4.0 select-object


【解决方案1】:

这听起来很像家庭作业。下面的答案就是这样对待它的。换句话说,这些应该有助于指出一些问题,但这里的读者不应该为你做功课。

  • 请检查管道变量的正确格式。是 $_ 不是 $
    例如,$_.Name Google:PowerShell 管道变量

  • 查看比较运算符的使用。 PowerShell 中没有 .eq 之类的东西

  • 您的 XML 格式不正确,因此我不确定此脚本是如何通过 $xmlDocument.Load() 用于加载文件的语法看起来不错,并且您在正确的轨道上加载属性值。一旦 XML 格式正确,您只需使用正确的数据路径即可。

  • 您只需要一次读取 XML。

  • 您不需要将管道变量与 select 语句一起使用。只需属性名称即可。
    例如,Select-Object Name 这将返回 Name 属性的值

我希望这足以让你再次动起来。

【讨论】:

  • 抱歉有错别字。实际代码是正确的。我无法剪切和粘贴,因为它位于不同环境的不同服务器上,所以我不得不重新输入。错别字已修复。
  • 我已经尝试在 Select-Object 语句中删除管道变量。当我删除管道变量时,我得到了相同的描述。
  • 我已经更正并再次运行。我得到相同的行为 - 如果我选择不同的变量,我只会得到第一个 Select-Object 语句的结果。有谁知道为什么? (Ps,我是 PowerShell 新手,这不是家庭作业)。
【解决方案2】:

只需添加此注释即可结束。

不知道管道出了什么问题。同事也有类似的问题。所以我改变了策略。

我通过设置一个新变量深入了解了 $xmlDocument: $group = $xmlDocument.flowController.rootGroup.processGroup

并用适当的代码替换

foreach ($property in $group.property...){
   if ($property.name -eq 'oldValue'){
     parameter.value = 'newValue'
   }
}

这很有效,并且可以通过更复杂的搜索进一步阐述/复杂化。

【讨论】:

    猜你喜欢
    • 2021-07-21
    • 2011-02-16
    • 2022-11-17
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 2015-08-03
    • 2014-01-01
    相关资源
    最近更新 更多