C# C语言中的Powershell与交换#

C# C语言中的Powershell与交换#,c#,powershell,exchange-server,C#,Powershell,Exchange Server,我用C#编写了以下代码,用于通过powershell连接到exchange 下面的代码工作正常,但是我还需要一个命令来使用exchange cmdlet 这是我现在的代码 Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(); PowerShell powershell = PowerShell.Create(); PSCommand command = new P

我用C#编写了以下代码,用于通过powershell连接到exchange

下面的代码工作正常,但是我还需要一个命令来使用exchange cmdlet

这是我现在的代码

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;
}
请尝试以下technet博客中的“使用本地运行空间的远程请求(编写远程类脚本)”一节

我相信你正在努力实现的是:

    // 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]是创建的第一个远程会话的结果。如果有帮助,请告诉我。

您可以尝试使用创建远程运行空间,下面给出了一个示例。您可以参考以下文章


您曾经解决过这个问题吗?然后您可以导入会话以使用它:使用powershell\uuuu2作为powershell=powershell.Create()command.AddScript(“导入PSSession-session$ra”)powershell\uuuuu2.Commands=command powershell\uuuuu2.Runspace=Runspace powershell\uuuuuuu 2.Invoke()结束使用
    // 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();
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 
}