I have a PSCredential Object in C# and want to pass it as parameter for this PowerShell Script
This is the PSCredential Object
PSCredential Credential = new PSCredential ( "bla" , blasecurestring)
This is the Script I want to run in C#
powershell.AddScript("$s = New-PSSession -ComputerName '" + serverName + "' -Credential " + Credential);
I couldn't understand the solution which is offered here Pass a Parameter object (PSCredential) inside a ScriptBlock programmatically in C#
EDIT: This thing is working
powershell.AddCommand("New-PSSession").AddParameter("ComputerName", serverName).AddParameter("Credential", Credential);
But how can I save the Session Info in a variable? I need them for the following commands:
powershell.AddScript(@"Invoke-Command -Session $s -ScriptBlock {" + cmdlet + "}");