Refining a function: How to run command on remote exchange server and receive results?

40 Views Asked by At

I have a .NET function which:

  1. Connects to Exchange
  2. Imports the session to Localhost
  3. Then runs the command

I have not been able to figure out how to just directly run a command on the Exchange server (via powershell) and then receive the response back - I have only been able to do it with this rather roundabout way.

How can I remotely run a Powershell command on Exchange and receive the response back in .NET?

This is my function:

protected string Receive_Exchange(string lscript)
{
//create credentials + encrypt them 
    string runasUsername = loginDetails.Username;
    string runasPassword = loginDetails.Password;
    SecureString ssRunasPassword = new SecureString();
    foreach (char x in runasPassword)
    {
        ssRunasPassword.AppendChar(x);
    }
// bind to PS credentials Object
    PSCredential credentials =
        new PSCredential(runasUsername, ssRunasPassword);

// prepare the connection to Remote server (Exchange 2010)
    var connInfo = new WSManConnectionInfo(
    new Uri("http://exchangeServer.domain.local/PowerShell"),
    "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
    credentials);
    object psSessionConnection;
//need .default in order to send it (not .basic)
    connInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
//Define/set executionPolicy
    InitialSessionState initialSessionState = InitialSessionState.CreateDefault();
    initialSessionState.ExecutionPolicy = ExecutionPolicy.RemoteSigned;
//open runspace
    Runspace runspace = RunspaceFactory.CreateRunspace(initialSessionState);
    runspace.Open();
    PowerShell powershell = PowerShell.Create();
//send the remote connection request to exchange
    var command = new PSCommand();
    command.AddCommand("New-PSSession");
    command.AddParameter("ConfigurationName", "Microsoft.Exchange");
    command.AddParameter("ConnectionUri", new Uri("http://exchangeServer.domain.local/PowerShell"));
    command.AddParameter("Authentication", "Kerberos");
    powershell.Commands = command;
    powershell.Runspace = runspace;
    var result = powershell.Invoke();
    psSessionConnection = result[0];
//start to build command to import exchange connection to localhost
    var command1 = new PSCommand();
    command1.AddCommand("Import-PSSession");
    command1.AddParameter("Session", psSessionConnection);
    powershell.Commands = command1;
    powershell.Runspace = runspace;
//after importing connection, actually run the script
    powershell.AddScript(lscript);
    var result22 = powershell.Invoke();
    runspace.Close();
    string json = JsonConvert.SerializeObject(result22, Formatting.Indented,
                new JsonSerializerSettings()
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                });
    return json;
} 

It is a behemoth and it is inefficient causing my entire app to slow down.

I love that it works but I hate how it works - please help me in making it run in a more straightforward way.

0

There are 0 best solutions below