我正在编写执行以下操作的 Powershell 代码
- 读取目录中 XML 文件的内容
- 对于目录 a 中的每个 XML 文件。打印 XML 文件的选定内容(不是所有节点) b. 将所选内容保存到名为 $securities 的哈希表中
我不能做 2(b)
这是我的代码:
$directory = 'C:\Scripts\Results'
[hashtable]$securities = @{}
$CUSIP=$NULL
$Int_Rate=$NULL
$Issue_Date=$NULL
$Maturity_Date=$NULL
foreach ($i in (Get-ChildItem $directory -Name)){
write-host $i
[xml]$xml = [xml](Get-Content $directory\$i)
$CUSIP = $xml.AuctionData.AuctionAnnouncement.CUSIP
Write-Host CUSIP $CUSIP
$Int_Rate = $xml.AuctionData.AuctionResults.InterestRate
Write-Host Interest Rate $Int_Rate
$Issue_Date = $xml.AuctionData.AuctionAnnouncement.IssueDate
Write-Host Issue Date $Issue_Date
$Maturity_Date = $xml.AuctionData.AuctionAnnouncement.MaturityDate
Write-Host Maturity Date $Maturity_Date
Write-Host `n
$securities.add=($i,$CUSIP,$Int_Rate,$Issue_Date,$Maturity_Date)
}
$securities
这是我对 $security 的输出
名称值
---- -----
CUSIP {Jul_2012.xml, 912828TE0, 0.125, 2012-07-31...}
这不是我想要的价值。我正在寻找的价值类似于
Jan_2011.xml CUSIP 912828PP9 利率 1.125 发行日期 2011-01-31 到期日 2021-01-15
Jan_2012.xml CUSIP 912828SA9 利率 0.125 发行日期 2012-01-31 到期日期 2022-01-15
Jul_2010.xml CUSIP 912828NM8 利率 1.250 发行日期 2010-07-15 到期日期 2020-07-15
Jul_2011.xml CUSIP 912828QV5 利率 0.625 发行日期 2011-07-29 到期日 2021-07-15
Jul_2012.xml CUSIP 912828TE0 利率 0.125 发行日期 2012-07-31 到期日期 2022-07-15
请指出我正确的方向。
编辑
嗨大卫
我试过
$securities.add($i, [pscustomobject]@{'CUSIP'=$CUSIP;'Int_Rate'=$Int_Rate;'Issue_Date'=$Issue_Date;'Maturity_Date'=$Maturity_Date})
并得到 $security 的以下输出
Name Value
---- -----
Jul_2012.xml @{CUSIP=912828TE0; Int_Rate=0.125; Issue_Date=2012-07-31; Maturity_Date=2022-07-15}
Jul_2011.xml @{CUSIP=912828QV5; Int_Rate=0.625; Issue_Date=2011-07-29; Maturity_Date=2021-07-15}
Jul_2010.xml @{CUSIP=912828NM8; Int_Rate=1.250; Issue_Date=2010-07-15; Maturity_Date=2020-07-15}
Jan_2012.xml @{CUSIP=912828SA9; Int_Rate=0.125; Issue_Date=2012-01-31; Maturity_Date=2022-01-15}
Jan_2011.xml @{CUSIP=912828PP9; Int_Rate=1.125; Issue_Date=2011-01-31; Maturity_Date=2021-01-15}
但我不知道如何引用它。
例如,当我试图为
$securities.Jul_2012.xml('CUSIP')
我得到错误:
You cannot call a method on a null-valued expression.
At C:\Scripts\treasury_holdings.ps1:24 char:1
+ $securities.Jul_2012.xml('CUSIP')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
代替
912828TE0
编辑:
谢谢大卫。
为了帮助其他人,这是最新的代码:
$directory = 'C:\Scripts\Results'
[hashtable]$securities = @{}
$CUSIP=$NULL
$Int_Rate=$NULL
$Issue_Date=$NULL
$Maturity_Date=$NULL
foreach ($i in (Get-ChildItem $directory -Name)){
write-host $i
[xml]$xml = [xml](Get-Content $directory\$i)
$CUSIP = $xml.AuctionData.AuctionAnnouncement.CUSIP
$Int_Rate = $xml.AuctionData.AuctionResults.InterestRate
$Issue_Date = $xml.AuctionData.AuctionAnnouncement.IssueDate
$Maturity_Date = $xml.AuctionData.AuctionAnnouncement.MaturityDate
$securities.add($i, [pscustomobject]@{'CUSIP'=$CUSIP;'Int_Rate'=$Int_Rate;'Issue_Date'=$Issue_Date;'Maturity_Date'=$Maturity_Date})
Write-Host CUSIP $securities[$i].CUSIP
Write-Host Interest Rate $securities[$i].Int_Rate
Write-Host Issue Date $securities[$i].Issue_Date
Write-Host Maturity Date $securities[$i].Maturity_Date
Write-Host `n
}