1

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.

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();
}
Member0927
  • 41
  • 1
  • 2
  • 9

2 Answers2

3

I could not test this, but it might put you on the right track:

        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);

        string scriptPath = $@"
        $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://servername/poweshell -Credential {remoteCredential} | Out-String
        Import-PSSession $Session";

        Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
        runspace.Open();
        RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
        Pipeline pipeline = runspace.CreatePipeline();
        string scriptfile = scriptPath;
        Command myCommand = new Command(scriptfile, false);
        pipeline.Commands.Add(myCommand);
        pipeline.Invoke();
        runspace.Close();
  • I tried it. But it throws "Cannot perform operation because operation "NewNotImplementedException at offset 78 in file:line:column :0:0 " is not implemented." error. – Member0927 Feb 08 '17 at 11:31
  • Ok, the question is if you can actually establish a remote session to "http://servername/poweshell". When you run $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://servername/poweshell -Credential credential; Import-PSSession $Session" directly from PowerShell, can you return any data? Btw this could help as well: https://msdn.microsoft.com/en-us/library/bb332449(v=exchg.80).aspx – Matt Szadziul Feb 08 '17 at 13:01
  • I've tried above solution but to no avail. The problem here is I can run PSSession command from Windows PowerShell command prompt. But same is not working from my .Net application. When I invoke PowerShell command it returns 0 count. "var result = ps.Invoke();" Count in var result is 0 – Member0927 Feb 08 '17 at 13:25
  • How about those solutions? http://stackoverflow.com/questions/36236897/connect-to-microsoft-exchange-powershell-within-c-sharp – Matt Szadziul Feb 08 '17 at 18:42
  • It worked. I can now connect to powershell and create session. But another issue now is that when I try to create a mailbox using "New-Mailbox" command it returns the error that "A parameter cannot be found that matches parameter name 'Identity". I've posted the issue on new thread. http://stackoverflow.com/questions/42136343/create-mailbox-on-exchange-2010-using-c-sharp – Member0927 Feb 09 '17 at 12:11
1

I've got an article that describes an easy way to run Powershell through WinRM from .NET at http://getthinktank.com/2015/06/22/naos-winrm-windows-remote-management-through-net/ or you can get the code directly at https://github.com/NaosProject/Naos.WinRM.

I've been using this for years with any parameter encoding issues and it grossly simplifies running these operations...

The code is in a single file if you want to just copy it and it's also a NuGet package that includes the reference to System.Management.Automation.

// this is the entrypoint to interact with the system (interfaced for testing).
var machineManager = new MachineManager(
    "10.0.0.1",
    "Administrator",
    MachineManager.ConvertStringToSecureString("xxx"),
    true);

// can run random script blocks WITH parameters.
var fileObjects = machineManager.RunScript(
    "{ param($path) ls $path }",
    new[] { @"C:\PathToList" });

Hope this helps, I've been using this for a while with my automated deployments. Please leave comments if you find issues.

wlscaudill
  • 505
  • 4
  • 6