简体   繁体   English

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

[英]Cannot connect to Exchange PowerShell to run scripts remotely using C#

I need to run PowerShell script on our exchange server. 我需要在我们的交换服务器上运行PowerShell脚本。 I've enabled PS-Remoting and WinRM-quickconfig on both my local machine and SMTP server. 我已经在本地计算机和SMTP服务器上启用了PS-Remoting和WinRM-quickconfig。 When i try to connect I receive the error below. 当我尝试连接时,我收到以下错误。

Connecting to remote server failed with the following error message : WinRM cannot process the request. 连接到远程服务器失败,并显示以下错误消息:WinRM无法处理该请求。 The following error occured while using Kerberos authentication: There are currently no logon servers available to service the logon request. 使用Kerberos身份验证时发生以下错误:当前没有可用于服务登录请求的登录服务器。

Here is my connection code below NOTE: I can connect to SMTP server with this user account using RDP 这是下面的连接代码注意:我可以使用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);
        }

Figured this out! 想通了! Changed the variable target to Uri type and address to service i added port/wsman 将变量目标更改为Uri类型和地址以服务我添加的port / wsman

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM