M J*_*nks 5 xml powershell loops
首先,我要承认我是一个Powershell(和编码)菜鸟。我在一些脚本中迷迷糊糊,但是我对任何接近能力的东西都没有要求。我希望一些更有经验的人可以使我走上正确的道路。
我正在尝试从多个XML文件中提取特定的元素数据,这些数据将用于填充另一个XML文件。我从中提取数据的文件是发票,我想获取发票号和时间戳并将这些值放入清单中。清单结构如下
<?xml version="1.0" encoding="utf-8"?>
<Manifest>
<Invoice>
<InvoiceID></InvoiceID>
<Timestamp></Timestamp>
</Invoice>
</Manifest>
Run Code Online (Sandbox Code Playgroud)
我从中提取的XML位于清单将保存到的目录的子目录中。为了简单起见,发票中的元素名称与清单中的相应元素相同。清单的文件夹结构为“ C:\Projects\Powershell\Manifest\Manifest.xml”,发票的文件夹结构为“ C:\Projects\Powershell\Manifest\Invoices\*.xml”。
使用以下代码,我可以仅从子目录“ ”中第一个 XML 的元素“ InvoiceID”和“ Timestamp” 获取数据。但是,该代码确实为每个发票文件创建了一个条目。它只是用从第一个文件获取的值填充每个元素。(因此,例如,如果我在“ ”目录中有三个Invoice XML文件,则会得到以下结果:三个复杂元素实例,每个实例都填充并在第一个文件中找到。因此,它计算文件数并输出一个相应数量的元素,只是从第一个元素获取数据。)\Invoices\Invoices<Invoice>InvoiceIDTimestamp
这是代码:
$files = Get-ChildItem "C:\Projects\Powershell\Manifest\Invoices\*.xml"
$xmlData = @"
<Invoice>
<InvoiceId>$InvID</InvoiceId>
<Timestamp>$Timestamp</Timestamp>
</Invoice>
"@
$Manifest = "C:\Projects\Powershell\Manifest\Manifest.xml"
ForEach ($file in $files) {
$xmldoc = [xml](Get-Content $file)
$InvID = $xmldoc.Manifest.Invoice.InvoiceID
$Timestamp = $xmldoc.Manifest.Invoice.Timestamp
ForEach ($xml in $xmldoc)
{
Add-Content $Manifest $xmlData
}}
Run Code Online (Sandbox Code Playgroud)
一旦弄明白了这一点,我就可以正确格式化输出文件的结束标记。
我知道我的循环肯定不正确,但是在仔细阅读这本书直到我的大脑受到伤害之后,我终于诉诸了这个问题。我想念/弄错什么明显的东西?
"..."和@"<newline>...<newline>"@字符串中的字符串插值(扩展)立即发生,并使用引用变量当时包含的值。
因此,在循环的每次迭代中都会输出相同的字符串(其值在循环之前确定foreach。
您的用例需要一种模板方法,其中字符串插值被延迟,并根据需要使用当时的变量值调用$ExecutionContext.InvokeCommand.ExpandString()[1]:
# Define the *template* string as a *literal* - with *single* quotes.
$xmlData = @'
<Invoice>
<InvoiceId>$InvID</InvoiceId>
<Timestamp>$Timestamp</Timestamp>
</Invoice>
'@
# ...
# ForEach ($file in $files) { ...
# Perform interpolation *on demand* with $ExecutionContext.InvokeCommand.ExpandString()
Add-Content $Manifest -Value $ExecutionContext.InvokeCommand.ExpandString($xmlData)
# }
Run Code Online (Sandbox Code Playgroud)
笔记:
变量引用也可以通过用 中的括起来显式地描述变量名称来嵌入{...},例如${InvID},这可能在某些情况下需要消除歧义。
为了嵌入表达式/命令输出,请使用$()子表达式运算符,如下所示。
为了逐字 $嵌入实例,请将它们转义为`$.
一个简单的例子:
# Define a template string, *single-quoted*, with *literal contents*:
# - '$InvID' is simply literally part of the string, not a variable reference (yet).
# - Ditto for $((Get-Date).TimeOfDay)
$strTempl = 'Invoice ID $InvID extracted at $((Get-Date).TimeOfDay).'
# Echo the template string as-is - unexpanded - ...
$strTempl
# ... and expand it on demand
$InvID = 1
$ExecutionContext.InvokeCommand.ExpandString($strTempl)
# ... and again, after assigning a different value to $InvID
$InvID = 2
$ExecutionContext.InvokeCommand.ExpandString($strTempl)
Run Code Online (Sandbox Code Playgroud)
上面的结果类似于:
Invoice ID $InvID extracted at $((Get-Date).TimeOfDay). # template literal
Invoice ID 1 extracted at 11:38:12.2719300. # first on-demand expansion
Invoice ID 2 extracted at 11:38:12.2766010. # second on-demand expnsion
Run Code Online (Sandbox Code Playgroud)
[1] 通过cmdlet以更容易发现的方式展示该$ExecutionContext.InvokeCommand.ExpandString() 方法的功能是此 GitHub 功能请求的主题。Expand-String
| 归档时间: |
|
| 查看次数: |
338 次 |
| 最近记录: |