title: Establish WSManConnection from Azure Worker Role
body: <p>We are attempting to automate a large number of our azure/service maintenance tasks using a combination of Azure Service Bus queues and Azure Worker Roles. In short, the concept is as follows....</p>
  
  <ol>
  <li>Maintenance task is posted to SB Queue</li>
  <li>Worker role listens for tasks on the SB Queue</li>
  <li>Worker role connects to desired VM/Web Role/Cloud service and executes a remote powershell command</li>
  </ol>
  
  <p>In practice, this works as expected when operating within a development environment, however after the worker role is published, the remote powershell connection fails with the response "Access is denied". The code to establish the connection is as follows...</p>
  
  <pre><code>PSCredential cred = new PSCredential(config.Username, config.PrimaryPassword);
  WSManConnectionInfo connection = new WSManConnectionInfo(true, config.PrimaryServer, 5986, "/wsman", "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", cred);
  
  using (Runspace runspace = RunspaceFactory.CreateRunspace(connection))
  {
      runspace.Open();
      using (PowerShell shell = PowerShell.Create())
      {
          shell.Runspace = runspace;
          // DO SOMETHING HERE
          shell.Invoke();
      }
      runspace.Close();
  }
  </code></pre>
  
  <p>Initially, I had suspected that this was a CA certificate issue, however I have since connected to the worker role via RDP and confirmed that the certificates are being deployed correctly. In addition I have also managed to acheive a connection to the target server via a "winrs -r:" command also using the remote desktop connection.</p>
  
  <p>As confirmation, the worker role is also running with elevated permissions.</p>
  
  <p>Any help with this would be much appreciated</p>
  
  <p>Thanks in advance</p>