MSBuild与凭据调用Powershell

问题描述:

我想部署一个Windows服务使用MSBuild脚本运行Powershell命令。MSBuild与凭据调用Powershell

的MSBuild的脚本部署文件,我需要和PowerShell脚本将卸载,并使用此命令重新安装Windows服务:

调用命令-ComputerName IPAddressHere -FilePath“C:\ theScriptFileName.ps1” -credential“ TheUserName“

使用IP地址(我需要因为不同的域)我需要使用凭据。问题是它会提示输入密码,这对TeamCity的自动化不起作用。

我知道我可以保存凭据到一个变量,以便及时将无法显示,但我需要得到它成一条线像下面那的MSBuild可以执行:

powershell.exe -nonInteractive -executionpolicy Unrestricted -command“& Invoke-Command -ComputerName IPAddressHere -FilePath'C:\ theScriptFileName.ps1'”

有没有适当的方法来做到这一点?

也许这样?第一

function Export-Credential($cred, $path) { 
    $cred.Password = $cred.Password | ConvertFrom-SecureString 
    $cred | Export-Clixml $path 
} 
function Import-Credential($path) { 
    $cred = Import-Clixml $path 
    $cred.password = $cred.Password | ConvertTo-SecureString 
    New-Object System.Management.Automation.PSCredential($cred.username, $cred.password) 
} 

保存凭据常会与将要运行建立在同一台机器上相同的用户:

$Creds = $host.ui.PromptForCredential("Need credentials", "Please enter username/password with proper rights on objects to manage.`r`n`r`nExample: AD-Domain\username", $env:userdomain + "\" + $env:username, "") 
$IPAddressHere = "192.168.0.1" 
powershell.exe -NonInteractive -executionpolicy Unrestricted -command "& {Invoke-Command -ComputerName $IPAddressHere -FilePath 'C:\theScriptFileName.ps1' -credentials $creds}" 

Lee Holmes' article on exporting credentials使用的代码。 (那么,在每个这样的机器和用户配置文件上。)然后,在构建脚本中,从相同路径导入Credential并将新的$ cred传递给Invoke-Command。