I have been trying for days to add my custom cmdlet to the runspace of a remote powershell in C#
string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
System.Security.SecureString sString = new System.Security.SecureString();
foreach(char passwordChar in password.ToCharArray())
{
sString.AppendChar(passwordChar);
}
PSCredential credential = new PSCredential(username, sString);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, computerName, 5985, "/wsman", shellUri, credential);
try {
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open();
pipe = runspace.CreatePipeline();
pipe.Commands.AddScript(script);
Collection<PSObject> result = pipe.Invoke();
foreach (PSObject line in result)
{
scriptOutput.Add(line.ToString());
}
pipe.Dispose();
runspace.Close();
}
I have something like this that works to execute script on a remote machine. But I need to add a custom cmdlet.
I know with a local powershell you can use InitialSessionState like this :
InitialSessionState iss = InitialSessionState.CreateDefault();
SessionStateCmdletEntry gfv = new SessionStateCmdletEntry("Get-Custom", typeof(GetCustomCommand), null);
iss.Commands.Add(gfv);
using (Runspace runspace = RunspaceFactory.CreateRunspace(iss))
Where GetCustomCommand extends PSCmdlet.
But with remote powershell you can't initialize the runspace with InitialSessionState. How can I add an SessionStateCmdLetEntry to my remote runspace ?
Could someone help me ? Thanks