AzureVMPowerShell 批量创建VM
Posted JasonWJ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AzureVMPowerShell 批量创建VM相关的知识,希望对你有一定的参考价值。
登录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文件:
批量创建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 -openstack 批量创建虚拟机微信微信小程序 微信开发工具中新创建的json文件,编译报错VM1781:2 pages/module/module.json 文件解析错误 SyntaxError: Unexpected e(代