无法连接到Exchange PowerShell以使用C#远程运行脚本

时间:2016-02-03 15:54:14

标签: c# powershell exchange-server smtpclient

我需要在我们的Exchange服务器上运行PowerShell脚本。我在本地计算机和SMTP服务器上启用了PS-Remoting和WinRM-quickconfig。当我尝试连接时,我收到以下错误。

连接到远程服务器失败,并显示以下错误消息:WinRM无法处理请求。使用Kerberos身份验证时出现以下错误:当前没有可用于登录请求的登录服务器。

以下是我的连接代码 注意:我可以使用RDP

使用此用户帐户连接到SMTP服务器
public static void Main(string[] args)
        {
            //CREATE SECURE STRING PASSWORD
            string pwd = "MyPasswordAsAString";
            char[] cpwd = pwd.ToCharArray();
            SecureString pass = new SecureString();
            foreach (char c in cpwd)
            pass.AppendChar(c);
            //CREATE PS CREDENTIAL
            PSCredential Credential = new PSCredential("Domain\\Username", pass);
            //RESOURCE URI
            string shell = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";

            //ADDRESS OF SMTP SERVER
            var target = new Uri("HTTP://FULLYQUALIFIEDDOMAINNAME");

            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(target, shell, Credential );

            //CREATE RUNSPACE
            Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);

            //OPEN CONNECTION
            runspace.Open();
            using (PowerShell ps = PowerShell.Create())
            { 
                ps.Runspace = runspace;
                ps.AddScript("Get-Service"); 
                var results = ps.Invoke();
                // Do something with result ... 
            }  
            runspace.Close();




            //AFTER FILE IS CREATED THROUGH POWERSHELL READ IT WITH EXCELL
            DataTable PF2 = processFile(@"C:\inetpub\Clients\MLK\confRooms-permissions.xlsx", "xlsx");
            importData(PF2);
        }

1 个答案:

答案 0 :(得分:0)

想出来了! 将变量目标更改为Uri类型和地址为service i添加了port / wsman