2013-10-22 56 views
0

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

的命令是从的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的用户名。 我需要能够在可执行目标上联机创建凭据。我该如何做到这一点?

回答

2

尝试把周围的PSCredential的的实例化对象的括号例如: -

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

PowerShell的分析往往解释变量为文字字符串(或数字)。如果你想评估一个表达式,你通常会把表达式放在parens里面。这在PowerShell中被称为分组表达式。