将XML作为结构化数据导入Powershell



查看文档,Powershell中有用于导入和导出Jsoncsv数据的命令。对于Xml,似乎只有ConvertTo-Xml。我预计使用Xml会分配更多的数据,到目前为止,将Xml数据放入powershell会话会很困难。

我尝试了许多解决方案,都使用看起来像.NetC#的调用来导入Xml,到目前为止,所有解决方案都给了我错误,我不知道任何.NetC#。例如,这个答案建议做:

[xml]$xmlObject = (New-Object System.Net.WebClient).DownloadString("Filepath")   
Write-Host $xmlObject.root.User_Blob.Item.Key

我使用提问者自己的Xml:将解决方案联系起来

[xml]$xmlObject = (New-Object System.Net.WebClient).DownloadString("C:tempSample 2.xml")
Write-Host $xmlObject.root.User_Blob.Item.Key

我得到了这个错误:

MetadataError: Cannot convert value "<?xml version="1.0" encoding="utf-16"?>  
<root>  
<User_Blob>  
<Item>  
<Key>LogonMethod</Key>  
<Value>prompt</Value>  
</Item>  
<Item>  
<Key>ServerURLEntered</Key>  
<Value>http://myserver/config.xml</Value>  
</Item>  
<Item>  
<Key>ServerURLListUsers</Key>  
<Value>  
<LSOption>http://myurl/config.xml</LSOption>  
<LSOption>http://myurl</LSOption>  
</Value>  
</Item>  
<Item>  
<Key>UserDisplayDimensions</Key>  
<Value>fullscreen</Value>  
</Item>  
</User_Blob></root>'" to type "System.Xml.XmlDocument". Error: "The specified node cannot be inserted as the valid child of th  
is node, because the specified node is the wrong type."

我也尝试了同样的答案第二个解决方案:

$xmlObject = New-Object XML
$xmlObject.Load("C:tempSample 2.xml")

我无法到达第三行,因为我之前遇到了这个错误:

MethodInvocationException: Exception calling "Load" with "1" argument(s): "There is no Unicode byte order mark. Cannot switch to  
Unicode."

我在一些powershell博客上遵循了其他解决方案,甚至尝试了一些xml模块,但都没有成功。

为了清楚起见,我只是在寻找一种方法,将下面的示例文件导入为哈希表或PSObject:

<?xml version="1.0"?>
<results command="list0" result="1">
<items display_path="C:Temp" lister="0x20517e0" path="C:Temp" tab="0x4e2522">
<item id="20" name="White.png" path="C:TempWhite.png" sel="0" type="0" />
<item id="1" name="All Tasks.lnk" path="C:TempAll Tasks.lnk" sel="0" type="0" />
<item id="15" name="Recipes.lnk" path="C:TempRecipes.lnk" sel="1" type="0" />
<item id="16" name="Dinner plan.lnk" path="C:TempDinner plan.lnk" sel="1" type="0" />
<item id="14" name="Car.lnk" path="C:TempCar.lnk" sel="1" type="0" />
<item id="18" name="bike.lnk" path="C:Tempbike.lnk" sel="1" type="0" />
<item id="5" name="blue.png" path="C:Tempblue.png" sel="0" type="0" />
<item id="4" name="Black.png" path="C:TempBlack.png" sel="0" type="0" />
<item id="7" name="File 2.ahk" path="C:TempFile 2.ahk" sel="0" type="0" />
<item id="9" name="File 4.ahk" path="C:TempFile 4.ahk" sel="0" type="0" />
<item id="6" name="file 1.ahk" path="C:Tempfile 1.ahk" sel="0" type="0" />
<item id="8" name="File 3.ahk" path="C:TempFile 3.ahk" sel="0" type="0" />
</items>
</results>

PSV版本7.3.0-预览.7

Windows终端预览1.15.2003.0


我也可以在其他论坛上交叉发布这个问题。

如有任何帮助,我们将不胜感激!

当直接输入到PS控制台时,这对我有效。从文件加载时,您可能需要考虑意外编码、BOM、无关字符等。编码和文件格式本身就是一整章。我们所有人都在某个时刻编码疼痛:(

你可能已经知道了,但是"@在PS中是一个多行字符串文字。使用PS的默认字符串编码,所以它只起作用:(

$xmlString = @"
<?xml version="1.0"?>
<results command="list0" result="1">
<items display_path="C:Temp" lister="0x20517e0" path="C:Temp" tab="0x4e2522">
<item id="20" name="White.png" path="C:TempWhite.png" sel="0" type="0" />
<item id="1" name="All Tasks.lnk" path="C:TempAll Tasks.lnk" sel="0" type="0" />
<item id="15" name="Recipes.lnk" path="C:TempRecipes.lnk" sel="1" type="0" />
<item id="16" name="Dinner plan.lnk" path="C:TempDinner plan.lnk" sel="1" type="0" />
<item id="14" name="Car.lnk" path="C:TempCar.lnk" sel="1" type="0" />
<item id="18" name="bike.lnk" path="C:Tempbike.lnk" sel="1" type="0" />
<item id="5" name="blue.png" path="C:Tempblue.png" sel="0" type="0" />
<item id="4" name="Black.png" path="C:TempBlack.png" sel="0" type="0" />
<item id="7" name="File 2.ahk" path="C:TempFile 2.ahk" sel="0" type="0" />
<item id="9" name="File 4.ahk" path="C:TempFile 4.ahk" sel="0" type="0" />
<item id="6" name="file 1.ahk" path="C:Tempfile 1.ahk" sel="0" type="0" />
<item id="8" name="File 3.ahk" path="C:TempFile 3.ahk" sel="0" type="0" />
</items>
</results>
"@
$test = [xml]$xmlString
$test.results.items.item | where id -eq 16
# bit of xpath
$test.CreateNavigator().Select("//item[@id=16]").UnderlyingObject

最新更新