我正在尝试使用运行 Powershell 命令的 MSBuild 脚本来部署 Windows 服务。
MSBuild 脚本部署我需要的文件,PowerShell 脚本将使用以下命令卸载并重新安装 Windows 服务:
Invoke-Command -ComputerName IPAddressHere -FilePath "C:\theScriptFileName.ps1" -credential "TheUserName"
使用 IP 地址(由于域不同,我需要使用 IP 地址)我需要使用凭据。问题是它会提示输入密码,这对于 TeamCity 的自动化不起作用。
我知道我可以将凭据保存到一个变量中,这样提示就不会显示,但我需要将其放入 MSBuild 可以执行的类似于以下内容的行中:
powershell.exe -NonInteractive -executionpolicy Unrestricted -command "& Invoke-Command -ComputerName IPAddressHere -FilePath 'C:\theScriptFileName.ps1' "
有没有正确的方法来做到这一点?
使用来自的代码Lee Holmes 关于导出凭证的文章 http://www.leeholmes.com/blog/2008/06/04/importing-and-exporting-credentials-in-powershell/:
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)
}
首先在将运行构建的同一台计算机上与同一用户的常规会话中保存凭据。 (嗯,在每台这样的计算机和用户配置文件上。)然后,在构建脚本中,从同一路径导入 Credential 并将新的 $cred 传递给 Invoke-Command。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)