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

使用powershell复制Azure虚机

程序员文章站 2022-03-09 12:21:25
...
#复制一台虚拟机到新的存储账号并完全恢复虚拟机(包含磁盘、终结点、ACL)
########请将下列参数填写完整#######
#Azure订阅ID
$subID = "1abd0887-7807-55e1-8475-bbd974c911da"
#被复制虚拟机名称
$oldvmName = "whywrite"
#被复制虚拟机所在云服务
$oldserviceName = "whywrite"
#新虚拟机名称
$newvmName = "whywrit3"
#新虚拟机将被放置在哪个云服务
$newserviceName = "whywrit3"
#新虚拟机存放在哪个存储账号
$newStor = "whywrit3"
#新虚拟机所在的虚拟网络
$newVnet = "fltrpVnet"
#新虚拟机所在的子网a
$newSubnet = "Subnet-3"
#新虚拟机将被绑定哪个内网IP
$newDip = "155.20.2.188"
 
#######################脚本部分############################
$newStorKey = (Get-AzureStorageKey -StorageAccountName $newStor ).Primary
$vm = Get-AzureVM -ServiceName $oldserviceName -Name $oldvmName
$vmSize = $vm.InstanceSize
$vmAS = $vm.AvailabilitySetName $vmAS = $newserviceName
$osDiskName = (Get-AzureOSDisk -VM $vm).DiskName
$osType = (Get-AzureOSDisk -VM $vm).OS
$osvhdUri = (Get-AzureOSDisk -VM $vm).MediaLink.AbsoluteUri
$osvhdName = (Get-AzureOSDisk -VM $vm).MediaLink.Segments[2]
$datadisks = Get-AzureDataDisk -VM $vm
$endpoints = Get-AzureEndpoint -VM $vm
$context = New-AzureStorageContext -StorageAccountName $newStor -StorageAccountKey $newStorKey -Endpoint "core.chinacloudapi.cn"
"拷贝VHD文件到新的存储账户"
Set-AzureSubscription -SubscriptionId $subID -CurrentStorageAccountName $newStor
"Copy OS VHD"
Start-AzureStorageBlobCopy -SrcUri $osvhdUri -DestContainer vhds –DestBlob ($newvmName +"-"+$osvhdName) -DestContext $context
sleep 3
"Copy data VHD"
foreach($datadisk in $datadisks){
"Copy "+$datadisk.MediaLink.Segments[2]
Start-AzureStorageBlobCopy -SrcUri $datadisk.MediaLink.AbsoluteUri -DestContainer vhds -DestBlob ($newvmName +"-"+$datadisk.MediaLink.Segments[2]) -DestContext $context
sleep 3
}
 
"验证传输结果:"
for($i = 1; $i -le 200 ; $i++){
"第"+$i+"次检查传输结果:"
$buloon = "true"
$oscopystats = Get-AzureStorageBlobCopyState -Blob ($newvmName +"-"+$osvhdName) -Container vhds
if( $oscopystats.Status -eq "Pending"){
$buloon = "false"
"验证OS盘传输结果:"
$newvmName +"-"+$osvhdName+"传输未结束,已传输:" + $oscopystats.BytesCopied/$oscopystats.TotalBytes
}elseif($oscopystats.Status -eq "Failed"){
$buloon = "false"
"验证OS盘传输结果:"
$newvmName +"-"+$osvhdName+"传输失败,报错信息:" + $oscopystats.StatusDescription
}
 
 
if($buloon -eq "true"){
foreach($datadisk in $datadisks){
 
$vhdcopystats = Get-AzureStorageBlobCopyState -Blob ($newvmName +"-"+$datadisk.MediaLink.Segments[2]) -Container vhds
if( $vhdcopystats.Status -eq "Pending"){
$buloon = "false"
"验证Data盘传输结果:"
$datadisk.MediaLink.Segments[2]+"传输未结束,已传输:" + $vhdcopystats.BytesCopied/$vhdcopystats.TotalBytes
}elseif($vhdcopystats.Status -eq "Failed"){
$buloon = "false"
"验证Data盘传输结果:"
$datadisk.MediaLink.Segments[2]+"传输失败,报错信息:" + $vhdcopystats.StatusDescription
}
}
}
if($buloon -eq "true"){
"传输已完成"
break
}
sleep 30
}
 
