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();
}
1
  • What outcome are you expect from adding string and PSCredential? Commented Feb 8, 2017 at 17:14

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

5 Comments

I tried it. But it throws "Cannot perform operation because operation "NewNotImplementedException at offset 78 in file:line:column <filename unknown>:0:0 " is not implemented." error.
Ok, the question is if you can actually establish a remote session to "servername/poweshell". When you run $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri servername/poweshell -Credential credential; Import-PSSession $Session" directly from PowerShell, can you return any data? Btw this could help as well: msdn.microsoft.com/en-us/library/bb332449(v=exchg.80).aspx
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
How about those solutions? stackoverflow.com/questions/36236897/…
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. stackoverflow.com/questions/42136343/…
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.