当我运行以下3行代码时,我得到一个术语无法识别的错误,但是如果我直接在PowerShell中输入netsh命令就可以了。我的方法有什么问题?
PowerShell ps = PowerShell.Create();
ps.AddCommand("netsh interface set interface \"Wi-Fi\" admin=enable");
ps.Invoke();
答案 0 :(得分:1)
使用Invoke-Expression,因为netsh不是有效的Powershell命令
PowerShell ps = PowerShell.Create();
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
ps.Runspace = runspace;
ps.AddCommand("Invoke-Expression");
ps.AddArgument("netsh interface set interface \"Wi-Fi\" admin=enable");
// if you need the result save it in a psobject
Collection<PSObject> result = ps.Invoke();
runspace.Close();