0

I am using .Net 4.6.1 for a Winform application.

I neet to execute some command to configure user settings through powershell. The powershell reference that I used is under C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0, so (I guess) its version is 3.0.

I use following to execute powershell command:

System.Management.Automation.PowerShell shell = System.Management.Automation.PowerShell.Create();
shell.AddScript("Get-LocalUser -Verbose");

then check

shell.Streams.Error[0].Exception;

and see following error:

The term 'Get-LocalUser' 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.

I also tried executing the command through a powershell file like:

var psi = new ProcessStartInfo();
psi.CreateNoWindow = false;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.Verb = "runas";
psi.FileName = @"powershell.exe";
psi.Arguments = $" -File \"<path-to-file>\\MyScript.ps1\"";
try
    {
        Process proc = new Process();
        proc.StartInfo = psi;
        proc.Start();
        string f = proc.StandardError.ReadToEnd();
        proc.WaitForExit();
        exitCode = proc.ExitCode;
    }

But I got the same error.

How can I call a powershell script or powershell within c#?

EDIT: it turns out to be 32-64 bit issue. When I switched to x64, it worked just fine.

Mp0int
  • 18,172
  • 15
  • 83
  • 114
  • 2
    Possible duplicate of [Execute PowerShell Script from C# with Commandline Arguments](https://stackoverflow.com/questions/527513/execute-powershell-script-from-c-sharp-with-commandline-arguments) – Avi Meltser May 17 '19 at 13:04
  • did you try executing the same command in a powershell window on the same machine? Does it work? Maybe the relevant module is just missing...that's what the error implies. – ADyson May 17 '19 at 13:05
  • Also, what version of powershell? According to the docs this command is only available in PS 5.1 – ADyson May 17 '19 at 13:08
  • Also, a lot of PS code only works in a 64-bit environment...did you compile your C# to target 64-bit? – ADyson May 17 '19 at 13:08
  • All in all, I'd say it's 50/50 whether this is actually anything to do with C#, or just a powershell issue. – ADyson May 17 '19 at 13:08
  • @ADyson I tried the command with powershell and it worked just fine. `$PSVersionTable.PSVersion` displays it as 5.1. Since I want to port related Powershell dll's with my application, I took them from the location I mention above.Is this a version difference issue? Where can I reference correct version of dll's into my project – Mp0int May 17 '19 at 13:22
  • 2
    @ADyson it turns out to be 32-64 bit issue. When Iswitched to x64, it workd just fine. Thanks – Mp0int May 17 '19 at 13:31
  • 1
    Added that as an answer, cheers for getting back to me. As for porting the DLLs, I'm not sure that's going to work (although I don't know enough about it to say for certain). But why do you want to? it seems unnecessary really. PS 5.1 comes as standard on recent windows versions, and can easily be installed on others. If I had to guess I'd say creating a working powershell environment is probably quite a bit more complicated than just bundling a few library files. – ADyson May 17 '19 at 13:34

2 Answers2

3

You should ensure you compile your C# code to target x64 (i.e. 64-bit) as most PowerShell modules will only work in 64-bit mode. If your C# code runs in 32-bit mode, it will invoke a 32-bit PowerShell environment, which will cause problems.

ADyson
  • 57,178
  • 14
  • 51
  • 63
1

I had the similar experience and succeeded only after changing ExecutionPolicy. The error was equal, but all the details seemed fine.

PS 7.1.3, Microsoft.Powershell.SDK.

Use this:

PowerShell.Create().AddCommand("Set-ExecutionPolicy")
            .AddParameter("-ExecutionPolicy", "Bypass")
            .Invoke();

Run this code once before doing anything with the PowerShell.

walkedin
  • 11
  • 2