Since I need to deploy, start, stop and remove many virtual machines created from a common image I created (you know, Tabular is not part of the standard images provided by Microsoft…), I wanted to minimize the time required to execute every operation from my Windows Azure PowerShell console (but I suggest you using Windows PowerShell ISE), so I also wanted to fire the commands as soon as possible in parallel, without losing the result in the console.

In order to execute multiple commands in parallel, I used the Start-Job cmdlet, and using Get-Job and Receive-Job I wait for job completion and display the messages generated during background command execution. This technique allows me to reduce execution time when I have to deploy, start, stop or remove virtual machines. Please note that a few operations on Azure acquire an exclusive lock and cannot be really executed in parallel, but only one part of their execution time is subject to this lock. Thus, you obtain a better response time also in these scenarios (this is the case of the provisioning of a new VM).

Finally, when you remove the VMs you still have the disk containing the virtual machine to remove. This cannot be done just after the VM removal, because you have to wait that the removal operation is completed on Azure. So I wrote a script that you have to run a few minutes after VMs removal and delete disks (and VHD) no longer related to a VM. I just check that the disk were associated to the original image name used to provision the VMs (so I don’t remove other disks deployed by other batches that I might want to preserve).

These examples are specific for my scenario, if you need more complex configurations you have to change and adapt the code. But if your need is to create multiple instances of the same VM running in a workgroup, these scripts should be good enough.

I prepared the following PowerShell scripts:

  • ProvisionVMs: Provision many VMs in parallel starting from the same image. It creates one service for each VM.
  • RemoveVMs: Remove all the VMs in parallel – it also remove the service created for the VM
  • StartVMs: Starts all the VMs in parallel
  • StopVMs: Stops all the VMs in parallel
  • RemoveOrphanDisks: Remove all the disks no longer used by any VMs. Run this script a few minutes after RemoveVMs script.
 

ProvisionVMs
# Name of subscription 
 
$SubscriptionName = "Copy the SubscriptionName property you get from Get-AzureSubscription"
 
$VmNames=New-Object System.Collections.ArrayList 
     $VmNames.Add("erictest001")
     $VmNames.Add("erictest002")
 
# Name of storage account (where VMs will be deployed)
 
$StorageAccount = "Copy the Label property you get from Get-AzureStorageAccount"
 
function Provision-VM( [string]$VmName ) {
 
Start-Job -ArgumentList $VmName {
 
param($VmName)
 
$Location = "Copy the Location property you get from Get-AzureStorageAccount"
 
$InstanceSize = "A5" # You can use any other instance, such as Large, A6, and so on
 
$AdminUsername = "UserName" # Write the name of the administrator account in the new VM
 
$Password = "Password" # Write the password of the administrator account in the new VM
 
$Image = "Copy the ImageName property you get from Get-AzureVMImage"
 
# You can list your own images using the following command:
 
# Get-AzureVMImage | Where-Object {$_.PublisherName -eq "User" }
 
New-AzureVMConfig -Name $VmName -ImageName $Image -InstanceSize $InstanceSize |
 
Add-AzureProvisioningConfig -Windows -Password $Password -AdminUsername $AdminUsername|
 
New-AzureVM -Location $Location -ServiceName "$VmName" -Verbose
 
}
 
}
 
# Set the proper storage - you might remove this line if you have only one storage in the subscription
 
Set-AzureSubscription -SubscriptionName $SubscriptionName -CurrentStorageAccount $StorageAccount
 
# Select the subscription - this line is fundamental if you have access to multiple subscription
 
# You might remove this line if you have only one subscription
 
Select-AzureSubscription -SubscriptionName $SubscriptionName
 
# Every line in the following list provisions one VM using the name specified in the argument
 
# You can change the number of lines - use a unique name for every VM - don't reuse names
 
# already used in other VMs already deployed
 
foreach($VmName in $VmNames)
     {
       Provision-VM $VmName
     }
 
 
# Wait for all to complete
 
While (Get-Job -State "Running") { 
 
Get-Job -State "Completed" | Receive-Job
 
Start-Sleep 1
 
}
 
# Display output from all jobs
 
Get-Job | Receive-Job
 
# Cleanup of jobs
 
Remove-Job *
 
# Displays batch completed
 
echo "Provisioning VM Completed"
 
RemoveVMs
# Name of subscription 
 
$SubscriptionName = "Copy the SubscriptionName property you get from Get-AzureSubscription"
 
function Remove-VM( [string]$VmName ) {
 
Start-Job -ArgumentList $VmName {
 
param($VmName)
 
Remove-AzureService -ServiceName $VmName -Force -Verbose
 
}
 
}
 
