[英]How to run Start-AzureVM in parallel from a VSTS Build in Azure Powershell?
I have a PowerShell script running in an Azure Powershell task in VSTS, that starts several Azure virtual machines in a DevTest lab, but starts the domain controller first, waits for it to start, and then starts the others one after the other. 我有一个在VSTS中的Azure Powershell任务中运行的PowerShell脚本,该脚本在DevTest实验室中启动了多个Azure虚拟机,但先启动了域控制器,等待其启动,然后依次启动另一个。
I'd like to start all the others in parallel and I tried to do this using Start-Job
but that spawns a new Powershell process which doesn't have the security context of the Azure login, so it fails. 我想并行启动所有其他应用程序,并尝试使用Start-Job
进行此操作,但这产生了一个新的Powershell进程,该进程没有Azure登录的安全上下文,因此失败。 I'm trying something like this: 我正在尝试这样的事情:
[cmdletbinding()]
param (
[ValidateSet("Start","Stop")][string]$Action,
$labName = "DevTestLab",
$labResourceGroup = "DevTestLabRG"
)
if ($Action -eq "Start") {
Write-Verbose "Starting the domain controller first"
Get-AzureRmResource | Where-Object {
$_.Name -match "dc" -and
$_.ResourceType -eq "Microsoft.Compute/virtualMachines" -and
$_.ResourceGroupName -match $labResourceGroup } | Start-AzureRmVM
Write-Verbose "Starting other machines in the lab as background jobs"
foreach ($AzureRMResource in Get-AzureRmResource |
Where-Object {
$_.Name -notmatch "dc" -and
$_.ResourceType -eq "Microsoft.Compute/virtualMachines" -and
$_.ResourceGroupName -match $labResourceGroup } )
{
Start-Job {
$myResource = $using:AzureRMResource
Start-AzureRMVM -Name $myResource.Name -ResourceGroupName $myResource.ResourceGroupName
}
}
# wait for all machines to start before exiting the session
Get-Job | Wait-Job
Get-Job | Remove-Job
}
I am using a Hosted Agent to run the script, so I can't have many agents running at once, and VSTS doesn't support parallel tasks as far as I am aware. 我使用的是托管代理来运行脚本,因此我无法一次运行很多代理,据我所知,VSTS不支持并行任务。
Any ideas on how to solve this? 关于如何解决这个问题的任何想法?
Instead of creating a PowerShell script you can also use the Azure CLI. 除了创建PowerShell脚本外,还可以使用Azure CLI。 The Azure CLI has a vm start
and vm stop
command that takes a list of ids. Azure CLI具有一个使用ID列表的vm start
和vm stop
命令。 You can also use the CLI to query for all the ids you want. 您也可以使用CLI查询所需的所有ID。 The following is a simple Bash snippet that starts/stops all vms where the id contains Test . 以下是一个简单的Bash代码段,用于启动/停止ID包含Test的所有vm。
# example usage
az vm start --ids $(
az vm list --query "[].id"
-o tsv | grep "Test"
)
az vm stop --ids $(
az vm list --query "[].id"
-o tsv | grep "Test"
)
Source: Azure CLI 2.0: Quickly Start / Stop ALL VMs 来源: Azure CLI 2.0:快速启动/停止所有虚拟机
您还可以通过Azure CLI任务使用带有--no-wait
参数的z vm start命令尝试使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.