为什么此Powershell函数有时返回列表,而有时返回XmlElement?

克里斯·贾斯特

出于某种原因,如果我的XML文件仅包含一个计算机节点,则以下Read-TestControllerXml powershell函数将返回System.Xml.XmlElement类型的对象,但是如果有多台计算机,它将返回一个列表(这是我一直希望的)节点。

XML文件的示例如下:

<test_controller>
    <defaults>
        <nunit path='C:\Program Files (x86)\NUnit 2.6.2\bin\nunit-console.exe' />
        <nunit_results local_path='C:\Test_Results' remote_path='C:\Test_Results' />
        <powershell path='C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe' />
        <psexec path='C:\PSTools\PsExec.exe' />
    </defaults>

    <platform os='windows' version='2012' cpu='x64'>
      <computer hostname='VM-2012R2-03' alias='standalone-01' configuration='standalone' image='VM-2012R2-03:Clean' />
    </platform>
</test_controller>

这是powershell函数:

####################################################################
# Parses the TestController.xml file and returns a list of Computer XML objects.
#
# Syntax:
#     Read-TestControllerXml [-TestControllerXml] <path to TestController.xml> [<CommonParameters>]
#
# Returns:  An array of [Xml] nodes for each of the <computer> tags in TestController.xml.
####################################################################
function Read-TestControllerXml
{
    [CmdletBinding()]
    Param
    (
        [parameter(Mandatory=$true)]
        [string] $TestControllerXml
    )

    $ErrorActionPreference = "Stop"

    # Read the TestController XML configuration file
    $TestController = [xml] (Get-Content $TestControllerXml)

    Write-Verbose "Selecting computer nodes..."
    $computerNodes = $TestController.SelectNodes("//test_controller/platform/computer")
    Write-Verbose "Selecting default nodes..."
    $defaultNodes = $TestController.SelectSingleNode("//test_controller/defaults")

    $computers = @()

    foreach ($computerNode in $computerNodes)
    {
        Write-Verbose "***** Before adding default attributes *****"
        Write-Verbose "$($computerNode.OuterXml)"

        # Add default attributes to the node
        foreach ($dnode in $defaultNodes.ChildNodes)
        {
            if ($dnode.NodeType -eq "Comment") { continue } # Skip comments.

            # Search for the node items in the defaults section
            $cnode = $computerNode

            if ($dnode.Name -ne $computerNode.Name)
            {
                Write-Verbose "Selecting '$($dnode.Name)' nodes..."
                $cnode = $computerNode.SelectSingleNode($dnode.Name)
            }

            # Append the default nodes that do not exist in the computer specific sections  
            if ($cnode -eq $null)
            {
                Write-Verbose "*** cnode != null  cnode.OuterXml is: '$($cnode.OuterXml)'"
                Write-Verbose "Appending default '$($dnode.Name)' node that don't exist in the computer nodes..."
                $cnode=$computerNode.AppendChild($TestController.ImportNode($dnode, $true))
            }
            # Append the default attributes that do not exist in the computer specific sections  
            else
            {
                Write-Verbose "Appending default attributes that don't exist in computer nodes..."
                foreach ($attribute in $dnode.Attributes)
                {
                    if (-not $cnode.HasAttribute($attribute.Name))
                    {
                        Write-Verbose "Adding attribute '$($attribute.Name)=$($attribute.Value)' to the computer node."
                         $cnode.SetAttribute($attribute.Name, $attribute.Value)
                    }
                }
            }
        }

        Write-Verbose "***** After adding default attributes *****"
        Write-Verbose "$($computerNode.OuterXml)"
        Write-Verbose "************************************************************"

        $computers += $computerNode
    }

    return $computers
}
克里斯·贾斯特

我刚刚发现了这个类似的问题,并且从那里可以找到解决方案:函数未返回预期的对象

即改变:

return $computers

至:

return ,$computers

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Linux

为什么max()有时返回nan而有时忽略它?

来自分类Dev

为什么Ruby Procs有时会返回而有时却是错误?

来自分类Dev

为什么函数有时返回0,有时返回false?

来自分类Java

为什么在Guava过滤器/变换函数中有时返回可修改的视图而有些返回不可修改的视图?

来自分类Dev

为什么此URLClassLoader有时起作用而有时却不起作用?

来自分类Dev

为什么datetime对象有时返回日期,有时返回日期和时间?

来自分类Dev

