从 VSTS 通过 Azure PowerShell 脚本将凭据双跳到目标 VM 上的 PowerShell 脚本

Double hopping credentials from VSTS through Azure PowerShell script to PowerShell script on target VM

我需要以域管理员的身份在目标 VM 上自动执行脚本。问题是,虚拟机没有 public。我也不应该重写脚本,因为它是由团队成员编写的,我宁愿为我的团队提供适用于他们且可自动化的解决方案,而不是每次都重写他们的脚本。 当前进程看起来像thi

  1. VSTS 使用 Azure PowerShell 脚本命令 1 启动构建过程。ps1
  2. Command1.ps1 在目标 VM 上安装 Azure 自定义脚本扩展
  3. 自定义脚本扩展下载并执行 command2。ps1 以域管理员身份运行 command3。ps1

我遇到的问题是我无法将凭据从 VSTS 传递到 command2。ps1 请向我推荐我应该如何正确地做到这一点。 我找到的选项:

  1. 不确定 VSTS 是否可行 https://blogs.technet.microsoft.com/ashleymcglone/2016/08/30/powershell-remoting-kerberos-double-hop-solved-securely/

  2. 给目标VM添加IPpublic地址,配置WinRM,执行command2.ps1,删除publicIP地址

我相信有更好的方法来做到这一点。 命令 1.ps1:

    param
(
    [Parameter(Mandatory)]
    [String]$resourceGroupName,

    [Parameter(Mandatory)]
    [String]$targetVMname,

    [Parameter(Mandatory)]
    [String]$vmLocation,

    [Parameter(Mandatory)]
    [String]$FileUri,

    [Parameter(Mandatory)]
    [String]$nameOfTheScriptToRun,

    [Parameter(Mandatory)]
    [String]$customScriptExtensionName,

    [Parameter(Mandatory)]
    [String]$domainAdminName,

    [Parameter(Mandatory)]
    [String]$domainAdminPassword

)

$domainAdminPasswordSecureString = ConvertTo-SecureString -String $domainAdminPassword -AsPlainText -Force
$DomainCredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $domainAdminName, $domainAdminPasswordSecureString

Set-AzureRmVMCustomScriptExtension -Argument "-DomainCredentials $DomainCredentials" `
    -ResourceGroupName $resourceGroupName `
    -VMName $targetVMname `
    -Location $vmLocation `
    -FileUri $FileUri `
    -Run $nameOfTheScriptToRun `
    -Name $customScriptExtensionName

Remove-AzureRmVMCustomScriptExtension -Force `
    -ResourceGroupName $resourceGroupName `
    -VMName $targetVMname `
    -Name $customScriptExtensionName

command2.ps1:

    param
(
    [Parameter(Mandatory)]
    [System.Management.Automation.PSCredential]$DomainCredentials
)

$url = "https://raw.githubusercontent.com/x/command2.ps1"
$output = "C:\command2.ps1"

Invoke-WebRequest -Uri $url -OutFile $output
Start-Process -FilePath powershell.exe -ArgumentList $output -Credential $DomainCredentials

你实际上没有双跳问题,因为你没有在节点上执行命令,你正在启动扩展,它下载脚本并执行它。

所以你需要做的是:

Set-AzureRMVMCustomScriptExtension ... -Argument "-domainAdminName admin -domainAdminPassword passw0rD" -VM $Vm
$vm | Update-AzureVM

并在您的脚本中(在机器内部调用,因此 command2.ps1)执行:

$domainAdminPasswordSecureString = ConvertTo-SecureString -String $domainAdminPassword -AsPlainText -Force
$DomainCredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $domainAdminName, $domainAdminPasswordSecureString

并将适当的参数粘贴到第二个脚本中(因此它接受那些)

此外,您不需要中间脚本,您可以直接下载"https://raw.githubusercontent.com/x/command2.ps1"并带参数执行