O PowerShell altera o tipo do objeto de retorno

Eu estou usando o PowerShell v3 e o Windows PowerShell ISE. Eu tenho a seguinte função que funciona bem:

function Get-XmlNode([xml]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
    # If a Namespace URI was not given, use the Xml document's default namespace.
    if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI }   

    # In order for SelectSingleNode() to actually work, we need to use the fully qualified node path along with an Xml Namespace Manager, so set them up.
    [System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($XmlDocument.NameTable)
    $xmlNsManager.AddNamespace("ns", $NamespaceURI)

    [string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter

    # Try and get the node, then return it. Returns $null if the node was not found.
    $node = $XmlDocument.SelectSingleNode($fullyQualifiedNodePath, $xmlNsManager)
    return $node
}

Agora, criarei algumas funções semelhantes, portanto, quero dividir as primeiras 3 linhas em uma nova função, para que não precise copiar e colar em todas as partes, portanto, fiz o seguinte:

function Get-XmlNamespaceManager([xml]$XmlDocument, [string]$NamespaceURI = "")
{
    # If a Namespace URI was not given, use the Xml document's default namespace.
    if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI }   

    # In order for SelectSingleNode() to actually work, we need to use the fully qualified node path along with an Xml Namespace Manager, so set them up.
    [System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($XmlDocument.NameTable)
    $xmlNsManager.AddNamespace("ns", $NamespaceURI)
    return $xmlNsManager
}

function Get-XmlNode([xml]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
    [System.Xml.XmlNamespaceManager]$xmlNsManager = Get-XmlNamespaceManager -XmlDocument $XmlDocument -NamespaceURI $NamespaceURI
    [string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter

    # Try and get the node, then return it. Returns $null if the node was not found.
    $node = $XmlDocument.SelectSingleNode($fullyQualifiedNodePath, $xmlNsManager)
    return $node
}

O problema é que quando "return $ xmlNsManager" é executado, o seguinte erro é lançado:

Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Xml.XmlNamespaceManager".

Portanto, embora eu tenha expressamente convertido minhas variáveis ​​$ xmlNsManager para serem do tipo System.Xml.XmlNamespaceManager, quando ele é retornado da função Get-XmlNamespaceManager, o PowerShell está convertendo-o em uma matriz de objetos.

Se eu não lançar explicitamente o valor retornado da função Get-XmlNamespaceManager para System.Xml.XmlNamespaceManager, o seguinte erro será lançado da função .SelectSingleNode () porque o tipo de dados incorreto está sendo passado para o segundo parâmetro da função.

Cannot find an overload for "SelectSingleNode" and the argument count: "2".

Portanto, por algum motivo, o PowerShell não mantém o tipo de dados da variável de retorno. Eu realmente gostaria de obter este trabalho de uma função para que eu não precise copiar e colar essas 3 linhas em todo o lugar. Qualquer sugestão é apreciada. Obrigado.

questionAnswers(2)

yourAnswerToTheQuestion