Create a PS Cmdlet in c# that opens a PSSession and outputs session

Anthony Antonacci 1 Reputation point
2021-12-26T21:10:56.617+00:00

Hi Guys,

I'm trying to create a PS Cmdlet in C# that will Creaet a PS-Session (Runspace?) and output the Ps-Session to the user.
i am able to establish a connection in c# but i can't figure out how to pass it to PowerShell? (i assume i need to output a ps session?)

PSCredential creds = new PSCredential(Uname, Pass);


WSManConnectionInfo connectionInfo = new WSManConnectionInfo();
connectionInfo.ComputerName = IP;
connectionInfo.Port = Port;
connectionInfo.Credential = creds;
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Credssp; 

Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
runspace.Open();
Windows for business | Windows Server | User experience | PowerShell
Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2021-12-27T00:40:35.52+00:00

    You could start here: using-c-to-create-powershell-cmdlets-the-basics
    create-powershell-cmdlets-in-c-sharp-pipeline-chaining

    There are more if you search for "create powershell cmdlet using c#"

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.