MSBuild calling Powershell with credentials

…衆ロ難τιáo~ 提交于 2019-12-02 12:22:35

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.

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}" 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!