title: Run Exchange Powershell commands from Linux using Ruby/WinRM
body: <p>I am attempting to run the enable-mailbox command for existing users in Active Directory from a ruby script. I'm using <a href="https://github.com/WinRb/WinRM" rel="nofollow">this winrm gem</a>. So far I have been able to connect to the exchange server using winrm and kerberos authentication. I can run an exchange management shell from powershell. From there I can execute exchange commands.</p>
  
  <p>However, when I try to run enable-mailbox I get the following error:</p>
  
  <blockquote>
    <p>Active Directory operation failed on . The supplied credential for
    'domain\account' is invalid.</p>
  </blockquote>
  
  <p>The 'operation failed on .' is verbatim. There is no text in the space where you would think there should be. The domain\account is the same one I'm using to successfully connect with winrm via kerberos.</p>
  
  <p>Here's my simple code:</p>
  
  <pre><code>endpoint = 'http://server:5985/wsman'
  krb5_realm = 'myrealm'
  winrm = WinRM::WinRMWebService.new(endpoint, :kerberos, :realm =&gt; krb5_realm)
  
  #exch_cmd = "Get-Help Enable-Mailbox" NOTE THAT THIS COMMAND WORKS FINE
  exch_cmd = "Enable-Mailbox -Identity:'user DN' -Alias:'username' -Database:'mailbox'"
  command = "powershell -psconsolefile \"C:\\Program Files\\Microsoft\\Exchange Server\\V15\\bin\\exshell.psc1\" -command \". "+exch_cmd+"\""
  
  winrm.cmd(command) do |stdout, stderr|
    STDOUT.print stdout
    STDERR.print stderr
  end
  </code></pre>
  
  <p>Thanks for any help!</p>