PowerShell的 - 如果然后:不能调用一个空值表达式的方法(PowerShell - If

2019-11-01 02:01发布

当我运行程序这个功能是我得到一个神秘的错误是没有意义的我。 我不知道我当时正在上一个空值表达式的方法。 这occures,我认为这可以是一个范围问题或值是没有得到设置。 然而,我一直无法推测出来,并把它带到社区:

You cannot call a method on a null-valued expression.
At C:\Users\Administrator\Desktop\DCB Settings Modification\DCBxPowershell.ps1:747 char:21
+                 If ($resetAdapter -eq $FAIL_RESULT){
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\Administrator\Desktop\DCB Settings Modification\DCBxPowershell.ps1:760 char:17
+             If ($resetAdapter -eq $FAIL_RESULT){
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull**

$FAIL_RESULT = 0
$PASS_RESULT = 1

Function Use-Menu
{ 
    param($DCBmenuItems, $modificationCatagoryChoosen)

    ## Function Menu-Choose --## Holds choosen Network Interface Index to work with            
    $networkIndex = Menu-Choose $strippedNetworkIndex $networkChooseTitle
    #$networkIndex[0]  ### Debug -
    $resetAdapter = $FAIL_RESULT

    Start-Sleep -s .7

    If ($result = $PASS_RESULT) {
    ## Find Current Config in order to display it to user
        $dcbConfig = Find-Config $networkIndex
    }

    #The following 'DO WHILEs' are for the "Go back to previous Menu" functionality.
    Do {
        Do {

            ## Function Menu-Choose --## Let user choose which catagory of modification to perform
            $modificationCatagoryChoosen = Menu-Choose $DCBmenuItems $DCBMenuTitle $networkIndex -scope global
            If (($DCBmenuItems.count - 1) -eq $modificationCatagoryChoosen) {
                $resetAdapter = $PASS_RESULT
            }

            # These If Then statements allow reset of adapter without changing settings
            If ($resetAdapter -eq $FAIL_RESULT){
                $DCBmenuItems2 = $xmlDCBoptions.MenuItems.MenuOptions[$modificationCatagoryChoosen].Option
                $DCBMenuTitle2 = $xmlDCBoptions.MenuItems.MenuOptions[$modificationCatagoryChoosen].Name

                Start-Sleep -s .7

                ## Function Menu-Choose --## Let user choose which modification to perform
                $modificationChoosen = Menu-Choose $DCBmenuItems2 $DCBMenuTitle2 $networkIndex
            }

        } While (($modificationChoosen -eq $DCBmenuItems2.GetUpperBound(0)) -and ($resetAdapter -eq $FAIL_RESULT))

        # These If Then statements allow reset of adapter without changing settings
        If ($resetAdapter -eq $FAIL_RESULT){ 
            ## Changes the options to choose on Menu-Choose to last chosen catagory
            $DCBmenuItems3 = $xmlDCBoptions.MenuItems.MenuOptions[$modificationCatagoryChoosen].SubMenu[$modificationChoosen].Option
            $DCBMenuTitle3 = $xmlDCBoptions.MenuItems.MenuOptions[$modificationCatagoryChoosen].SubMenu[$modificationChoosen].Name

            Start-Sleep -s .7

            ## Function Menu-Choose --## Let user choose how to modify DCB setting
            $modificationOptionChoosen = Menu-Choose $DCBmenuItems3 $DCBMenuTitle3 $networkIndex
        }
    } While (($modificationOptionChoosen -eq $DCBmenuItems3.GetUpperBound(0)) -and ($resetAdapter -eq $FAIL_RESULT)) 

    # These If Then statements allow reset of adapter without changing settings
    If ($resetAdapter -eq $FAIL_RESULT){
        Start-Sleep -s .7

         ## Function Set-RegistryValues --## Records modified DCB setting to registry
        Set-RegistryValues $xmlDCBregEdits $modificationCatagoryChoosen $modificationChoosen $modificationOptionChoosen
    }

Return $networkIndex

Answer 1:

这个错误的位置看起来不正确。 我没有看到PowerShell是如何让这种类型的错误的if ()条件。 但是这条线可能会造成错误:

$DCBmenuItems3.GetUpperBound(0)

添加一个检查,以确保$DCBmenuItems3不为空。 此外,调试问题的时候(或赫克只是作为一个正常的做法),我把Set-StrictMode -version latest在我的脚本的顶部。



Answer 2:

FAIL_RESULT = 0
PASS_RESULT = 1

错字?

$FAIL_RESULT = 0
$PASS_RESULT = 1


Answer 3:

看来,从两个改变的if-then语句,在第一次插入休息我最初的if-then语句后,加入第二DO循环相同的一个固定的,我有这个问题。 我这样做的原因是,去了我的菜单中的一层,然后再返回到顶层后,我发现选择复位适配器时的错误是不存在的。

边栏:我没有解释,我在这里列出的功能是通过$ DCBmenuItems迭代,虽然这可能是显而易见的代码的菜单。

我想$resetAdapter没有得到正确设置的某些原因。 我不知道为什么这解决了我的问题,为什么其他代码扔错误100%。 下面是修改后的代码:

$FAIL_RESULT = 0
$PASS_RESULT = 1

Function Use-Menu
{ 
    param($DCBmenuItems, $modificationCatagoryChoosen)

    ## Function Menu-Choose --## Holds choosen Network Interface Index to work with            
    $networkIndex = Menu-Choose $strippedNetworkIndex $networkChooseTitle
    #$networkIndex[0]  ### Debug -

    $resetAdapter = $FAIL_RESULT
    $modificationCatagoryChoosen = $null
    $modificationChoosen = $null
    $modificationOptionChoosen = $null
    $DCBmenuItems2 = $null
    $DCBMenuTitle2 = $null
    $DCBmenuItems3 = $null
    $DCBMenuTitle3 = $null

    Start-Sleep -s .7

    If ($result = $PASS_RESULT) {
    ## Find Current Config in order to display it to user
        $dcbConfig = Find-Config $networkIndex
    }

    #The following 'DO WHILEs' are for the "Go back to previous Menu" functionality.
    Do {
        Do {

            ## Function Menu-Choose --## Let user choose which catagory of modification to perform
            $modificationCatagoryChoosen = Menu-Choose $DCBmenuItems $DCBMenuTitle $networkIndex -scope global
            If (($DCBmenuItems.count - 1) -eq $modificationCatagoryChoosen) {
                $resetAdapter = $PASS_RESULT
                break
            }

            # These If Then statements allow reset of adapter without changing settings

            $DCBmenuItems2 = $xmlDCBoptions.MenuItems.MenuOptions[$modificationCatagoryChoosen].Option
            $DCBMenuTitle2 = $xmlDCBoptions.MenuItems.MenuOptions[$modificationCatagoryChoosen].Name

            Start-Sleep -s .7

            ## Function Menu-Choose --## Let user choose which modification to perform
            $modificationChoosen = Menu-Choose $DCBmenuItems2 $DCBMenuTitle2 $networkIndex


        } While (($modificationChoosen -eq $DCBmenuItems2.GetUpperBound(0)) -and ($resetAdapter -eq $FAIL_RESULT))

        If (($DCBmenuItems.count - 1) -eq $modificationCatagoryChoosen) {
                break
        }
        # These If Then statements allow reset of adapter without changing settings

        ## Changes the options to choose on Menu-Choose to last chosen catagory
        $DCBmenuItems3 = $xmlDCBoptions.MenuItems.MenuOptions[$modificationCatagoryChoosen].SubMenu[$modificationChoosen].Option
        $DCBMenuTitle3 = $xmlDCBoptions.MenuItems.MenuOptions[$modificationCatagoryChoosen].SubMenu[$modificationChoosen].Name

        Start-Sleep -s .7

        ## Function Menu-Choose --## Let user choose how to modify DCB setting
        $modificationOptionChoosen = Menu-Choose $DCBmenuItems3 $DCBMenuTitle3 $networkIndex

    } While (($modificationOptionChoosen -eq $DCBmenuItems3.GetUpperBound(0)) -and ($resetAdapter -eq $FAIL_RESULT)) 

    # These If Then statements allow reset of adapter without changing settings
    If ($resetAdapter -eq $FAIL_RESULT){
        Start-Sleep -s .7

         ## Function Set-RegistryValues --## Records modified DCB setting to registry
        Set-RegistryValues $xmlDCBregEdits $modificationCatagoryChoosen $modificationChoosen $modificationOptionChoosen
    }

Return $networkIndex

}



文章来源: PowerShell - If Then: cannot call a method on a null-valued expression