AzureVMPowerShell 批量创建VM

弓背霞明剑照霜,秋风走马出咸阳。这篇文章主要讲述AzureVMPowerShell 批量创建VM相关的知识,希望能为你提供帮助。
登录Azure China
Azure ChinaConnect-AzAccount -Environment AzureChinaCloud
?登录Azure Global
Azure GlobalConnect-AzAccount


指定订阅ID:
可在Azure Portal 订阅中查询


【AzureVMPowerShell 批量创建VM】查询Azure市场VM映像信息:
根据本次测试需要创建VM的镜像,通过以下命令查询镜像的相关信息(后续脚本中需要)。大家可以根据自己的需求,调整自己的参数。
使用 Azure PowerShell 查找并使用 Azure 市场 VM 映像    (参考文档:??https://docs.azure.cn/zh-cn/virtual-machines/windows/cli-ps-findimage??)

$locName="China North2"
Get-AzVMImagePublisher -Location $locName | Select PublisherName

$pubName="OpenLogic"
Get-AzVMImageOffer -Location $locName -PublisherName $pubName | Select Offer

$offerName="CentOS"
Get-AzVMImageSku -Location $locName -PublisherName $pubName -Offer $offerName |Select Skus

$skuName="7.5"
Get-AzVMImage -Location $locName -PublisherName $pubName -Offer $offerName -Sku $skuName | Select Version



根据要创建VM的环境信息创建CSV文件:



批量创建VM PowerShell脚本:
#导入csv文件
Import-Csv -Path C:\\Users\\vmconfig.csv |

#依次获取csv文件中定义的参数
ForEach-Object

#必要参数
$resourceGroup = $_.ResourceGroup
$vNetResourceGroup = $_.vNetResourceGroup
$vNetName = $_.vNetName
$subnetName = $_.subnet
$privateIp = $_.PrivateIp
$VMName = $_.VMName
$OSType = $_.VMOS
$VMSize = $_.VMSize
$location = $_.Location
$user = $_.UserName
$password = ConvertTo-SecureString -string $_.Password -AsPlainText -Force
$credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $user, $password

#可选参数
$storageName = $_.StorageAccountName
$availabilitySetName = $_.availabilitySet
$networkSecurityGroupName = $_.NetworkSecurityGroup
#$publicIp = $_.PublicIp
$imageResourceGroup = $_.imageresourceGroup
$imageName = $_.imageName

$nicName = $VMName + nic

#创建公共IP资源
#$pip = new-Azpublicipaddress -name $vip -resourcegroupname $resourceGroup -location $location -allocationmethod dynamic -Force -ipaddressversion ipv4

#获取虚拟网络和子网
$vnet = Get-Azvirtualnetwork -name $vNetName -resourcegroupname $vNetResourceGroup
$subnet = Get-AzVirtualNetworkSubnetConfig -Name $subnetName -virtualnetwork $vnet

#获取或创建网络安全组
$nsg = Get-AzNetworkSecurityGroup -ResourceGroupName $resourceGroup -Name $networkSecurityGroupName -ErrorAction Ignore
if($nsg -eq $null)

$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $resourceGroup -Location $location -Name $networkSecurityGroupName


#创建网络接口
#$nic = new-Aznetworkinterface -name $nicName -ResourceGroupName $resourceGroup -location $location -subnet $subnet -Force -Publicipaddress $pip -PrivateIpAddress $privateIp -NetworkSecurityGroup $nsg
$nic = new-Aznetworkinterface -name $nicName -ResourceGroupName $resourceGroup -location $location -subnet $subnet -Force -PrivateIpAddress $privateIp -NetworkSecurityGroup $nsg

#获取或创建可用性集
$aset = Get-AzAvailabilityset -ResourceGroupName $resourceGroup -Name $availabilitySetName -ErrorAction Ignore
if($aset -eq $null)

$aset = New-AzAvailabilityset -ResourceGroupName $resourceGroup -Name $availabilitySetName -Location $location -sku aligned -PlatformFaultDomainCount 2 -PlatformUpdateDomainCount 3


#创建虚拟机及磁盘配置
$vm = New-AzVMConfig -vmName $VMName -vmSize $VMSize -availabilitysetid $aset.id
$osDiskName = $VMName + osdisk
$dataDiskName = $VMName + datadisk
if ($OSType -eq "linuximage")

$vm = Set-AzVMSourceImage -

    推荐阅读