I'm currently using visual studio 2022 and System.Management.Automation version 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.
System.Management.Automationpackage is not recommended. If you usePowerShellStandard.Library, you're relying on the machine's regular PowerShell installation, which should give you the same commands as in an interactive session.ParameterBindingException?Rename-LocalUser : Parameter set cannot be resolved using the specified named parameters.)