【问题标题】:Powershell invoke-command with PSCredential in line带有 PSCredential 的 Powershell 调用命令
【发布时间】:2013-10-22 15:50:56
【问题描述】:

我需要能够在另一台服务器上运行命令。该脚本充当在实际服务器上运行的另一个脚本的引导程序。这在同一域上的服务器上效果很好,但如果我需要在远程服务器上运行此脚本,我需要指定凭据。

命令从 Msbuild 目标文件中启动,如下所示:

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

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

  </Target>

但是,我需要能够通过创建具有安全密码的新 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>

当我运行构建时,会弹出一个对话框,用户名设置为 System.Management.Automation.PSCredential。 我需要能够在可执行目标上创建内联凭据。我该如何做到这一点?

【问题讨论】:

    标签: powershell powershell-2.0


    【解决方案1】:

    尝试在 PSCredential 对象的实例化周围放置括号,例如:

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

    PowerShell 解析倾向于将参数解释为文字字符串(或数字)。如果要计算表达式,通常将表达式放在括号内。这在 PowerShell 中称为分组表达式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 2016-12-12
      • 2019-10-25
      • 1970-01-01
      相关资源
      最近更新 更多