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!

7
  • You could try "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine"...it'll apply to all users and when running with no profile Commented Jun 15, 2021 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. Commented Jun 15, 2021 at 12:19
  • Does this answer your question? PowerShell says "execution of scripts is disabled on this system." Commented Jun 15, 2021 at 12:27
  • also, do not post errors as pictures, please Commented Jun 15, 2021 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 Commented Jun 15, 2021 at 12:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.