Powershell invoke-command with PSCredential in line

Posted by jaffa on Stack Overflow See other posts from Stack Overflow or by jaffa
Published on 2013-10-22T15:50:56Z Indexed on 2013/10/22 15:53 UTC
Read the original article Hit count: 293

Filed under:
|

I need to be able to run a command on another server. This script acts as a bootstrap to another script which is run on the actual server. This works great on servers on the same domain, but if I need to run this script on a remote server, I need to specify credentials.

The command is kicked off from a Msbuild targets file like so:

  <Target Name="PreDeployment" Condition="true" BeforeTargets="MSDeployPublish">

    <Exec Command="powershell.exe -ExecutionPolicy Bypass invoke-command bootstrapScript.ps1 -computername $(MyServer) -argumentlist param1, param2" />

  </Target>

However, I need to be able to supply the credentials by creating a new PSCredentials object with a secure password for my deployment script to run on a remote server:

<Target Name="PreDeployment" Condition="true" BeforeTargets="MSDeployPublish">          
        <Exec Command="powershell.exe -ExecutionPolicy Bypass invoke-command bootstrapScript.ps1 -computername $(MyServer) -credential New-Object System.Management.Automation.PSCredential ('admin', (convertto-securestring $(Password) -asplaintext -force)) -argumentlist param1, param2" />
  </Target>

When I run the build, a dialog pops up with the username set to System.Management.Automation.PSCredential. I need to be able to create the credentials in-line on the executable target. How do I accomplish this?

© Stack Overflow or respective owner

Related posts about powershell

Related posts about powershell-v2.0