# Select the subscription - this line is fundamental if you have access to multiple subscription
 
# You might remove this line if you have only one subscription
 
Select-AzureSubscription -SubscriptionName $SubscriptionName
 
# Every line in the following list remove one VM using the name specified in the argument
 
# You can change the number of lines - use a unique name for every VM - don't reuse names
 
# already used in other VMs already deployed
 
foreach($VmName in $VmNames)
     {
       Remove-VM $VmName
     }
 
# Wait for all to complete
 
While (Get-Job -State "Running") { 
 
Get-Job -State "Completed" | Receive-Job
 
Start-Sleep 1
 
}
 
# Display output from all jobs
 
Get-Job | Receive-Job
 
# Cleanup
 
Remove-Job *
 
# Displays batch completed
 
echo "Remove VM Completed"
 
StartVMs
# Name of subscription 
 
$SubscriptionName = "Copy the SubscriptionName property you get from Get-AzureSubscription"
 
function Start-VM( [string]$VmName ) {
 
Start-Job -ArgumentList $VmName {
 
param($VmName)
 
Start-AzureVM -Name $VmName -ServiceName $VmName -Verbose
 
}
 
}
 
# Select the subscription - this line is fundamental if you have access to multiple subscription
 
# You might remove this line if you have only one subscription
 
Select-AzureSubscription -SubscriptionName $SubscriptionName
 
# Every line in the following list starts one VM using the name specified in the argument
 
# You can change the number of lines - use a unique name for every VM - don't reuse names
 
# already used in other VMs already deployed
 
foreach($VmName in $VmNames)
     {
       Start-VM $VmName
     }
# Wait for all to complete
 
While (Get-Job -State "Running") { 
 
Get-Job -State "Completed" | Receive-Job
 
Start-Sleep 1
 
}
 
# Display output from all jobs
 
Get-Job | Receive-Job
 
# Cleanup
 
Remove-Job *
 
# Displays batch completed
 
echo "Start VM Completed" 
 
StopVMs
# Name of subscription 
 
$SubscriptionName = "Copy the SubscriptionName property you get from Get-AzureSubscription"
 
function Stop-VM( [string]$VmName ) {
 
Start-Job -ArgumentList $VmName {
 
param($VmName)
 
Stop-AzureVM -Name $VmName -ServiceName $VmName -Verbose -Force
 
}
 
}
 
# Select the subscription - this line is fundamental if you have access to multiple subscription
 
# You might remove this line if you have only one subscription
 
Select-AzureSubscription -SubscriptionName $SubscriptionName
 
# Every line in the following list stops one VM using the name specified in the argument
 
# You can change the number of lines - use a unique name for every VM - don't reuse names
 
# already used in other VMs already deployed
 
foreach($VmName in $VmNames)
     {
       Stop-VM $VmName
     }
# Wait for all to complete
 
While (Get-Job -State "Running") { 
 
Get-Job -State "Completed" | Receive-Job
 
Start-Sleep 1
 
}
 
# Display output from all jobs
 
Get-Job | Receive-Job
 
# Cleanup
 
Remove-Job *
 
# Displays batch completed
 
echo "Stop VM Completed"
 
RemoveOrphanDisks
$Image = "Copy the ImageName property you get from Get-AzureVMImage"
 
# You can list your own images using the following command:
 
# Get-AzureVMImage | Where-Object {$_.PublisherName -eq "User" }
 
# Remove all orphan disks coming from the image specified in $ImageName
 
Get-AzureDisk |
 
Where-Object {$_.attachedto -eq $null -and $_.SourceImageName -eq $ImageName} |
 
Remove-AzureDisk -DeleteVHD -Verbose 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Refference:http://sqlblog.com/blogs/marco_russo/archive/2013/10/29/powershell-script-to-deploy-multiple-vm-on-azure-in-parallel-azure-powershell.aspx

