【发布时间】:2017-09-25 16:28:49
【问题描述】:
作为域管理员,我需要在目标 VM 上自动执行脚本。问题是,虚拟机没有公共空间。我也不应该重写脚本,因为它是由团队成员编写的,我宁愿为我的团队提供适用于他们且可自动化的解决方案,而不是每次都重写他们的脚本。 当前进程看起来像这样
- VSTS 使用 Azure PowerShell 脚本 command1.ps1 启动构建过程
- Command1.ps1 在目标 VM 上安装 Azure 自定义脚本扩展
- 自定义脚本扩展下载并执行以域管理员身份运行 command3.ps1 的 command2.ps1
我遇到的问题是我无法将凭据从 VSTS 传递到 command2.ps1 请推荐我应该如何正确地做到这一点。 我找到的选项:
不确定是否可以使用 VSTS https://blogs.technet.microsoft.com/ashleymcglone/2016/08/30/powershell-remoting-kerberos-double-hop-solved-securely/
给目标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