Powershell and Exchange in C #

I have the following C # code that I use to connect to an exchange through powershell.

The following code works fine, but another command is required to use the Exchange cmdlets.

Here is the code I have.

Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
PowerShell powershell = PowerShell.Create();

PSCommand command = new PSCommand();
command.AddCommand("New-PSSession");
command.AddParameter("ConfigurationName", "Microsoft.Exchange");
command.AddParameter("ConnectionUri", new Uri("https://ps.outlook.com/powershell/"));
command.AddParameter("Credential", creds);
command.AddParameter("Authentication", "Basic");
command.AddParameter("AllowRedirection");

powershell.Commands = command;

try
{
    runspace.Open();
    powershell.Runspace = runspace;

    Collection<PSObject> commandResults = powershell.Invoke();

    StringBuilder sb = new StringBuilder();

    foreach (PSObject ps in commandResults)
    {
        sb.AppendLine(ps.ToString());
    }

    sb.AppendLine();

    lbl.Text += sb.ToString();
}
finally
{
    // dispose the runspace and enable garbage collection
    runspace.Dispose();
    runspace = null;
    // Finally dispose the powershell and set all variables to null to free
    // up any resources.
    powershell.Dispose();
    powershell = null;
}

My problem is that I still need to run the command import-pssession $session, where $sessionis the result of my first command. However, I'm not sure how I can declare this output as a $ session variable or something like:

 PSCommand command = new PSCommand();
 command.AddCommand("Import-PSSession");
 command.AddParameter("Session", #Not sure how to put session info which is what the first command produces into here.);
+3
source share
2 answers

, . http://msdn.microsoft.com/en-us/library/windows/desktop/ee706560 (v = vs .85). ASPX.

string schemaURI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
Uri connectTo = new Uri("https://ps.outlook.com/powershell/");
PSCredential credential = new PSCredential(user,secureStringPassword ); // the password must be of type SecureString
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(connectTo,schemaURI, credential);
connectionInfo.MaximumConnectionRedirectionCount = 5;
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

try
{
    Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo);
    remoteRunspace.Open();
}
catch(Exception e)
{
    //Handle error 
}
+3

" ( )" http://blogs.technet.com/b/exchange/archive/2009/11/02/3408653.aspx

, :

    // Set the runspace as a local variable on the runspace
    powershell = PowerShell.Create();
    command = new PSCommand();
    command.AddCommand("Set-Variable");
    command.AddParameter("Name", "ra");
    command.AddParameter("Value", result[0]);
    powershell.Commands = command;
    powershell.Runspace = runspace;
    powershell.Invoke();

[0] - . , .

+1

All Articles