powershell - Powershell通过函数调用传递XMLDocument

标签 powershell

我正在尝试通过函数调用传递System.XML.XMLDocument对象,但是该对象在函数调用后更改为System.Object[]

更具体地说,我有一个这样的功能:

function GenerateXml()
{

    [System.XML.XMLDocument]$xmlDoc=New-Object System.XML.XMLDocument
    $declaration = $xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes")
    $xmlDoc.AppendChild($declaration)

    [System.XML.XMLElement]$root = $xmlDoc.CreateElement("dummy")
    $root.SetAttribute("Name", "dummy value")
    $xmlDoc.AppendChild($root)
    $xmlDoc
}

[System.XML.XMLDocument]$xmlDoc = GenerateXml

这将输出如下错误:
Cannot convert value "System.Object[]" to type "System.Xml.XmlDocument".

那么通过函数调用传递XMLDocument的最佳方法是什么?

最佳答案

事实证明,Powershell会将方法的所有输出返回到数组。要获取实际的返回值,您可以获取输出数组的最后一个成员,或者只是不输出您实际想要的东西。

关于powershell - Powershell通过函数调用传递XMLDocument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34885629/

相关文章:

powershell - 如何使用Powershell向Windows服务器上的域帐户授予SeBatchLogon特权?

azure - 如何将特定的azure资源导出到json文件(arm模板)

xml - Powershell根据子节点值选择父xml节点并添加子元素

powershell - PowerShell 中的模糊字符串匹配

string - 在包含花括号的字符串上使用 -f 运算符

.net - SHA256 的 key 算法

c# - 为什么 PowerShell 类不加载管理单元

arrays - 在本地子网中查找可用设备名称并重命名设备

git - 如何使用 PowerShell 在 Azure DevOps 管道中运行 git Push?

.net - 使用 powershell 解析逗号分隔的文件