1

C# Code: The C# code is unable to call the function Trial2 which is present in the powershell script although it is being executed before.

    StringBuilder sb = new StringBuilder();
    PowerShell psExec = PowerShell.Create();
    psExec.AddScript(@ "C:\Users...\sc.ps1");
    psExec.AddCommand("Trial2").AddParameter("a", "Ram");
    Collection < PSObject > results;
    Collection < ErrorRecord > errors;
    results = psExec.Invoke();
    errors = psExec.Streams.Error.ReadAll();
    if (errors.Count > 0) {
        foreach(ErrorRecord error in errors) {
            sb.AppendLine(error.ToString());
        }
    } else {
        foreach(PSObject result in results) {
            sb.AppendLine(result.ToString());
        }
    }
    Console.WriteLine(sb.ToString());

Powershell Script:

function Trial2($a){
    "Yes! $a";
}

Error I get: enter image description here

I have Set-ExecutionPolicy to Unrestricted in the Powershell as well.

Thanks in advance!

mklement0
  • 382,024
  • 64
  • 607
  • 775
Amrit Ohri
  • 11
  • 3
  • You could try "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine"...it'll apply to all users and when running with no profile – Ctznkane525 Jun 15 '21 at 12:18
  • 2
    Change `psExec.AddScript("Set-ExecutionPolicy")` to `psExec.AddCommand("Set-ExecutionPolicy")`, and then call `AddStatement()` before calling `AddScript(...)` on the next line. – Mathias R. Jessen Jun 15 '21 at 12:19
  • Does this answer your question? [PowerShell says "execution of scripts is disabled on this system."](https://stackoverflow.com/questions/4037939/powershell-says-execution-of-scripts-is-disabled-on-this-system) – ba-a-aton Jun 15 '21 at 12:27
  • also, do not post errors as pictures, please – ba-a-aton Jun 15 '21 at 12:42
  • Change `psExec.AddScript(@"C:\Users...\sc.ps1")` to `psExec.AddScript(@ ". 'C:\Users...\sc.ps1'")` (notice the leading `.`) - this will dot-source the script as opposed to just executing it in itw own scope – Mathias R. Jessen Jun 15 '21 at 12:43
  • @MathiasR.Jessen I have dot sourced it but still it gives the same error. – Amrit Ohri Jun 15 '21 at 13:04
  • @AmritOhri You need another `AddStatement()` in between dot-sourcing the script and `AddCommand("Trial2")`. Otherwise it's the equivalent of `. 'script.ps1' | Trial2` (which is probably not what you want) – Mathias R. Jessen Jun 15 '21 at 14:26

1 Answers1

0

Mathias R. Jessen has provided all the necessary pointers in his comments, but let me put it all together:

PowerShell psExec = PowerShell.Create();

// Add a script block that dot-sources (.) your .ps1 file,
// which in turn defines the Trial2 function.
psExec.AddScript(@". C:\Users...\sc.ps1");

// Start a new statement to ensure that the dot-sourcing is performed
// before additional commands are executed.
psExec.AddStatement();

// Now you can add a command that calls your Trial2 function.
psExec.AddCommand("Trial2").AddParameter("a", "Ram");

// ...

Note that the API is fluent, so you could use a single statement; the following demonstrates this, and also shows how to set the execution policy directly via the API rather than by submitting a Set-ExecutionPolicy command, as you originally attempted:

// Create an initial default session state.
var iss = System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2();
// Set its script-file execution policy.
iss.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Bypass;

// Create a PowerShell instance with a runspace based on the 
// initial session state.
PowerShell psExec = PowerShell.Create(iss);

psExec
  .AddScript(@". C:\Users...\sc.ps1")
  .AddStatement()
  .AddCommand("Trial2").AddParameter("a", "Ram");

// ...

Note that setting the execution policy this way is the equivalent of setting it with
Set-ExecutionPolicy -Scope Process. This means that it'll stay in effect for all PowerShell sessions started from the current process.

mklement0
  • 382,024
  • 64
  • 607
  • 775