使用 Powershell 选择启用/禁用网络适配器
Make Choices to Enable / Disable Net Adapter Using Powershell
我正在尝试使用 Powershell 来选择 Enable/Disable 网络适配器 "Ethernet"
我编码了这个
$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}
}
错误:
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
在此之前,我未能使用 powershell 执行 On/Off 脚本(如果启用了网络适配器,则禁用它,反之亦然。有什么办法可以做到这一点吗?
使用这个:https://technet.microsoft.com/en-us/library/ff730939.aspx我想你想要做的是:
$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
}
}
您采用的方法无效,因为您试图将 process
分配给 ChoiceDescription
数组。在上面的示例中,您必须先创建两个 ChoiceDescription
对象,然后再将它们分配给数组
我正在尝试使用 Powershell 来选择 Enable/Disable 网络适配器 "Ethernet" 我编码了这个
$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}
}
错误:
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
在此之前,我未能使用 powershell 执行 On/Off 脚本(如果启用了网络适配器,则禁用它,反之亦然。有什么办法可以做到这一点吗?
使用这个:https://technet.microsoft.com/en-us/library/ff730939.aspx我想你想要做的是:
$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
}
}
您采用的方法无效,因为您试图将 process
分配给 ChoiceDescription
数组。在上面的示例中,您必须先创建两个 ChoiceDescription
对象,然后再将它们分配给数组