Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中使用Powershell访问Exchange#_C#_Powershell_Exchange Server - Fatal编程技术网

C# 在C中使用Powershell访问Exchange#

C# 在C中使用Powershell访问Exchange#,c#,powershell,exchange-server,C#,Powershell,Exchange Server,我正在尝试使用c中的powershell访问exchange服务器# 这是我试图使用的代码 WSManConnectionInfo ConnectionInfo = new WSManConnectionInfo(new Uri(@"http://myexchangeserver.com"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", (PSCredential)null);

我正在尝试使用c中的powershell访问exchange服务器# 这是我试图使用的代码

            WSManConnectionInfo ConnectionInfo = new WSManConnectionInfo(new Uri(@"http://myexchangeserver.com"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", (PSCredential)null);
            ConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
            using (Runspace runspace = RunspaceFactory.CreateRunspace(ConnectionInfo))
            {
                using (PowerShell PS_Instance = PowerShell.Create())
                {
                    PS_Instance.AddCommand("Get-MailboxPermission -Identity UAT_TestGeneric");

                    runspace.Open();
                    PS_Instance.Runspace = runspace;
                    Collection<PSObject> results = PS_Instance.Invoke();

                    if (PS_Instance.Streams.Error.Count > 0)
                    {
                        //Couldnt Connect To Server
                    }
                    else
                    {
                        //Do Stuff
                    }
                }
WSManConnectionInfo ConnectionInfo=newwsmanconnectioninfo(新Uri(@)http://myexchangeserver.com"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange“,(PSCredential)null);
ConnectionInfo.AuthenticationMechanism=AuthenticationMechanism.Kerberos;
使用(Runspace Runspace=RunspaceFactory.CreateRunspace(ConnectionInfo))
{
使用(PowerShell PS_Instance=PowerShell.Create())
{
PS_Instance.AddCommand(“Get-MailboxPermission-Identity UAT_TestGeneric”);
Open();
PS_Instance.Runspace=运行空间;
集合结果=PS_Instance.Invoke();
如果(PS_Instance.Streams.Error.Count>0)
{
//无法连接到服务器
}
其他的
{
//做事
}
}
我一直收到这个错误消息

WinRM客户端从远程服务器接收到HTTP状态代码403 WS-Management服务。有关更多信息,请参阅 关于远程故障排除帮助主题

我已检查了exchange servers powershell IIS文件夹,它不需要SSL,如此处所示

有人可以帮忙吗?
谢谢。

由于错误403表示“拒绝访问”,因此我认为您正在使用的taskuser未启用远程powershell。请尝试通过(如MS文档中所述)启用该选项:

设置用户-RemotePowerShellEnabled$True

解决问题的另一个有用的来源是MS文档。

以防其他人看到。 我修复了这个问题,因为URI是错误的

WSManConnectionInfo ConnectionInfo = new WSManConnectionInfo(new Uri(@"http://myexchangeserver.com"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", (PSCredential)null);
uri必须为@“”。
因此,尽管在服务器上正确设置了powershell,但您需要连接到正确的IIS文件夹路径

,您可能希望先尝试使用powershell进行连接,以测试您是否具有正确的预请求,并检查什么是正确的身份验证等
WSManConnectionInfo ConnectionInfo = new WSManConnectionInfo(new Uri(@"http://myexchangeserver.com"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", (PSCredential)null);