2016-05-17 4 views
0

Ich bekomme einen Begriff nicht erkannt Fehler, wenn ich die folgenden 3 Zeilen Code ausführen, aber wenn ich den Befehl Netsh direkt in PowerShell eingeben, funktioniert es. Was ist los mit meinem Ansatz?Wie aktiviert man eine Netzwerkkarte mit PowerShell in C#?

PowerShell ps = PowerShell.Create(); 
ps.AddCommand("netsh interface set interface \"Wi-Fi\" admin=enable"); 
ps.Invoke(); 

Antwort

1

Verwenden Invoke-Expression, wie netsh kein gültiger Befehl Powershell ist

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(); 
Verwandte Themen