C# 在C中使用Powershell访问Exchange#
我正在尝试使用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);
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);