c# 可以将登录凭据(非显式)与 wsmanconnectioninfo 一起使用吗?

标签 c# powershell exchange-server

我有下面的多线程函数,当我是唯一一个使用它的人时,这对于显式凭据很好,但我被要求将它放入一个 powershell 脚本中,它将是纯文本以利用多线程能力。

是否可以将运行脚本的帐户的隐含凭据与 wsmanconnectioninfo 一起使用?如果没有,是否有其他方法可以在没有显式凭据的情况下创建 Exchange shell 连接?

    private Collection<PSObject> runPowerShellScript(object server)
    {
        Collection<PSObject> psobjs = new Collection<PSObject>();
        string result = "";
        string serverName = server.ToString();

        string loginPassword = "xxx";
        System.Security.SecureString secpassword = new SecureString();
        foreach (char c in loginPassword)
        {
            secpassword.AppendChar(c);
        }

        PSCredential credential = new PSCredential(@"domain/samaccount", secpassword);
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://xxxxx/powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

        using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
        {
            PowerShell powershell = PowerShell.Create();

            if (runspace.RunspaceStateInfo.State == RunspaceState.Opened)
            {
                // do nothing
            }
            else
            {
                runspace.Open();
                powershell.Runspace = runspace;
            }

            try
            {
                PSCommand command = new PSCommand();
                command.AddScript($@"get-mailboxdatabase -Server " + server + " -Status");
                powershell.Commands = command;                    
                psobjs = powershell.Invoke();

                if (powershell.HadErrors == true)
                {
                    result = "Failed - " + powershell.Streams.Error[0].ToString();
                    result = result.Replace("\"", "*");
                }
            }
            catch (Exception ex)
            {
                string fail = ex.Message;
            }
        }
        object serverNameO = server;
        PSObject serverNameObj = new PSObject(serverNameO);
        psobjs.Insert(0, serverNameObj);

        return psobjs;
    }

最佳答案

这是有效的

WSManConnectionInfo wmc = new WSManConnectionInfo(new Uri(`"http://xxx/powershell`"));
wmc.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
wmc.ShellUri = `"http://schemas.microsoft.com/powershell/Microsoft.Exchange`";

using (Runspace runspace = RunspaceFactory.CreateRunspace(wmc))
{

}

关于c# 可以将登录凭据(非显式)与 wsmanconnectioninfo 一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52066794/

相关文章:

Powershell:需要从 PS 脚本创建 "active"ftp 传输连接

powershell - Azure cmdlet - 使用双因素身份验证时 session 无效

c# - WriteableBitmap访问冲突问题

c# - 无法将MVC 4部署到服务器

c# - SQL Server 中的 varchar 是否保留其编码?

powershell - 如何在 V2 中模拟 PowerShell V3 属性

active-directory - 用于检索 Exchange 通讯组列表的 Active Directory (LDAP) 查询/过滤器

c# - TimeZoneInfo - 为什么我似乎不能创建它的实例?

c# - 设置与房间相关的 ExtendedProperties

c# - 根据枚举从列表中获取元素