从PowerShell命令输出创建XML文件(Creating XML file from PowerShell command output)

编程入门 行业动态 更新时间:2024-10-26 02:36:00
从PowerShell命令输出创建XML文件(Creating XML file from PowerShell command output)

我没有太多使用XML,所以这对大多数人来说可能是一个基本问题......

我想从Get-ChildItem收集信息,并结合Get-Acl,并将其存储在XML文件中,以便稍后在PowerShell中使用它。

问题:为了让我使用Get-Acl,我必须在管道中使用foreach循环,然后将对象类型更改为system.string。 因此,当我将其导出到XML时,它几乎只是一个平面文件,而不是表结构。

我怎样才能恢复桌面结构?

如果我只是执行此命令,我可以得到表结构:

Get-ChildItem . -Recurse | Select FullName, PSIsContainer, CreationTimeUTC, LastAccessTimeUtc, Length | Export-Clixml .\STIGTest\Baseline_1.xml

当我执行此命令,这是我需要的,我得到平面文件:

Get-ChildItem . -Recurse | foreach-object {$AclOwner = (get-acl $_.pspath).owner; $_.Fullname, $_.PSIsContainer, $_.CreationTimeUtc, $_.LastAccessUtc, $_.Length, $AclOwner} | Export-Clixml .\STIGTest\Baseline_2.xml

我已经看到你可以将系统对象转换为字符串,但似乎无法找到我可以反向的位置。 我猜他们是否作为系统对象保留XML文件将保持表结构?

I have not worked with XML much so this may be a basic question to most...

I want to collect information from Get-ChildItem, combined with Get-Acl, and store it in an XML file so I can use it later within PowerShell.

The issue: in order for me to use the Get-Acl I have to use a foreach loop within the pipeline, which is in turn changing the object type to system.string. So when I export that to XML it is pretty much just a flat file, not table structure.

How can I get the table structure back?

If I just do this command I can get the table structure:

Get-ChildItem . -Recurse | Select FullName, PSIsContainer, CreationTimeUTC, LastAccessTimeUtc, Length | Export-Clixml .\STIGTest\Baseline_1.xml

When I do this command, which is what I need, I get the flat file:

Get-ChildItem . -Recurse | foreach-object {$AclOwner = (get-acl $_.pspath).owner; $_.Fullname, $_.PSIsContainer, $_.CreationTimeUtc, $_.LastAccessUtc, $_.Length, $AclOwner} | Export-Clixml .\STIGTest\Baseline_2.xml

I have seen where you can convert a system object to a string but can't seem to find where I can do the reverse. I am guessing if they stay as a system object the XML file will keep the table structure?

最满意答案

尝试一下这个。 它利用了Select-Object可以创建表达式指定的“计算”属性这一事实。

Get-ChildItem . -Recurse | Select FullName, PSIsContainer, CreationTimeUTC, LastAccessTimeUtc, Length, @{Name='Owner';Expression={ (Get-Acl $_.PSPath).Owner }} | Export-Clixml .\STIGTest\Baseline_1.xml

我写了@{Name=...; Expression=...} @{Name=...; Expression=...}为了清楚起见,但它们可以缩短为@{N=...,E=...} 。

Give this one a try. It takes advantage of the fact that Select-Object can create "calculated" properties specified by an expression.

Get-ChildItem . -Recurse | Select FullName, PSIsContainer, CreationTimeUTC, LastAccessTimeUtc, Length, @{Name='Owner';Expression={ (Get-Acl $_.PSPath).Owner }} | Export-Clixml .\STIGTest\Baseline_1.xml

I wrote @{Name=...; Expression=...} for clarity but they can be shortened to @{N=...,E=...}.

更多推荐

本文发布于:2023-08-01 19:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1364826.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   文件   XML   PowerShell   output

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!