选择使用Powershell启用/禁用网络适配器
我正在尝试使用Powershell来选择启用/禁用网络适配器“以太网” 我把这个编码了选择使用Powershell启用/禁用网络适配器,powershell,adapter,Powershell,Adapter,我正在尝试使用Powershell来选择启用/禁用网络适配器“以太网” 我把这个编码了 $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
$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}
}
错误:
对象不能存储在此类型的数组中。
在D:\Untitled4.ps1:5字符:1
+$choices=System.Management.Automation.Host.ChoiceDescription[];
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+类别信息:OperationsStopped:(:)[],InvalidCastException
+FullyQualifiedErrorId:System.InvalidCastException
使用“4”参数调用“PromptForChoice”时出现异常:“值”
不能为空。参数名称:choices“位于D:\Untitled4.ps1:6 char:1
+$answer=$host.ui.PromptForChoice($caption,$message,$choices,0)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+CategoryInfo:NotSpecified:(:)[],MethodInvocationException
+FullyQualifiedErrorId:ArgumentNullException
在此之前,我无法使用powershell执行开/关脚本操作(如果启用了网络适配器,则禁用它,反之亦然)。有什么方法可以做到这一点吗?使用此方法:我认为您希望执行以下操作:
$caption=“选择操作:”
$message=“您希望网络适配器处于什么状态?”
$enable=新对象System.Management.Automation.Host.ChoiceDescription“&enable”`
“启用网络适配器”
$disable=新对象System.Management.Automation.Host.ChoiceDescription“&disable”`
“禁用网络适配器”
$options=[System.Management.Automation.Host.ChoiceDescription[]($enable,$disable)
$result=$host.ui.PromptForChoice($caption、$message、$options,0)
切换($result)
{
0 {
“您选择了启用。”
启动进程powershell-动词运行方式-参数D:\ena.ps1
}
1 {
“您选择了禁用。”
启动进程powershell-动词运行方式-参数D:\dis.ps1
}
}
您所采用的方法不起作用,因为您试图将
进程
分配给ChoiceDescription
数组。在上面的示例中,您必须先创建两个ChoiceDescription
对象,然后再将它们分配给数组谢谢,它起作用了,我注意到了我犯的错误