简体   繁体   English

来自其他文件的PowerShell XML节点值如果其他节点匹配

[英]PowerShell XML Node Value from another file IF other Nodes -match

I have a script that's generating a xml file based on a .csv that's going to act as payload through a webservice (RESTapi). 我有一个脚本,它基于.csv生成一个xml文件,该文件将通过Web服务(RESTapi)充当有效负载。

The payload requires me to generate some GUIDs which is fine as long as I'm generating new ones. 有效载荷要求我生成一些GUID,只要我生成新的GUID就可以了。 In order to successfully update data through the API i need to specify a GUID that already exist on each user in the receiving system. 为了通过API成功更新数据,我需要指定接收系统中每个用户已经存在的GUID。

So the first thing I do is a GET request to the system to export all users. 所以我做的第一件事是向系统发出GET请求以导出所有用户。 I then check users in my generated .xml file against the .xml from the GET request. 然后,我在生成的.xml文件中检查来自GET请求的.xml中的用户。

If users match, I want to extract the GUID that's existing in the export to my generated output. 如果用户匹配,我想将导出中存在的GUID提取到我生成的输出中。 And this is where I'm stuck. 这就是我被困住的地方。

For example I've would match: 例如,我会匹配:

<CVs>
  <CV>
    <GUID></GUID>
    <EMPLOYEE>
        <IDENTIFIER>aa</IDENTIFIER>
    </EMPLOYEE>
  </CV>
</CVs>

Against: 反对:

    <CV>
        <GUID>800b96cd-56e9-4587-a9db-41c2a4095d04</GUID>
        <EMPLOYEE>
           <USERNAME>aa</USERNAME>
        </EMPLOYEE>
    </CV>
    <CV>
        <GUID>a07ac517-c7d5-4766-89b6-9101260f6f53</GUID>
        <EMPLOYEE>
           <USERNAME>bb</USERNAME>
        </EMPLOYEE>
    </CV>
    <CV>
        <GUID>84d82720-3e06-4802-9206-69f25b2aa46d</GUID>
        <EMPLOYEE>
           <USERNAME>cc</USERNAME>
        </EMPLOYEE>
    </CV>
  </CVs>

And since "aa" is a match, I would like this output: 既然“aa”是匹配的,我想要这个输出:

<CVs>
  <CV>
    <GUID>800b96cd-56e9-4587-a9db-41c2a4095d04</GUID>
    <EMPLOYEE>
        <IDENTIFIER>aa</IDENTIFIER>
    </EMPLOYEE>
  </CV>
</CVs>

This is my working code for producing everything, except getting that GUID from another .xml if two nodes are matching. 这是我生成所有内容的工作代码,除非在两个节点匹配时从另一个.xml获取GUID。

$docTemplate = @'
  <CV>
    <GUID></GUID>
    <EMPLOYEE>
        <IDENTIFIER>$($cert.Identifier)</IDENTIFIER>
    </EMPLOYEE>
    <PAGE id="8">
  $($certs -join "`n")
    </PAGE>
  </CV>
'@

# Per-certificate template.
$entryTemplate = @'
    <RECORD>
      <GUID></GUID>
      <FIELD id="21">
        <DATA>
          <GUID></GUID>
          <VALUE>$($cert.Certificate)</VALUE>
        </DATA>
      </FIELD>
      <FIELD id="36">
        <DATA><VALUE>$($cert.From)</VALUE></DATA>
      </FIELD> 
      <FIELD id="22">$($cert.ExpireDate)
        <DATA><VALUE>$($cert.to)</VALUE></DATA>
      </FIELD>
    </RECORD>
'@

$cvData = Import-Csv demo.csv -Delimiter ';' | Group-Object Identifier -ov grp | ForEach-Object {

  $certs = foreach ($cert in $_.Group) {
    $ExecutionContext.InvokeCommand.ExpandString($entryTemplate)  
  }
  $ExecutionContext.InvokeCommand.ExpandString($docTemplate)
}

$rootTemplate = @'
<CVs>
$($cvData -join "`n")
</CVs>
'@
$output = $ExecutionContext.InvokeCommand.ExpandString($rootTemplate) | Set-Content -LiteralPath 'cvoutput.xml'



#Add GUID
$xmlFile = '.\cvoutput.xml'
[xml]$xmlDoc = Get-Content $xmlFile

#Add Record GUID
foreach ($element in $xmlDoc.CVs.CV.PAGE.RECORD)
{
    [string]$element.GUID = New-Guid
}

#Check if Node exists (certifications require a GUID in the Cert Data Node)
foreach ($element in $xmlDoc.CVs.CV.PAGE.RECORD.FIELD | Where-Object  {$_.name -match "Certifications"}) {
  [string]$element.DATA.GUID = New-Guid
}


$xmlDoc.Save('cv-data.xml')

