从 PowerShell 命令输出创建 XML 文件
我没有太多使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试一下这个。它利用了
Select-Object
可以创建由表达式指定的“计算”属性这一事实。我写了
@{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.I wrote
@{Name=...; Expression=...}
for clarity but they can be shortened to@{N=...,E=...}
.@Josh Einstein 的答案以及我在评论中给出的链接,另一个答案是要走的路。但是还有另一种方法可以将 acl 信息添加到对象本身,下面总结了您在问题中提到的尝试:
@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: