PowerShell-如果则:无法在空值表达式上调用方法 [英] PowerShell - If Then: cannot call a method on a null-valued expression

查看:706
本文介绍了PowerShell-如果则:无法在空值表达式上调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行程序时,此功能处于输入状态,我收到一个对我来说没有意义的神秘错误.我不知道自己在空值表达式上运行方法.在我看来这是范围问题或未设置值.但是,我无法弄清楚并把它发布给社区:

When I run the program this function is in I get a cryptic error that doesn't make sense to me. I wasn't aware I was running a method on a null-valued expression. It occures to me that this is either a scope issue or a value is not getting set. I however have not been able to figure it out and put it out to the community:

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

推荐答案

似乎从最初的两个if-then语句更改为在我的初始if-then语句之后插入中断,并在第二个Do-Loop上添加了相同的语句解决了我遇到的问题.我这样做的原因是,在我的菜单下降了一层然后返回到顶层后,我注意到选择重置适配器时不存在该错误.

It appears that changing from two if-then statements at first to inserting breaks after my initial if-then statement and adding an identical one on the second Do-Loop fixed the problem I was having. The reason I did this was that after going down one layer of my menu and then returning to the top layer I notice the error was absent when choosing reset-adapter.

侧栏: 我没有解释说,我在这里列出的功能是一个菜单,它会反复遍历 $ DCBmenuItems,尽管从代码中可以明显看出这一点.

Side Bar: I didn't explain that the function I listed here was a menu that iterates through the $DCBmenuItems, although this may have been obvious from the code.

我认为$resetAdapter由于某种原因未正确设置.对于这为什么能解决我的问题以及为什么其他代码引发错误,我不是100%的确定.这是修改后的代码:

I think $resetAdapter wasn't getting set correctly for some reason. I'm not 100% sure as to why this solved my problem and why the other code threw errors. Here is the amended code:

$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-如果则:无法在空值表达式上调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