从 PowerShell 命令输出创建 XML 文件

发布于 2024-11-28 07:14:22 字数 805 浏览 0 评论 0原文

我没有太多使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

永言不败 2024-12-05 07:14:22

尝试一下这个。它利用了 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=...;为了清楚起见,表达式=...},但它们可以缩短为@{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=...}.

記憶穿過時間隧道 2024-12-05 07:14:22

@Josh Einstein 的答案以及我在评论中给出的链接,另一个答案是要走的路。但是还有另一种方法可以将 acl 信息添加到对象本身,下面总结了您在问题中提到的尝试:

Get-ChildItem . -recurse | 
foreach-object {$AclOwner = (get-acl $_.pspath).owner; Add-Member -input $_ -MemberType noteproperty -Name AclOwner -Value $AclOwner; $_ } | 
Select FullName, PSIsContainer, CreationTimeUTC, LastAccessTimeUtc, Length, AclOwner |
Export-Clixml .\STIGTest\Baseline_1.xml

@Josh Einstein's answer and from the link I had given in the comment, the other answer is the way to go. But there is another way by which you can add the acl information to the object itself, and the below adds up on what you were trying as mentioned in the question:

Get-ChildItem . -recurse | 
foreach-object {$AclOwner = (get-acl $_.pspath).owner; Add-Member -input $_ -MemberType noteproperty -Name AclOwner -Value $AclOwner; $_ } | 
Select FullName, PSIsContainer, CreationTimeUTC, LastAccessTimeUtc, Length, AclOwner |
Export-Clixml .\STIGTest\Baseline_1.xml
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文