How to create a batch of VMs with PowerShell

本文介绍了一个使用PowerShell脚本在Azure环境中批量创建虚拟机的方法。通过定义存储帐户、订阅名称及虚拟机名称等参数,该脚本可以并行部署多个虚拟机,并为每个虚拟机配置管理员账户和密码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Foreword

When we do some test that need several VMs, we can use PowerShell script or CmdLets to implement what we want.

Keywords

PowerShell, New-AzureVMConfig, New-AzureProvisioningConfig, New-AzureVM, Get-AzureVM, Add-AzureDataDisk, Update-AzureVM

Summary

Detailed

# Name of subscription

$SubscriptionName = "CIETest01"

# Name of storage account (where VMs will be deployed)

$StorageAccount = "portalvhdstpb7xn5sd8cck"

$VmNames=New-Object System.Collections.ArrayList

$VmNames.Add("erictest001")

$VmNames.Add("erictest002")

function Provision-VM( [string]$VmName ) {

Start-Job -ArgumentList $VmName {

param($VmName)

$Location = "China North"

$InstanceSize = "Small" # You can use any other instance, such as Large, A6, and so on

$AdminUsername = "ericwen" # Write the name of the administrator account in the new VM

$Password = "Passw0rd" # Write the password of the administrator account in the new VM

$Image = "0c5c79005aae478e8883bf950a861ce0__Windows-Server-2012-Essentials-20131018-enus" #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"

echo "Attach new data disk to VM"

foreach($VmName in $VmNames){

Get-AzureVM -ServiceName $VmName -Name $VmName -Verbose | Add-AzureDataDisk -CreateNew -DiskSizeInGB 10 -DiskLabel "main" -LUN 1 -Verbose | Update-AzureVM -Verbose

}

Conclusion

Reference

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

转载于:https://www.cnblogs.com/ericwen/p/3714823.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值