title: Remote Access with Powershell and Jenkins
body: <p>I am trying to run a powershell script on a remote (Windows 2008 Server R2) machine. The following code works great when executed directly from powershell. (I.e. everything is set up correctly, WinRM services are running, Hosts trust each other, login is correct...)</p>
  
  <p>However, when I execute the exact same code from a Jenkins instance (running on the same machine where I tested) I get a <strong>PSSessionStateBroken</strong> connection failure, . (Not posting full error because it is in German on my machine.)</p>
  
  <p>I suppose that means Jenkins is using powershell differently or has different powershell/winrm settings or insufficient privileges. Any ideas?</p>
  
  <pre><code>$computer = "&lt;some ip&gt;"
  $user = "Administrator"
  $password = "&lt;secretpassword&gt;"
  $securepassword = ConvertTo-SecureString -String $password -AsPlainText -Force
  $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $securepassword
  Invoke-Command -ComputerName $computer -ScriptBlock { Get-ChildItem C:\ } -Credential $cred
  </code></pre>
  
  <p><strong>Edit:</strong> Managed to fix it by running jenkins service as Administrator. Works for me, but does not feel right...</p>