I try to run Windows PowerShell workflow script from console application.
Approach 1:
var processInfo = new System.Diagnostics.ProcessStartInfo()
{
FileName = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe",
Arguments = $"\"{scriptFile}\"",
UseShellExecute = false
};
System.Diagnostics.Process.Start(processInfo).WaitForExit();
Approach 2 :
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
{
runspace.Open();
RunspaceInvoke runspaceInvoke = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.Add(new Command(scriptFile));
var psObjects = pipeline.Invoke();
}
The above two approaches give me the error - Windows PowerShell Workflow is not supported in a Windows PowerShell x86-based console. Open a Windows PowerShell x64-based console, and then try again
Approach 3:
WSManConnectionInfo connectionInfo = new WSManConnectionInfo();
connectionInfo.ShellUri = "Microsoft.PowerShell.Workflow";
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
runspace.Open();
PowerShell powershell = PowerShell.Create();
powershell.AddCommand(scriptFile, true);
powershell.Invoke();
Error for Approach 3 - Connecting to remote server localhost failed with the following error message : Access is denied.
I am bit hesitant to use Set-PSSessionConfiguration as I am not much aware of its after effects. I am running this code on Windows Server 2012.
How can I run the workflow using c# ?