使用Powershell从CSV提取数据以用于字段/函数

时间:2019-10-24 09:38:29

标签: xml powershell

源数据样本:

    <Appointment affiliation="Hospital One" deptname="Cardiology" divname="" deptcode="821" title="ASST CLIN PROF"/>
  </Person>
  <Person lifenumber="27901" lname="WINNER" mname="" fname="KURT" affiliation="Hospital One" email="kurt.WINNER@mss.edu" building="Annenberg" floor="17 TH FL" room="17-44" phone="(212) 241-1234" hrstatus="A" active_direct_user="hirsck01" active_direct_provider="MSSMCAMPUS">
    <Appointment affiliation="Hospital One" deptname="Pediatrics" divname="" deptcode="852" title="PROF LECTR"/>
  </Person>
  <Person lifenumber="30899" lname="OLYMPIA" mname="R" fname="MARTIN" affiliation="Hospital One" email="martin.OLYMPIA@mss.edu" building="" floor="" room="" phone="" hrstatus="A" active_direct_user="gellem03" active_direct_provider="HOSPITAL">
    <Appointment affiliation="Hospital One" deptname="Neurology" divname="" deptcode="841" title="ASSOC CLN PROF"/>
    <Appointment affiliation="Hospital One" deptname="Neurology" divname="" deptcode="105" title="ASSOC ATTN"/>
  </Person>
  <Person lifenumber="31183" lname="SCOOBY" mname="" fname="JAMES" affiliation="Hospital Two" email="" building="" floor="" room="" phone="" hrstatus="A" active_direct_user="" active_direct_provider="">
    <Appointment affiliation="Elmhurst/Queens Hospital" deptname="Otolaryngology" divname="" deptcode="A35" title="O.R. TECH"/>
  </Person>

代码示例:感谢Vonpryz,他提供了一种修改的方式来提取数据,但是现在我仍然坚持试图找出如何针对选定生命数字的CSV进行提取。有没有办法让@lifenumber呼叫与CSV中的一组生命号码相冲突?

[xml]$p = get-content C:\Scripts\source\Hospitalone_XML.xml
# Search by lifenumber
$nl = $p.SelectNodes('/People/Person[@lifenumber="27091"]')
# Check the email
$nl.email
$nl.lname
$nl.fname
# How many appointments?
$nl.appointment.count
# See the titles
$nl.appointment | % { $_.title }
$nl.appointment | % { $_.deptcode }

1 个答案:

答案 0 :(得分:0)

假设您有一个带有lifenumber列的csv文件:

$CSV = Import-Csv numbers.csv
[xml]$p = Get-Content C:\Scripts\source\Hospitalone_XML.xml

foreach($row in $CSV){
    $people = $p.SelectNodes(('/People/Person[@lifenumber="{0}"]' -f $row.lifenumber))
    # work with $people here
}