我正在尝试使用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 = [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 char:1 + $ choices = System.Management.Automation.Host.ChoiceDescription []; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:OperationStopped:(:) [],InvalidCastException + FullyQualifiedErrorId:System.InvalidCastException
使用“4”参数调用“PromptForChoice”的异常:“Value 不能为空。参数名称:选项“在D:\ Untitled4.ps1:6 char:1 + $ answer = $ host.ui.PromptForChoice($ caption,$ message,$ choices,0) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId:ArgumentNullException
我之前在使用powershell执行On / Off脚本时失败了(如果net适配器启用然后禁用它,反之亦然。有没有办法做到这一点?
答案 0 :(得分:2)
使用此: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
对象,然后再将它们分配给数组