Lorsque j'exécute le programme, cette fonction est activée. Je reçois une erreur cryptique qui n'a aucun sens pour moi. Je n'étais pas au courant que j'utilisais une méthode sur une expression à valeur nulle. Il me vient à l'esprit que c'est un problème de portée ou qu'une valeur n'est pas définie. J'ai cependant pas été en mesure de le comprendre et de le mettre à la communauté:PowerShell - If Then: impossible d'appeler une méthode sur une expression à valeur nulle
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
Je sais que c'est un ancien poste, mais, je remarque ici et ci-dessous, vous avez la ligne 'Si ($ result = $ PASS_RESULT) {'. La variable '$ result' n'est pas utilisée ailleurs - il n'est pas du tout évident de savoir à quoi elle sert. Mais le plus important, voulez-vous dire 'If ($ result -eq $ PASS_RESULT) {'? Et est-ce que c'était peut-être une faute de frappe "traduction" quand vous avez dépouillé votre code pour la présentation ici, et si oui, cela a-t-il pu avoir quelque chose à voir avec votre problème? Il suffit de demander, car 'if ($ result = $ PASS_RESULT) {' dans ce contexte est juste mauvais. –