"用新的VHD创建磁盘"
"恢复OS磁盘"
Add-AzureDisk -DiskName ($newvmName +"-"+$osDiskName) -MediaLocation ("https://"+$newStor+".blob.core.chinacloudapi.cn/vhds/" + $newvmName +"-"+$osvhdName) -OS $osType
 
"恢复data磁盘"
foreach($datadisk in $datadisks){
Add-AzureDisk -DiskName ($newvmName +"-"+$datadisk.DiskName) -MediaLocation ("https://"+$newStor+".blob.core.chinacloudapi.cn/vhds/" + $newvmName +"-"+$datadisk.MediaLink.Segments[2])
}
 
"使用新的磁盘恢复虚拟机"
if($vmAS -eq $null){
$vmConfig = New-AzureVMConfig -Name $newvmName –InstanceSize $vmSize -DiskName ($newvmName +"-"+$osDiskName)
$vmConfig|Set-AzureSubnet -SubnetName $newSubnet | Set-AzureStaticVNetIP -IPAddress $newDip
}else{
$vmConfig = New-AzureVMConfig -Name $newvmName –InstanceSize $vmSize -DiskName ($newvmName +"-"+$osDiskName) -AvailabilitySetName $vmAS
$vmConfig|Set-AzureSubnet -SubnetName $newSubnet | Set-AzureStaticVNetIP -IPAddress $newDip
}
 
"为虚拟机恢复数据盘"
foreach($datadisk in $datadisks){
$vmConfig|add-AzureDataDisk -Import -DiskName ($newvmName +"-"+$datadisk.DiskName) -LUN $datadisk.Lun
}
 
 
 
"为虚拟机恢复终结点和ACL"
<#
foreach($endpoint in $endpoints){
$acl_config = New-AzureAclConfig
$acls = $endpoint.Acl
$endpoint.Name
foreach( $acl in $acls){
Set-AzureAclConfig -AddRule $acl.Action -Order $acl.Order -RemoteSubnet $acl.RemoteSubnet -Description $acl.Description -ACL $acl_config
}
if($endpoint.LBSetName -eq $null){
$vmConfig|Add-AzureEndpoint -Name $endpoint.Name -Protocol $endpoint.Protocol -LocalPort $endpoint.LocalPort -PublicPort $endpoint.Port -ACL $acl_config
}elseif($endpoint.IdleTimeoutInMinutes -eq $null){
$vmConfig|
Add-AzureEndpoint -Name $endpoint.Name -Protocol $endpoint.Protocol -LocalPort $endpoint.LocalPort -PublicPort $endpoint.Port -ACL $acl_config -LBSetName $endpoint.LBSetName -ProbePort $endpoint.ProbePort -ProbeProtocol $endpoint.ProbeProtocol -ProbeTimeoutInSeconds $endpoint.ProbeTimeoutInSeconds -ProbeIntervalInSeconds $endpoint.ProbeIntervalInSeconds -LoadBalancerDistribution $endpoint.LoadBalancerDistribution
}else{
$vmConfig|
Add-AzureEndpoint -Name $endpoint.Name -Protocol $endpoint.Protocol -LocalPort $endpoint.LocalPort -PublicPort $endpoint.Port -ACL $acl_config -LBSetName $endpoint.LBSetName -ProbePort $endpoint.ProbePort -ProbeProtocol $endpoint.ProbeProtocol -ProbeTimeoutInSeconds $endpoint.ProbeTimeoutInSeconds -IdleTimeoutInMinutes $endpoint.IdleTimeoutInMinutes -ProbeIntervalInSeconds $endpoint.ProbeIntervalInSeconds
}
} #>
 
$vmConfig|New-AzureVM -ServiceName $newserviceName –VnetName $newVnet
######################脚本部分#####################
相关标签: azure powershell