1

I'm Trying to use Powershell to make a choices to Enable/Disable Net Adapter "Ethernet" I Coded This

$caption = "Choose Action";
$message = "What do you want to do?";
$enab = start-process powershell -verb runas -argument D:\ena.ps1
$disa = start-process powershell -verb runas -argument D:\dis.ps1
$choices = [System.Management.Automation.Host.ChoiceDescription[]]($enab,$disa);
$answer = $host.ui.PromptForChoice($caption,$message,$choices,0)
switch ($answer){
0 {"You entered Enable"; break}
1 {"You entered Disable"; break}
}

Error :

Object cannot be stored in an array of this type. At D:\Untitled4.ps1:5 char:1 + $choices = System.Management.Automation.Host.ChoiceDescription[]; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], InvalidCastException + FullyQualifiedErrorId : System.InvalidCastException

Exception calling "PromptForChoice" with "4" argument(s): "Value cannot be null. Parameter name: choices" At D:\Untitled4.ps1:6 char:1 + $answer = $host.ui.PromptForChoice($caption,$message,$choices,0) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ArgumentNullException

I have failed before this to do On/Off script using powershell (if the net adapter is enable then disable it and vice versa. is there any way to do this?

1 Answer 1

2

Using this: https://technet.microsoft.com/en-us/library/ff730939.aspx I think what you want to be doing is the following:

$caption = "Choose action:"
$message = "What state do you want for the network adapter?"

$enable = New-Object System.Management.Automation.Host.ChoiceDescription "&Enable", `
    "Enables the Network Adapter"

$disable = New-Object System.Management.Automation.Host.ChoiceDescription "&Disable", `
    "Disables the Network Adapter"

$options = [System.Management.Automation.Host.ChoiceDescription[]]($enable, $disable)

$result = $host.ui.PromptForChoice($caption, $message, $options, 0) 

switch ($result)
{
    0 { 
        "You selected Enable." 
        start-process powershell -verb runas -argument D:\ena.ps1
    }
    1 {
        "You selected Disable."
        start-process powershell -verb runas -argument D:\dis.ps1
    }
}

The approach you were taking wasn't working because you were trying to assign a process to a ChoiceDescription array. In the example above, you have to first create two ChoiceDescription objects before assigning them to the array

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you it worked , and i noticed the mistake i made

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.