简体   繁体   English

符合PSCredential的Powershell invoke-command

[英]Powershell invoke-command with PSCredential in line

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: 该命令从Msbuild目标文件开始,如下所示:

  <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: 但是,我需要能够通过使用安全密码创建一个新的PSCredentials对象来提供凭据,以使我的部署脚本可以在远程服务器上运行:

<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. 运行构建时,将弹出一个对话框,其用户名设置为System.Management.Automation.PSCredential。 I need to be able to create the credentials in-line on the executable target. 我需要能够在可执行目标上在线创建凭据。 How do I accomplish this? 我该如何完成?

Try putting parens around the instantiation of the PSCredential object eg: 尝试在PSCredential对象的实例化周围放置括号,例如:

... -credential (New-Object System.Management.Automation.PSCredential 'admin',(convertto-securestring $(Password) -asplaintext -force)) -argumentlist param1, param2" />

PowerShell parsing tends to interpret arguments as literal strings (or numbers). PowerShell解析倾向于将参数解释为文字字符串(或数字)。 If you want to evaluate an expression you usually put the expression inside parens. 如果要评估表达式,通常将表达式放在括号内。 This is known as a grouping expression in PowerShell. 这在PowerShell中称为分组表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM