- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试构建一个脚本来将 XML 文件转换为 CSV。我开始遵循本指南 https://learn.microsoft.com/en-us/answers/questions/542481/parse-xml-to-csv-help.html但我现在遇到以下情况:
我的代码的第一部分工作正常,它在第一列写入 CSV 文件,但是当它通过以下迭代时,即使将值输出到控制台,它也不会将值写入文件。这是我的代码:
[xml]$x = Get-Content C:\Users\Desktop\Policy.xml
$x.Profiles |
ForEach-Object{
$_.Profile |
ForEach-Object{
$_.Ruleset |
ForEach-Object{
Write-Host "Policy ID = " $_.ID
[PSCustomObject]@{
policyName = $_.ID
}
$_.Conditions |
ForEach-Object{
$_.condition |
ForEach-Object{
if($_.name -eq "Resource") {
Write-Host "Resource = " $_.'#text'
[PSCustomObject]@{
resource = $_.'#text'
}
}
if($_.name -eq "Action") {
Write-Host "Action = " $_.'#text'
[PSCustomObject]@{
action = $_.'#text'
}
}
if($_.name -eq "actor") {
Write-Host "actor = " $_.'#text'
[PSCustomObject]@{
actor = $_.'#text'
}
}
if($_.name -eq "child") {
Write-Host "child = " $_.'#text'
[PSCustomObject]@{
child = $_.'#text'
}
}
if($_.name -eq "number") {
Write-Host "number = " $_.'#text'
[PSCustomObject]@{
number = $_.'#text'
}
}
}
}
}
}
} | Export-Csv C:\Users\Desktop\policy.csv -NoTypeInformation
因此,直到第一个 [PSCustomObject]
(第 10 行)它工作正常并且 policyName 列被写入 CSV 值及其相应的值。但是在第二个 [PSCustomObject]
(第 19 行)应该写入 Resource/Action/actor/child/number 的地方,它不再写入文件。
将这些值添加到现有的 [PSCustomObject]
中的正确方法是什么?
供引用:
XML 片段
<?xml version="1.0" encoding="UTF-8"?>
<Profiles xmlns:pcrf="nothing interesting here">
<Profile Desc="some description" ID="someid" Prio="0">
<Ruleset Class="class1" Desc="" ID="policyid1" Prio="10" active="true">
<Conditions>
<condition name="Resource" type="matchExact">resource1</condition>
<condition name="Action" type="matchExact">action1</condition>
<condition name="actor" type="matchExact">actor1</condition>
</Conditions>
</Ruleset>
<Ruleset Class="classX" Desc="" ID="policyidX" Prio="10" active="true">
<Conditions>
<condition name="Resource" type="matchExact">resource4</condition>
<condition name="Action" type="matchExact">action4</condition>
<condition name="child" type="matchExact">child1,child2</condition>
</Conditions>
</Ruleset>
</Profile>
<Profile Desc="some description" ID="someid" Prio="0">
<Ruleset Class="classY" Desc="" ID="policyidY" Prio="10" active="true">
<Conditions>
<condition name="Resource" type="matchExact">resource99</condition>
<condition name="Action" type="matchExact">action00</condition>
<condition name="child" type="matchExact">child5</condition>
<condition name="number" type="matchExact">number1</condition>
</Conditions>
</Ruleset>
</Profile>
</Profiles>
结果我得到了这个 CSV:
"policyName"
"policyid1"
这是 powershell 输出:
PS C:\Users\Desktop> .\xmltocsv.ps1
Policy ID = policyid1
Resource = resource1
Action = action1
actor = actor1
Resource = resource4
Action = action4
child = child1,child2
Resource = resource99
Action = action00
child = child5
number = number1
这是我希望得到的 CSV 文件:
"policyName","Resource","Action","actor","child","number"
"policyid1","resource1","action1","actor1","",""
"policyidX","resource4","action4","","child1,child2",""
"policyidY","resource99","action0","","child5","number1"
最佳答案
代码的主要问题是您需要根据 <condition>
创建一个输出对象元素,具有从父元素及其子元素收集的属性值,而您正在为每个输出对象的属性创建一个单独的对象([pscustomobject]
);这是一个大大简化的解决方案:
$x.Profiles.Profile.Ruleset.Conditions |
ForEach-Object {
$outputObj = [pscustomobject] @{
# Get the policy ID from the parent node.
policyId = $_.ParentNode.ID
# The following properties are filled in below.
Resource = ''
Action = ''
actor = ''
child = ''
number = ''
}
# Loop over all child elements and fill in the property
# that corresponds to the "name" attribute with the element's inner text.
# Note that the "name" *attribute* actually shadows
# the element's type-native .Name *property*.
foreach ($el in $_.ChildNodes) {
$outputObj.($el.name) = $el.InnerText
}
# Output the resulting object
$outputObj
} |
ConvertTo-Csv # To export to a *file, use Export-Csv
这会产生问题中所示的所需输出。
注意:
这创建了一个 [pscustomobject]
每<Conditions>
元素,具有从父元素收集的属性值以及一组固定的属性,如果存在于基于匹配的子元素中,则填充这些属性 name
属性值,使用匹配的子元素的内部文本作为属性值。
请注意属性访问 .Profiles.Profile.Ruleset.Conditions
隐式 遍历多个 <Profile>
和 <Ruleset>
元素并将它们全部返回,这要归功于名为 member-access enumeration 的功能.
关于xml - 如何向 PSCustomObject 添加值(XML 到 CSV 的转换),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74394699/
我在 Windows 7 下使用 Powershell 4,并且我的 cmdlet 定义如下: Function Transfer-File { [CmdletBinding()]
这个问题在这里已经有了答案: PowerShell output is crossing between functions (1 个回答) 关闭1 年前。 早上好 我非常喜欢从列出的项目返回的值中
我有一个非常大的员工 JSON 响应,我试图将其转换为表格格式,导出为 CSV 并最终插入到 SQL Server 中。我能够确定如何从 json 文件中获取我的所有变量,但是现在我将我的所有值都插入
我尝试使用这样的自定义方法定义对象,但是我的语法错误: $Obj = [pscustomobject]@{ A = @(5,6,7) B = 9 Len_A = {return
我正在准备一个表,其中包含有关虚拟机名称和操作系统磁盘的配置日期的信息。如果将单个虚拟机分配给 $VM,我可以轻松地从 $VM.disks.statuses.time[0] 命令检索该信息,但是当我尝
将 PSCustomObject 转换为 Hashtable 的最简单方法是什么?它的显示方式就像带有 splat 运算符、花括号和看似键值对的内容一样。当我尝试将其转换为 [Hashtable] 时
我遇到的问题源于传递了 PSCustomObject作为 Start-Process 的参数cmdlet(我实际上是在启动一个新的 PowerShell 进程以从调用脚本异步运行脚本)。尽管参数定义为
早上好 我的桌面上有 3 个文件夹,每个文件夹包含随机数量的文件夹。 文件夹 1 子文件夹:“here too”、“more stuff”、“Ranom stuff”、“Something Here”
我试图满足我对返回类型性能的好奇心。基本上我想看看在返回复杂数据时哈希表、psCustomObject 和我自己的类之间是否存在有意义的差异。但我在使用 psCustomObject 时遇到了问题。
背景:我有一些我想更容易访问的位置。我不想 mklink,因为那样的话可能会将路径保存为链接路径并在以后失败。 建议的解决方案:创建一些 PSDrive。从一个静态的哈希表数组开始,以便将来可以轻松地
我正在寻找一种智能方法将以下字符串转换为 PSCustomObjects 数组: Name : AdtAgent DisplayName : Microsoft Monitoring A
在 PowerShell v3.0 中引入了 PSCustomObject。它类似于 PSObject,但更好。除了其他改进(例如保留属性顺序)之外,还简化了从哈希表创建对象的过程: [PSCusto
我无法从哈希表 JSON 对象中提取键值 $personnelresult = Invoke-RestMethod -Method Get www.url.com -WebSession $curre
下面是powershell中存储的 #Maintainer Note: The leftmost parameter must match the registry key name exactly
我试图找到一个属性大于另一行属性的行。示例: $Array Name Value ---- ---- test1 105 test2 1
您好,我有以下脚本可以让我获取证书详细信息并将它们存储在 CSV 文件中。我想排除一些证书信息出现在 CSV 中文件是距离到期还有超过 3650 天的证书 这是我的脚本 $StartDate = Ge
我需要 PowerShell 的帮助,以将两个输出或两个 PSCustomObjects 合并为一个。例如, $services = Get-Service | Select Name, Startt
这个问题在这里已经有了答案: Powershell: reference an object property in another object property from within the
我有这个PSCustomObject我正在尝试将其格式化为表格并将其导出到 .csv文件,当 .csv文件已构建,单元格中的输出为 System.Object[] . $tableFindings =
我觉得我无法弄清楚这一点很愚蠢,但是说我有一个包含 pscustomobjects 的数组。在一个令人难以置信的高层次上,请看下面的例子: $arr = @() $obj1 = [pscustomob
我是一名优秀的程序员,十分优秀!