简体   繁体   English

如何从Azure Powershell中的VSTS构建并行运行Start-AzureVM?

[英]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 startvm 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.

相关问题 如何通过从另一个Powershell文件中调用来并行运行Powershell文件? - How to run a powershell files in parallel by calling it from another powershell file? 如何从 PowerShell 停止/启动 Azure Function - How to stop/start Azure Function from PowerShell 如何在VSTS上运行SharePoint Online PowerShell脚本 - How to Run SharePoint Online PowerShell scripts on VSTS Azure Powershell-get-AzureVM返回accountName不存在错误? - Azure Powershell - get-AzureVM returning an accountName does not exist error? Azure资源锁-被Powershell Remove-AzureVM绕过 - Azure Resource Lock - Bypassed by Powershell Remove-AzureVM Azure 自动化 - 图形 PowerShell 脚本并行运行? - Azure automation - Graphical PowerShell script run parallel? 在Mac上安装的VSTS代理中运行Powershell构建步骤? - Run powershell build step in VSTS agent installed on mac? 如何从 powershell 运行批处理“开始” - How to run batch “Start” from powershell 如何从 Azure 数据工厂运行 PowerShell - How to run PowerShell from Azure Data Factory 如何在 PowerShell 中并行运行命令 - How to run commands in parallel in PowerShell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM