Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been trying to create a connection with PowerShell from my .Net application using C#. After connection is done when I try to create a session, it returns empty collection.

What I have tried:

C#
string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";

PSCredential remoteCredential = new PSCredential("userID", StringToSecureString("Password"));

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "Ip Address of server", 5985, "/wsman", shellUri, remoteCredential, 1 * 60 * 1000);

runspace = RunspaceFactory.CreateRunspace(connectionInfo);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

runspace.Open();

using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = runspace;

ps.AddScript(@"$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://servername/poweshell -Credential " + remoteCredential);

//result count returned is 0 
var result = ps.Invoke();

ps.Commands.Clear();

ps.AddCommand("Import-PSSession $Session");

ps.Invoke();
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900