从C#连接到Exchange的PowerShell命令

时间:2012-05-11 23:26:56

标签: c# powershell exchange-server

我有以下命令连接到Exchange PowerShell。我使用C#进行连接,但最后一行Import-PSSession $Session会导致站点崩溃并将我带到调试器。

$password = ConvertTo-SecureString -AsPlainText -Force mypassword;$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist me@mail.net,$password;$LiveCred = Get-Credential -Credential $mycred;$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlookurlforpowershell/ -Credential $LiveCred -Authentication Basic –AllowRedirection; Import-PSSession $Session

这是我的C#代码:

private void initconnection(string text){

    Runspace rs = GetRunspace();
    Pipeline cmd = rs.CreatePipeline(text);
    Collection<PSObject> results = cmd.Invoke();
}

initconnection("$password = ConvertTo-SecureString -AsPlainText -Force Maxipereyra15;$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist cpereyra@crescentmortgage.net,$password;$LiveCred = Get-Credential -Credential $mycred;$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic –AllowRedirection; Import-PSSession $Session");

如何解决此问题?

0 个答案:

没有答案