【问题标题】:Double hopping credentials from VSTS through Azure PowerShell script to PowerShell script on target VM通过 Azure PowerShell 脚本从 VSTS 双跳凭证到目标 VM 上的 PowerShell 脚本
【发布时间】:2017-09-25 16:28:49
【问题描述】:

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

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

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

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

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

我确信有更好的方法来做到这一点。 命令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

【问题讨论】:

    标签: powershell azure azure-devops azure-powershell


    【解决方案1】:

    您实际上并没有双跳问题,因为您没有在节点上执行命令,而是在启动扩展程序,它会下载脚本并执行它。

    所以你需要做的是:

    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"并带参数执行

    【讨论】:

    • 您是否建议通过 Azure 自定义扩展从 VSTS 传递纯文本密码?我不认为这是个好主意。密码将在 C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.9\RuntimeSettings\0.settings 中以纯文本形式显示
    • 之后您将删除扩展名,所以这并不重要。那个地方会被清理干净。或者您可以使用受保护的设置,并且要执行的命令将被编码。 docs.microsoft.com/en-us/azure/virtual-machines/windows/…
    • 有什么方法可以通过 PowerShell 传递受保护的设置?如果没有,你会怎么做?将密码作为简单字符串传递是否安全?
    • 那么,这应该可以解决问题吗? Set-AzureRMVMCustomScriptExtension ... -Argument "-domainAdminName admin" -SecureExecution "-domainAdminPassword passw0rD" -VM $Vm ?
    猜你喜欢
    • 2021-02-18
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    相关资源
    最近更新 更多