【问题标题】:MSBuild calling Powershell with credentialsMSBuild 使用凭据调用 Powershell
【发布时间】:2014-12-08 06:18:51
【问题描述】:

我正在尝试使用运行 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' "

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

【问题讨论】:

    标签: powershell msbuild teamcity credentials


    【解决方案1】:

    使用来自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)
    }
    

    在与将运行构建的同一台计算机上的同一用户进行的常规会话中首先保存凭据。 (好吧,在每台这样的机器和用户配置文件上。)然后,在构建脚本中,从同一路径导入-Credential 并将新的 $cred 传递给 Invoke-Command。

    【讨论】:

      【解决方案2】:

      也许是这样的?

      $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}" 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-24
        • 2017-10-16
        相关资源
        最近更新 更多