MSBuild calling Powershell with credentials

后端 未结 2 1204
闹比i
闹比i 2021-01-28 10:03

I\'m trying to deploy a windows service using an MSBuild script that runs a Powershell command.

The MSBuild script deploys the files I need and the PowerShell script wi

相关标签:
2条回答
  • 2021-01-28 10:17

    Use the code from 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)
    }
    

    Save the credentials first in a regular session with the same user on the same machine that will be running the builds. (Well, on each such machine and user profile.) Then, in the build script, Import-Credential from the same path and pass the new $cred to Invoke-Command.

    0 讨论(0)
  • 2021-01-28 10:23

    Maybe something like this?

    $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}" 
    
    0 讨论(0)
提交回复
热议问题