AuthenticationMechanism

AuthenticationMechanism

运行以下代码:

        SecureString password = new SecureString();
        string runasUsername = "USERNAME";
        string runasPassword = "PASSWORD";

        string liveIdconnectionUri = "http://EXCHANGE_SERVER/PowerShell";

        foreach (char x in runasPassword)
        {
            password.AppendChar(x);
        }

        PSCredential credential = new PSCredential(runasUsername, password);

        // Set the connection Info
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
        credential);

        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; //AuthenticationMechanism.Default;

        // create a runspace on a remote path
        // the returned instance must be of type RemoteRunspace

        Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);

        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();

        command.AddCommand("Enable-Mailbox");
        command.AddParameter("Identity", "first.last");
        command.AddParameter("Alias", "Fist Last");

        powershell.Commands = command;
        try
        {
            // open the remote runspace
            runspace.Open();
            // associate the runspace with powershell
            powershell.Runspace = runspace;
            // invoke the powershell to obtain the results
            var result = powershell.Invoke();
        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.Message);
        }
        finally
        {
            // dispose the runspace and enable garbage collection
            runspace.Dispose();
            runspace = null;
            // Finally dispose the powershell and set all variables to null to free
            // up any resources.
            powershell.Dispose();
            powershell = null;
        }

        Console.WriteLine("done");
        Console.Read();

抛出异常:



我已经设置了基本身份验证,允许未加密的流量。

我在这里尝试解决方案powershell v2 remoting - How do you enable unencrypted traffic,不走运。

最佳答案

抱歉,长时间苦苦挣扎,不断更改可能的组合,最后使用了以下方法:
AuthenticationMechanism应该是AuthenticationMechanism.Default,而不是AuthenticationMechanism.Basic(很奇怪)。

最终的工作版本是:

        SecureString password = new SecureString();
        string runasUsername = "USERNAME";
        string runasPassword = "PASSWORD";

        string liveIdconnectionUri = "http://EXCHANGE_SERVER/PowerShell";

        foreach (char x in runasPassword)
        {
            password.AppendChar(x);
        }

        PSCredential credential = new PSCredential(runasUsername, password);

        // Set the connection Info
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
        credential);

        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default; //AuthenticationMechanism.Default;

        // create a runspace on a remote path
        // the returned instance must be of type RemoteRunspace

        Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);

        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();

        command.AddCommand("Enable-Mailbox");
        command.AddParameter("Identity", "MAIL_USER_ID_HERE");

        powershell.Commands = command;
        try
        {
            // open the remote runspace
            runspace.Open();
            // associate the runspace with powershell
            powershell.Runspace = runspace;
            // invoke the powershell to obtain the results
            var result = powershell.Invoke();
            if (result.Count > 0)
                Console.WriteLine("sucessful!");
            else
                Console.WriteLine("failed!");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            // dispose the runspace and enable garbage collection
            runspace.Dispose();
            runspace = null;
            // Finally dispose the powershell and set all variables to null to free
            // up any resources.
            powershell.Dispose();
            powershell = null;
        }

        Console.WriteLine("done");
        Console.Read();

08-29 01:03