PowerShell遍历对象数组并找到一个

时间:2018-05-28 17:03:12

标签: powershell

我在VSTS返回的PowerShell中有一个对象,看起来像这样(!important:Tomalak编辑的这些项目周围没有@ {):

  @{
    objectId="ae97aa664f6c0cf803790a1fad78fc43119c6b8a"
    gitObjectType="tree"
    commitId="696e3462ef20058c4f3183d28568196d30f7e18f"
    path="/"
    isFolder=True
    url="https://xxx.visualstudio.com/_apis/git/repositories/11f25afb-4130-499b-ae49-d3fe24b0fadb/items//?versionType=Branch&versionOptions=None"
  },
  @{
    objectId="e12eb8bd619733492279a8f8c8e0de99378dd299"
    gitObjectType="blob"
    commitId="696e3462ef20058c4f3183d28568196d30f7e18f"
    path="/Hello.ps1" 
    url="https://xxx.visualstudio.com/_apis/git/repositories/11125afb-4730-119b-ae49-d3fe24b0fadb/items//Hello.ps1?versionType=Branch&versionOptions=None"
  },
  @{
    objectId="a4579b63a5cf68ca6a95937d56f15bac47a4f768"
    gitObjectType="blob" 
    commitId="696e3462ef20058c4f3183d28568196d30f7e18f"
    path="/README.md"
    url="https://xxx.visualstudio.com/_apis/git/repositories/48f25afb-4730-499b-ae49-d3fe24b0fadb/items//README.md?versionType=Branch&versionOptions=None"
  },
  @{
    objectId="46e9b94f49b5a687c889f451c3a9b315e6044b7c" 
    gitObjectType="blob"
    commitId="696e3462ef20058c4f3183d28568196d30f7e18f"
    path="/file.txt"
    url="https://xxx.visualstudio.com/_apis/git/repositories/11f25afb-4130-499b-ae49-d3fe24b0fadb/items//file.txt?versionType=Branch&versionOptions=None"
  }

它保存在变量$xmlDataRepo中。当我做的时候

$xmlDataRepo | ForEach-Object {
    write-host $xmlDataRepo.value.commitId
} 

我得到了(!重要的是,这些值实际上是在PowerShell窗口中单行编写的!):

696e3462ef20058c4f3183d28568196d30f7e18f 696e3462ef20058c4f3183d28568196d30f7e18f 696e3462ef20058c4f3183d28568196d30f7e18f 696e3462ef20058c4f3183d28568196d30f7e18f

看起来不错。但想要做的是找到path=/file.txt的对象,然后只获取此对象objectId

我已尝试过这个但它不起作用:

$filename = "file.txt"

ForEach-Object -InputObject $xmlDataRepo.value -Process {
  if ($xmlDataRepo.value.path -eq "/$filename") {
    $oId = $xmlDataRepo.value.objectId
  } else {
    $oId = "0000000000000000000000000000000000000000"
  }
}

这里有什么问题?

更新1:

我现在尝试了以下代码:

$xmlDataRepo | ForEach-Object {
    if ($_.value.path -eq "/$filename") {
        write-host $_.value.objectId
    }
}

我也试过

$obj = ($xmlDataRepo | Where-Object { $_.value.path -eq "/$filename" })

if ($obj) { 
    $oldId = $_.value.commitId
} else {
    $oldId = "0000000000000000000000000000000000000000"
}

这是我调试时的对象。但我总是得到4个值(!)而不是只有文件名等于我变量中的文件名的值。

enter image description here

我还试过像

这样的Santiagos解决方案
$objectId = ($xmlDataRepo | Where-Object { $_.value.path -eq "/$filename" }).value.objectId
write-host "---$objectId"

但这也给了我四个元素而不是一个元素。

enter image description here

3 个答案:

答案 0 :(得分:0)

你可以使用Where-Object而不是自己迭代。见下文。

$filename = "file.txt"
$objectId = ($xmlDataRepo | Where-Object { $_.value.path -eq "/$filename" }).value.objectId

答案 1 :(得分:0)

试试这个:

$xmlDataRepo | ForEach-Object {
  if ($_.value.path -eq "/$filename") {
    $oId = $_.value.objectId
  } else {
    $oId = "0000000000000000000000000000000000000000"
  }
} 

答案 2 :(得分:0)

现在这对我有用:

$xmlDataRepo.value | ForEach-Object {
    if ($_.path -eq "/file.txt")
    {
        $oldId = $_.commitId
        $changeType = "edi“
    }
}