PowerShell Script to Deploy Multiple VM on Azure in Parallel #azure #powershell的更多相关文章

  1. SharePoint 2013 How to Backup Site Collection Automatically With a PowerShell Script

    In this post I will introduce a way how to run a script for backing up SharePoint data which could b ...

  2. 【Azure 应用服务】Azure Function App 执行PowerShell指令[Get-Azsubscription -TenantId $tenantID -DefaultProfile $cxt]错误

    问题描述 使用PowerShell脚本执行获取Azure订阅列表的指令(Get-Azsubscription -TenantId $tenantID -DefaultProfile $cxt).在本地 ...

  3. [New Portal]Windows Azure Virtual Machine (21) 将本地Hyper-V的VM上传至Windows Azure Virtual Machine

    <Windows Azure Platform 系列文章目录> 本章介绍的内容是将本地Hyper-V的VHD,上传到Azure数据中心,并且保留OS中的内容. 注意:笔者没有执行Syspr ...

  4. Azure 基础:使用 powershell 创建虚拟机

    在进行与 azure 相关的自动化过程中,创建虚拟主机是避不开的操作.由于系统本身的复杂性,很难用一两条简单的命令完成虚拟主机的创建.所以专门写一篇文章来记录使用 PowerShell 在 azure ...

  5. 使用 Azure PowerShell 将 IaaS 资源从经典部署模型迁移到 Azure Resource Manager

    以下步骤演示了如何使用 Azure PowerShell 命令将基础结构即服务 (IaaS) 资源从经典部署模型迁移到 Azure Resource Manager 部署模型. 也可根据需要通过 Az ...

  6. Azure资源管理工具Azure PowerShell介绍

    什么是 Azure PowerShell? Azure PowerShell 是一组模块,提供用于通过 Windows PowerShell 管理 Azure 的 cmdlet.你可以使用 cmdle ...

  7. 【Azure 应用服务】使用PowerShell脚本上传文件至App Service目录  

    问题描述 使用PowerShell脚本上传文件至App Service目录的示例 脚本示例 对文件进行上传,使用的 WebClient.UploadFile 方法进行上传.当文件夹中包含子目录,执行以 ...

  8. 【Azure 环境】用 PowerShell 调用 AAD Token, 以及调用Azure REST API(如资源组列表)

    问题描述 PowerShell 脚本调用Azure REST API, 但是所有的API都需要进行权限验证.要在请求的Header部分带上Authorization参数,并用来对List Resour ...

  9. Send email alert from Performance Monitor using PowerShell script (检测windows服务器的cpu 硬盘 服务等性能,发email的方法) -摘自网络

    I have created an alert in Performance Monitor (Windows Server 2008 R2) that should be triggered whe ...

随机推荐

  1. 继续说一下openjson 以及 json path 的使用 (2)

    在openjson 里面,其实是可以把数据类型array里面的值遍历出来的,举个栗子 ) = N' {"name":"test", "obj" ...

  2. MongoDB学习——基础入门

    MongoDB--基础入门 MongoDB是目前比较流行的一种非关系型数据库(NoSql),他的优势这里不废话,我们关注怎么使用它. 安装 下载,首先肯定要去下载,我们去官网下载,在国内,可能没FQ可 ...

  3. Java调优

    Java调优经验谈 对于调优这个事情来说,一般就是三个过程: 性能监控:问题没有发生,你并不知道你需要调优什么?此时需要一些系统.应用的监控工具来发现问题. 性能分析:问题已经发生,但是你并不知道问题 ...

  4. JAVA构造函数(方法)与方法是啥意思

    成员方法必须有返回类型即使是没有返回,也要写上void 构造函数(方法)没有返回类型,而且和类名一样!一个类里面,一看就知道了譬如:public class Test{public Test(){} ...

  5. SQLServer中比较末尾带有空格的字符串遇到的坑

    最近发现SQLServer中比较字符串的时候 如果字符串末尾是空格 那么SQLServer会无视那些空格直接进行比较 这和程序中平时的字符串判断逻辑不统一 );set @a=N'happycat198 ...

  6. Virtualbox配置双网卡

    hadoop内部的虚拟机群,使用Host-Only 因为我之前一直是把三台虚机配置成桥接网络,可以同时上网又可以互通,但有一段时间,网络一直不通畅,造成hadoop核心进程一直关闭. 最后为了稳定起见 ...

  7. 我了个大擦-PDO(二)

    hi 昨天又213了,虽然有室友3点多才睡觉的客观影响,但是昨晚不想学东西是本质原因.今天搞起.打算3.4天之内,学完PDO和AJAX这两个,还望大家没事儿来骂骂我,免的我又偷懒. 1.PDO 二.P ...

  8. Effective Java 读书笔记

    创建和销毁对象 >考虑用静态工厂方法替代构造器. 优点: ●优势在于有名称. ●不必再每次调用他们的时候都创建一个新的对象. ●可以返回原返回类型的任何子类型的对象. ●在创建参数化类型实例的时 ...

  9. UVA 10375 Choose and divide【唯一分解定理】

    题意:求C(p,q)/C(r,s),4个数均小于10000,答案不大于10^8 思路:根据答案的范围猜测,不需要使用高精度.根据唯一分解定理,每一个数都可以分解成若干素数相乘.先求出10000以内的所 ...

  10. 贪吃蛇(C++实现,VC6.0编译,使用了EasyX图形库)

    程序效果: 代码: //main.cpp 1 #include <iostream> #include<fstream> #include <graphics.h> ...