我没有太多使用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.xmlWhen 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.xmlI 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.xmlI wrote @{Name=...; Expression=...} for clarity but they can be shortened to @{N=...,E=...}.
更多推荐
发布评论