question

Karimakabery-2950 avatar image
0 Votes"
Karimakabery-2950 asked TimonYang-MSFT commented

Command Not found exeption when running command from powershell in c#

'm currently using visual studio 2022 and System.Management.Automation verion 7.1 I want to build an app that could run a PowerShell Script and return a string of result

Here is My Code

public string GetNetAdapterList()
{

     Runspace rs = RunspaceFactory.CreateRunspace();
     rs.Open();
     Pipeline pipeline = rs.CreatePipeline();
     pipeline.Commands.Add("Rename-LocalUser ");
     pipeline.Commands.Add("Out-String");
     Collection<PSObject> results = pipeline.Invoke();
     rs.Close();
     StringBuilder stringBuilder = new StringBuilder();  

     foreach (PSObject ps in results)
     {
         stringBuilder.AppendLine(ps.ToString());
     }
     return stringBuilder.ToString();

 }

after i run this app I Get Command Not Found Exception how to solve this problem?

Full Error : An exception of type 'System.Management.Automation.CommandNotFoundException' occurred in System.Management.Automation.dll but was not handled in user code The term 'Rename-LocalUser ' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

dotnet-csharpwindows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered TimonYang-MSFT commented

There is no problem with the code. This error shows that the powershell command cannot be recognized.

You can add parameters like this:

             Pipeline pipeline = rs.CreatePipeline();
    
             Command scriptCommand = new Command("Get-Help");
             CommandParameter commandParm = new CommandParameter("Name", "Format-Table");
             scriptCommand.Parameters.Add(commandParm);
             pipeline.Commands.Add(scriptCommand);

You can also use the PowerShell class and then use the PowerShell.AddParameter Method to add parameters.

PowerShell.AddParameter Method


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Karimakabery-2950
Is it useful to add parameters like this? If you still have any questions, please let us know.

0 Votes 0 ·
Castorix31 avatar image
0 Votes"
Castorix31 answered

It must be compiled as x64
+ remove the space at the end of "Rename-LocalUser "
+ parameters are missing for this command...



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.