If I got the gist correctly, you want to either create a new guid or use an existing guid that you get from a XML file containing a list of all existing users / guids. 如果我正确地得到了要点,你想要创建一个新的guid,或者使用从包含所有现有用户/ guid列表的XML文件中获得的现有guid。

Here's a minimal example on how you would read a file (I used a here string for the example instead), parse it as a list of user / guid, then check whether or not the user received is in the existing XML and get the corresponding GUID or create a new one. 这是一个关于如何读取文件的最小示例(我在此处使用了此处的字符串),将其解析为user / guid列表,然后检查用户是否收到了现有XML并获取相应的GUID或创建一个新的。

.

$OutTemplate = @"
<CVs>
  <CV>
    <GUID>{0}</GUID>
    <EMPLOYEE>
        <IDENTIFIER>{1}</IDENTIFIER>
    </EMPLOYEE>
  </CV>
</CVs>
"@

$AllUsers = @"
<CVS>
  <CV>
        <GUID>800b96cd-56e9-4587-a9db-41c2a4095d04</GUID>
        <EMPLOYEE>
           <USERNAME>aa</USERNAME>
        </EMPLOYEE>
    </CV>
    <CV>
        <GUID>a07ac517-c7d5-4766-89b6-9101260f6f53</GUID>
        <EMPLOYEE>
           <USERNAME>bb</USERNAME>
        </EMPLOYEE>
    </CV>
    <CV>
        <GUID>84d82720-3e06-4802-9206-69f25b2aa46d</GUID>
        <EMPLOYEE>
           <USERNAME>cc</USERNAME>
        </EMPLOYEE>
    </CV>
  </CVS>
"@


#   From File 
#   $XML = [xml](Get-Content -Path 'PathToXML.xml' -Raw)

$XML = [xml]$Allusers
$Nodes = $XML.SelectNodes('//CV') | Select @{'Name' = 'EmployeeID' ; 'Expression' = { $_.EMPLOYEE.USERNAME } }, GUID

# Instead of a fixed array, you'd have your received users here.
$Employees = @('aa', 'csd', 'ee')

foreach ($emp in $employees) {
    $Guid = $Nodes | Where EmployeeID -eq ($emp) | Select -First 1
    if ($Guid -eq $null) {
        $Guid = New-Guid
        $Color = 'DarkMagenta'
    }
    else {
        $Color = 'Cyan'
        $Guid = $Guid.Guid
    }

    $OutElement = $OutTemplate -f $guid, $emp

    Write-Host $OutElement -ForegroundColor $Color

}

The important part of this example is the : 这个例子的重要部分是:

$Nodes = $XML.SelectNodes('//CV') | Select @{'Name' = 'EmployeeID' ; 'Expression' = { $_.EMPLOYEE.USERNAME } }, GUID

From there, you have your list of existing user GUID and you can process all records against it and decide whether to use an existing GUID or create a new one. 从那里,您拥有现有用户GUID的列表,您可以处理所有记录,并决定是使用现有GUID还是创建新GUID。

Just like @sage-pourpre suggests the: 就像@ sage-pourpre建议:
$Nodes = $XML.SelectNodes('//CV') | Select @{'Name' = 'EmployeeID' ; 'Expression' = { $_.EMPLOYEE.USERNAME } }, GUID

With: 附:
foreach ($emp in $xmlDoc.CVs.CV ) { $Guid = $Nodes | Where EmployeeID -eq ($emp.EMPLOYEE.IDENTIFIER) | Select -First 1 [string]$emp.GUID = $Guid.GUID }

Solves the problem. 解决了这个问题。

Thank you! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PowerShell XML - 根据另一个节点值从节点获取值 - PowerShell XML - Get value from node depending on another nodes value 使用powershell将一个xml文件中的xml节点添加到另一个文件2中的特定现有节点中 - Adding xml nodes from one xml file into another with powershell into a specific existing node in File 2 根据另一个节点值从XML删除选定的重复节点 - Delete selected repeating node from XML based on another nodes value 使用Java中的SAX解析器从xml文件中提取xml节点(不是文本,而是完整的xml)以及其他测试节点 - extracting xml node(not text but complete xml ) and with other test nodes from xml file using SAX parser in java Powershell 从 xml 文件中删除空节点 - Powershell Delete empty nodes from xml file 如何使用Powershell从xml文件中仅导入父节点而保留所有子节点? - How to import only parent node leaving all the child nodes from xml file using powershell? 如何使用 PowerShell 从另一个文件更改 XML 中的值? - How to change value in XML from another file with PowerShell? 在PowerShell上将节点从一种XML导入到另一种XML - Import node from one XML to another on PowerShell 几个节点基于另一个节点值的XML值 - XML value frm few nodes based on another node value Xdocument从xml文件中的另一个注释获取元素节点值 - Xdocument getting element node value from another note in the xml file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM