Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Powershell Command is: Set-NetIPInterface -InterfaceIndex 11 -InterfaceMetric 99

This command is working when I run it on PowerShell as admin. but:
C# Code
PowerShell ps = PowerShell.Create();
ps.AddCommand("Set-NetIPInterface -InterfaceIndex 11 -InterfaceMetric 99");
try
{
 ps.invoke();
}
catch(Exception e)
{
 MessageBox.Show(e.Message+"");
}

When I run the above code I am getting the error message "The term 'Set-NetIPInterface -InterfaceIndex 11 -InterfaceMetric 99' is not recognized as the name of a cmdlet, function, script file or operable program. check the spelling of the name, or if a path was included, verify that the path is correct and try again"

Please help me out in resolving this issue.

What I have tried:

I didn't find much on the internet to resolved this issue
Posted
Updated 6-Sep-22 2:27am
v2
Comments
Member 14686752 1-Sep-20 1:32am    
The above work that I am doing is for changing the priority of Network cards, Is there any other way to do the same using C#?

Use AddScript() instead of AddCommand() and it will work
 
Share this answer
 
Check that your C# app has been elevated: if it isn't running as an admin, then nothing it tries to run will be either.
 
Share this answer
 
Comments
Member 14686752 31-Aug-20 7:38am    
Yes it is running as admin
Try setting it up like this Executing PowerShell scripts from C# | Microsoft Docs[^]

I do like the
using (PowerShell PowerShellInstance = PowerShell.Create())
{
// ... 
}


(using) to set up and tidy up the Powershell instance
 
Share this answer
 
Comments
Member 14686752 31-Aug-20 6:40am    
Getting the same error
Garth J Lancaster 31-Aug-20 6:48am    
see if this runs it https://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900