PowerShell cambia el tipo de objeto de retorno

Estoy usando PowerShell v3 y el ISE de Windows PowerShell. Tengo la siguiente función que funciona bien:

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
}

Ahora, voy a crear algunas funciones similares, por lo que quiero dividir las primeras 3 líneas en una nueva función para que no tenga que copiarlas y pegarlas en todas partes, así que he hecho esto:

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
}

El problema es que cuando "return $ xmlNsManager" ejecuta el siguiente error se produce:

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

Entonces, aunque he convertido explícitamente mis variables $ xmlNsManager para que sean del tipo System.Xml.XmlNamespaceManager, cuando se devuelve de la función Get-XmlNamespaceManager, PowerShell lo está convirtiendo en una matriz de objetos.

Si no emito explícitamente el valor devuelto por la función Get-XmlNamespaceManager a System.Xml.XmlNamespaceManager, entonces se produce el siguiente error desde la función .SelectSingleNode () porque el tipo de datos incorrecto se pasa al segundo parámetro de la función.

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

Por alguna razón, PowerShell no mantiene el tipo de datos de la variable de retorno. Realmente me gustaría que esto funcione desde una función para no tener que copiar y pegar esas 3 líneas por todas partes. Cualquier sugerencia es apreciada. Gracias.

Respuestas a la pregunta(2)

Su respuesta a la pregunta