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

本文关键字:程运行 运行 脚本 连接 Exchange PowerShell | 更新日期: 2023-09-27 18:33:00

我需要在我们的交换服务器上运行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);
        }

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

想通了!将变量目标更改为 Uri 类型,将地址更改为我添加端口/wsman 的服务