欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用powershell 创建Azure VM虚机

程序员文章站 2022-03-09 12:24:13
...
Import-Csv -Path "D:\roobomtcn2\csv\vm\vm-logstash.csv" |foreach{
$location = $_.location
$vmName = $_.vmName
$vmSise = $_.size
$storageName = $_.storageName
$imageName = $_.image
$diskSizeInGB = $_.diskSizeInGB
$AvailabilitySetName = $_.aset
$dip = $_.dip
$subnetName = $_.subnetName
$rg = $_.resourceGroup
$vnetName = $_.vnetName
$vnetrg = $_.vnetrg
$diag=$_.diag
$dataVhdUri = $_.datadisks.Split(',')
$vmname = $vmName
 
#指定资源组
$resourceGroup = $rg
 
#查看Subnet的ID
$vnet = Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $vnetrg
$subnet01 = Get-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
 
#创建一个公网IP地址,地址分配方式可以为Dynamic 或者是Static
#$publicIP = New-AzureRmPublicIpAddress -Name hstestprem-ip -ResourceGroupName $resourceGroup -Location "China North" -AllocationMethod Dynamic -IpAddressVersion IPv4 –Force
 
#创建网卡,默认网卡的私网IP为动态分配,如需静态指定,可以在最后加上-PrivateIpAddress <IP>
if(($NIC = Get-AzureRmNetworkInterface -ResourceGroupName $rg -Name $vmName -ErrorAction SilentlyContinue) -eq $null){
$NIC = New-AzureRmNetworkInterface -Name $vmName -ResourceGroupName $rg -Location $location -SubnetId $subnet01.Id -PrivateIpAddress $dip
}
#配置要使用的存储账号以及系统盘名称
$storage = Get-AzureRmStorageAccount -Name $storageName -ResourceGroupName $rg
#可用性集
if(($aset = Get-AzureRmAvailabilitySet -ResourceGroupName $rg -Name $AvailabilitySetName -ErrorAction SilentlyContinue) -eq $null){
$aset = New-AzureRmAvailabilitySet -ResourceGroupName $rg -Name $AvailabilitySetName -Location $location
}
 
#生成虚拟机的配置
$vmconfig = New-AzureRmVMConfig -VMName $vmname -VMSize $vmSise
$osDiskUri = '{0}vhds/{1}.vhd' -f $storage.PrimaryEndpoints.Blob.ToString(),$imageName.ToLower()
$vm = Set-AzureRmVMOSDisk -vm $vmconfig –Name $OSDiskName -VhdUri $osDiskUri -CreateOption attach -Linux
$vm=Set-AzureRmVMBootDiagnostics -VM $vm -Enable -ResourceGroupName $rg -StorageAccountName $diag
$vm = Add-AzureRmVMNetworkInterface -vm $vm -Id $NIC.Id -Primary
#添加数据磁盘
$i = 0
foreach($data in $dataVhdUri){
$datauri="https://" + $storageName + ".blob.core.chinacloudapi.cn/vhds/" + $data + ".vhd"
$vm = Add-AzureRmVMDataDisk -CreateOption Attach -VM $vm -Name $data -Lun $i -VhdUri $datauri
$i++
}
 
#创建虚拟机
New-AzureRmVM -ResourceGroupName $rg -Location $location -VM $vm
}
相关标签: azure powershell