在node.js / Express中,为什么在next()之后有时需要返回语句,而有时却不需要?

来自分类Dev

为什么有时“this”在 Vue 模板中有效而有时无效?

来自分类Dev

为什么`:type`有时显示`a`,而有时显示`t`?

来自分类Dev

为什么有时在setState之后立即调用render,而有时不调用

来自分类Dev

为什么有时显示背景而有时不显示背景?

来自分类Dev

为什么open(“ url”)有时返回File有时是StringIO?

来自分类Dev

为什么instanceof有时不编译,有时返回false?

来自分类Dev

Laravel-为什么json响应有时返回数组有时是对象

来自分类Dev

为什么recyclerView的findviewbyposition()有时仅返回null

来自分类Dev

为什么AS有时会接受大位移而有时却不接受?

来自分类Dev

给定的五个函数返回相同的输出有什么区别,特别是为什么有时有时它们不返回?

来自分类Dev

为什么 PersistentObjectSpace 有时返回一个代理,有时返回一个对象?

来自分类Dev

为什么Ruby Array切片有时返回nil,有时返回空Array?

来自分类Dev

为什么有时在paren中有返回值,但有时在Go函数签名中却没有?

来自分类Dev

为什么有时您必须创建一个新列表,而有时却不在Unity中?

来自分类Dev

为什么我有时需要使用JSON.stringify而有时不需要

来自分类Dev

为什么有时IEnumerable <T>中的元素是可变的,而有时又是不可变的?

来自分类Dev

为什么“ .kt”扩展名有时可见而有时却不可见?

来自分类Dev

为什么“尝试/捕获”中的命令有时需要-ErrorAction停止,而有时则不需要?

来自分类Dev

为什么我有时会出错而有时却没有呢?

来自分类Dev

为什么同一条SQL有时会报告错误,而有时却运行良好?

来自分类Dev

为什么time.sleep有时会等待,而有时却“跳到最后”

来自分类Dev

为什么有时未定义此变量?

Related 相关文章

  1. 1

    为什么max()有时返回nan而有时忽略它?

  2. 2

    为什么Ruby Procs有时会返回而有时却是错误?

  3. 3

    为什么函数有时返回0,有时返回false?

  4. 4

    为什么在Guava过滤器/变换函数中有时返回可修改的视图而有些返回不可修改的视图?

  5. 5

    为什么此URLClassLoader有时起作用而有时却不起作用?

  6. 6

    为什么datetime对象有时返回日期,有时返回日期和时间?

  7. 7

    在node.js / Express中,为什么在next()之后有时需要返回语句,而有时却不需要?

  8. 8

    为什么有时“this”在 Vue 模板中有效而有时无效?

  9. 9

    为什么`:type`有时显示`a`,而有时显示`t`?

  10. 10

    为什么有时在setState之后立即调用render,而有时不调用

  11. 11

    为什么有时显示背景而有时不显示背景?

  12. 12

    为什么open(“ url”)有时返回File有时是StringIO?

  13. 13

    为什么instanceof有时不编译,有时返回false?

  14. 14

    Laravel-为什么json响应有时返回数组有时是对象

  15. 15

    为什么recyclerView的findviewbyposition()有时仅返回null

  16. 16

    为什么AS有时会接受大位移而有时却不接受?

  17. 17

    给定的五个函数返回相同的输出有什么区别,特别是为什么有时有时它们不返回?

  18. 18

    为什么 PersistentObjectSpace 有时返回一个代理,有时返回一个对象?

  19. 19

    为什么Ruby Array切片有时返回nil,有时返回空Array?

  20. 20

    为什么有时在paren中有返回值,但有时在Go函数签名中却没有?

  21. 21

    为什么有时您必须创建一个新列表,而有时却不在Unity中?

  22. 22

    为什么我有时需要使用JSON.stringify而有时不需要

  23. 23

    为什么有时IEnumerable <T>中的元素是可变的,而有时又是不可变的?

  24. 24

    为什么“ .kt”扩展名有时可见而有时却不可见?

  25. 25

    为什么“尝试/捕获”中的命令有时需要-ErrorAction停止,而有时则不需要?

  26. 26

    为什么我有时会出错而有时却没有呢?

  27. 27

    为什么同一条SQL有时会报告错误,而有时却运行良好?

  28. 28

    为什么time.sleep有时会等待,而有时却“跳到最后”

  29. 29

    为什么有时未定义此变量?

热门标签

归档