I have the following function in my C# file:
private void RunScriptFile(string scriptPath, string computerName, PSCredential credential)
{
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
string scriptCommand = "Invoke-Command -ComputerName " + computerName + " -FilePath " + scriptPath + " -ArgumentList " + credential;
pipeline.Commands.AddScript(scriptCommand);
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
}
I am calling the following PowerShell script using Credentials passed in the above C# code; script.ps1
Param([PSCredential]$Credentials)
<code part using credentials>
After pipeline.Invoke()
, C# code just gets closed without any action, no errors are thrown too.
Is there something I am doing wrong while making the call? The same call if invoked from PowerShell like below works fine:
Invoke-Command -ComputerName <computerName> -FilePath <scipt.ps1> -ArgumentList " + credential;