登录Azure China

Azure ChinaConnect-AzAccount -Environment AzureChinaCloud

登录Azure Global

Azure GlobalConnect-AzAccount


指定订阅ID:

可在Azure Portal 订阅中查询


查询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文件:

【Azure】【VM】PowerShell 批量创建VM_虚拟机


批量创建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 -VM $vm -publishername "OpenLogic" -offer "CentOS" -skus "7.5" -version "7.5.20180524"
$vm = Set-AzVMOperatingSystem -VM $vm -Linux -ComputerName $VMName -Credential $credential
$vm = Set-AzVMOSDisk -VM $vm -Name $osDiskName -StorageAccountType standard_lrs -DiskSizeInGB 30 -CreateOption FromImage
$vm = Add-AzVMDataDisk -VM $vm -Name $dataDiskName -StorageAccountType standard_lrs -DiskSizeInGB 10 -Lun 0 -CreateOption Empty
}
elseif ($OSType -eq "winserver2016r2")
{
$vm = Set-AzVMSourceImage -VM $vm -publishername "microsoftwindowsserver" -offer "Windowsserver" -skus "2016-Datacenter-zhcn" -version "latest"
$vm = Set-AzVMOperatingSystem -VM $vm -Windows -ComputerName $VMName -Credential $credential
$vm = Set-AzVMOSDisk -VM $vm -Name $osDiskName -StorageAccountType standard_lrs -DiskSizeInGB 127 -CreateOption FromImage
$vm = Add-AzVMDataDisk -VM $vm -Name $dataDiskName -StorageAccountType standard_lrs -DiskSizeInGB 10 -Lun 0 -CreateOption Empty
}
elseif ($OSType -eq "linuximage")
{
$vm = Set-AzVMSourceImage -VM $vm -publishername "OpenLogic" -offer "CentOS" -skus "7.5" -version "7.5.20180524"
$vm = Set-AzVMOperatingSystem -VM $vm -Linux -ComputerName $VMName -Credential $credential
$vm = Set-AzVMOSDisk -VM $vm -Name $osDiskName -StorageAccountType standard_lrs -DiskSizeInGB 30 -CreateOption FromImage
$vm = Add-AzVMDataDisk -VM $vm -Name $dataDiskName -StorageAccountType standard_lrs -DiskSizeInGB 10 -Lun 0 -CreateOption Empty
}

#为虚拟机绑定网络接口
$vm = Add-AzVMNetworkinterface -VM $vm -ID $nic.id


#部署虚拟机并返回结果相关信息
$rst = New-AzVM -VM $vm -ResourceGroupName $resourceGroup -Location $location
if ($rst.IsSuccessStatusCode -eq $true)
{
$vminfo = Get-AzVM -ResourceGroupName $resourceGroup -Name $VMName
Write-Host "VM deploy successed! INFO:"
$vminfo | Select-Object Name,ResourceGroupName,Location,
@{Name="AvailabilitySet";Expression={$_.AvailabilitySetReference.Id.split("/")[8]}},
@{Name="BootDiagnosticsStorageUri";Expression={$_.DiagnosticsProfile.BootDiagnostics.StorageUri}},
@{Name="VmSize";Expression={$_.HardwareProfile.VmSize}},
@{Name="OSType";Expression={$_.StorageProfile.OsDisk.OSType}},
@{Name="OSDiskSizeGB";Expression={$_.StorageProfile.OsDisk.DiskSizeGB}}
}
}


PowerShell脚本解释:


  • 根据自己csv存放的位置修改一下路径

【Azure】【VM】PowerShell 批量创建VM_Azure VM_02


  • 调整需要创建的VM信息(此处的内容就用到了上边查询的VM映像信息)

【Azure】【VM】PowerShell 批量创建VM_虚拟机_03

磁盘类型:Premium_LRS,StandardSSD_LRS,Standard_LRS


实验结果:

【Azure】【VM】PowerShell 批量创建VM_Azure VM_04

【Azure】【VM】PowerShell 批量创建VM_Azure VM_05

【Azure】【VM】PowerShell 批量创建VM_Azure VM_06

【Azure】【VM】PowerShell 批量创建VM_虚拟机_07