我尝试使用运行Powershell命令的MSBuild脚本部署Windows服务。
MSBuild脚本部署我需要的文件,PowerShell脚本将使用此命令卸载并重新安装Windows服务:
Invoke-Command -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' "
有没有正确的方法呢?
答案 0 :(得分:2)
使用Lee Holmes' article on exporting credentials中的代码:
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)
}
首先将凭据保存在与将运行构建的同一台计算机上的同一用户的常规会话中。 (好吧,在每个这样的机器和用户配置文件上。)然后,在构建脚本中,从同一路径导入 - 凭据并将新的$ cred传递给Invoke-Command。
答案 1 :(得分:0)
也许是这样的?
$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}"