C# 如何在远程运行空间中使用powershell管理Exchange

C# 如何在远程运行空间中使用powershell管理Exchange,c#,powershell,office365,powershell-remoting,C#,Powershell,Office365,Powershell Remoting,我的老板让我建立一个web界面,允许某人在Exchange Online中重置邮箱密码。 获取我找到的所有信息(角色、邮件、重置密码等)的唯一方法是通过PowerShell。所以我尝试在ASP.Net中运行powershell。简单批处理很简单,但当我尝试在Exchange上连接时,所有操作都失败了 WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://ps.outlook.com/powers

我的老板让我建立一个web界面,允许某人在Exchange Online中重置邮箱密码。
获取我找到的所有信息(角色、邮件、重置密码等)的唯一方法是通过PowerShell。所以我尝试在ASP.Net中运行powershell。简单批处理很简单,但当我尝试在Exchange上连接时,所有操作都失败了

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://ps.outlook.com/powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", Credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
connectionInfo.MaximumConnectionRedirectionCount = 2;
connectionInfo.ProxyAccessType = System.Management.Automation.Remoting.ProxyAccessType.IEConfig;
connectionInfo.ProxyAuthentication = AuthenticationMechanism.Digest;
connectionInfo.ProxyCredential = ProxyCredential;
runspace = RunspaceFactory.CreateRunspace(connectionInfo);
runspace.Open();
凭据与新PSCredential类似(“admin@company.onmicrosoft.com,Securite.converttosecuring(“密码”)和ProxyCredential类似于新的PSCredential(“用户帐户”,Securite.converttosecuring(“密码”),因为它是LDAP帐户,我不知道是否设置了域

runspace.open()抛出System.Management.Automation.Remoting.PSRemotingTransportException,并显示此消息

La connexion au serveur distant a échoué avec le message d'erreur suivant : Accès refusé. Pour plus d'informations, voir la rubrique d'aide about_Remote_Troubleshooting.
威奇将被翻译成

remote server connexion has failed with this error message : Access Denied. For more informations, go to about_Remote_Troubleshooting.  
但我们是“关于远程故障排除”吗

所以,我的问题是我做错了什么?我能做些什么来修复它

感谢您的关注,抱歉我的英语,并提前感谢您的回答;o)


编辑:
我试图重写代码以使用PSSession

powershell = PowerShell.Create();
powershell.AddCommand("Set-Variable");
powershell.AddParameter("Name", "cred");
powershell.AddParameter("Value", Credential);
powershell.AddCommand("Set-ExecutionPolicy").AddParameter("ExecutionPolicy", "RemoteSigned");
powershell.AddCommand("Enable-PSRemoting").AddParameter("Force");
powershell.AddScript("$proxysettings = New-PSSessionOption -ProxyAccessType IEConfig");
powershell.AddScript("$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri 'https://ps.outlook.com/powershell/' -Credential $cred -Authentication Basic -AllowRedirection -SessionOption $proxysettings");
powershell.AddScript("Import-PSSession $session");
powershell.AddScript("Get-RoleGroup | Where-Object {$_.Members -EQ 'w.user'} | Select -Property Name  | Format-List | out-string");
Collection<PSObject> results = powershell.Invoke();
if (powershell.Streams.Error.Count > 0)
{
    String error = String.Empty;
    foreach (ErrorRecord _error in powershell.Streams.Error)
    {
        error += _error.Exception.Message + "\r\n";
    }
    throw new Exception(error);
}
翻译成:

Access denied : read about_Remote_Troubleshooting.
Invalid parameter "Session". Parameter is null.
"Get-RoleGroup" is not a cmdlet, function, script or executable. Check the orthographe...

我被诅咒/o\

关于远程故障排除,我敢打赌,这是一个powershell commandlet。哦,看,互联网:你需要使用远程PSSession,而不是运行空间。你为该用户启用远程shell访问吗?谢谢你的回答。已为此用户启用远程shell。我重写了代码,但仍然不起作用;o) 尝试使用Kerberos身份验证。我认为默认情况下,这些远程Exchange管理会话不允许基本身份验证。
Access denied : read about_Remote_Troubleshooting.
Invalid parameter "Session". Parameter is null.
"Get-RoleGroup" is not a cmdlet, function, script or executable. Check the orthographe...