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();
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,266 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,377 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 44,856 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