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";
}
I have Set-ExecutionPolicy to Unrestricted in the Powershell as well.
Thanks in advance!