xml - PowerShell 将 Get-Content 存储到变量中不返回任何数据

标签 xml powershell

我有一个非常简单的脚本,可以从 XML 文件中获取内容并返回它,但它不起作用:

$DataFile = Get-ChildItem \\$Server\C$\ *.data -Name
[xml]$DataFileContent = Get-Content Microsoft.PowerShell.Core\FileSystem::\\$Server\C$\$DataFile
Write-Host $DataFileContent

此脚本未在 Write-Host 上返回任何数据,但以下脚本返回正确的数据:

$DataFile = Get-ChildItem \\$Server\C$\ *.data -Name
Get-Content Microsoft.PowerShell.Core\FileSystem::\\$Server\C$\$DataFile

是的,该文件是一个xml文件并且该文件存在。我确定我遗漏了什么,但不确定是什么。任何帮助将不胜感激

最佳答案

您不能在此处使用Write-Host,因为您正试图显示一个[xml] 对象。请尝试使用 write-output

如果您需要Write-Host,您需要事先将对象表示为字符串或使用输出字符串的属性。例如:

[xml]$x = Get-Content y.xml
Write-Host $x.InnerXml

这也可能是与写入输出一起使用的好主意。

关于xml - PowerShell 将 Get-Content 存储到变量中不返回任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50514250/

相关文章:

android - SQLITE 支持 XML 查询吗?

python - 如何从 Element 获取原始 xml 作为字符串

sql - 如何找到没有特定子节点的节点?

powershell - Powershell-使用选择对象表达式的值

Powershell - 使用 AzureAD 模块获取用户的 LastPasswordChangeTimestamp

regex - 如何获取满足一定条件的$Matches索引的索引?

android - 在 Android 上使用 XML

math - PowerShell 数学问题?

performance - 填充非常大的哈希表-如何最有效地做到这一点?

xml - Jquery Ajax问题