forked from robnotto/ASRScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASR Protection Script.ps1
More file actions
126 lines (73 loc) · 4.76 KB
/
ASR Protection Script.ps1
File metadata and controls
126 lines (73 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
Connect-AzAccount
#Select Subscription
$subscription = get-azsubscription | Out-GridView -PassThru -Title "Select the Subscription to work with"
Select-AzSubscription -Subscription $subscription
#Select Source Resource Group
$ResourceGRoup=Get-azresourcegroup | Out-GridView -PassThru -Title "Select Azure Resource Group to Protect"
$RG=$ResourceGRoup.ResourceGroupName
#Select Recovery Vault
$vault = get-azrecoveryservicesvault | Out-GridView -PassThru -Title "Select the Vault to work with"
Set-AzRecoveryServicesAsrVaultContext -Vault $vault
#Get Azure ASR Fabric
$fabric=Get-AzRecoveryServicesAsrFabric
$Sourcecontainer = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $fabric[0]
$Targetcontainer = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $fabric[1]
#You can create if it does not exist and assign below
$CacheStorageAccount=Get-AzStorageAccount -Name "kf1wkmasrvaultasrcache" -ResourceGroupName "asrvault"
#Select Existing Azure Site Recovery Policy
$ASRPolicy=Get-AzRecoveryServicesAsrPolicy | Out-GridView -PassThru -Title "Select Replication Policy to use"
#Select Protection Container Mapping
$Source2TargetMapping=Get-AzRecoveryServicesAsrProtectionContainerMapping -ProtectionContainer $sourcecontainer | Out-GridView -PassThru -Title "Select Source Region"
$Target2SourceMapping=Get-AzRecoveryServicesAsrProtectionContainerMapping -ProtectionContainer $sourcecontainer | Out-GridView -PassThru -Title "Select Target Region"
#Select Target Resources
$TargetRecoveryRG=Get-AzResourceGroup -Name "asrtarget"
$TargetPPG=Get-AzProximityPlacementGroup -ResourceGroupName "ppgeast2" -Name "ppgeast2"
$TargetAvSet=Get-AzAvailabilitySet -ResourceGroupName "asrvault" -name "ASRDEMO-S2D-AS-asr"
#Vm List
$vms=Get-azVm -ResourceGroupName $ResourceGRoup.ResourceGroupName
$VMCount=$vms.Count
$ProtectVMCount=0
#Exclude VMs by name
$ExcludeVM="asrppg-s2d-1","asrppg-s2d-2","asrppgtest01"
#Check for Excluded VMs.
Foreach ($vm in $VMs) {
If ($vm.name -notin $ExcludeVM)
{
Write-Host "Protecting VM" $vm.Name
#Capture Availability Set
$VMavailabilitySet=$vm.AvailabilitySetReference
#Os Disk
$OSdiskId = $vm.StorageProfile.OsDisk.ManagedDisk.Id
$OSDisk = get-azdisk -ResourceGroupName $vm.ResourceGroupName -DiskName $OSdiskId.split('/')[-1]
$RecoveryOSDiskAccountType = $osdisk.sku.name
$RecoveryReplicaDiskAccountType = $osdisk.sku.name
#$RecoveryOSDiskAccountType = $vm.StorageProfile.OsDisk.ManagedDisk.StorageAccountType
#$RecoveryReplicaDiskAccountType = $vm.StorageProfile.OsDisk.ManagedDisk.StorageAccountType
$OSDiskReplicationConfig = New-AzRecoveryServicesAsrAzureToAzureDiskReplicationConfig -ManagedDisk -LogStorageAccountId $CacheStorageAccount.id -DiskId $OSdiskId -RecoveryResourceGroupId $TargetRecoveryRG.ResourceId -RecoveryReplicaDiskAccountType $RecoveryReplicaDiskAccountType -RecoveryTargetDiskAccountType $RecoveryOSDiskAccountType
#DataDisk
Write-host "Located" $vm.StorageProfile.DataDisks.Count "Data Disks on" $Vm.Name
$datadiskconfigs = @()
foreach($datadisk in $vm.StorageProfile.DataDisks)
{
$datadiskId = $datadisk.ManagedDisk.Id
Write-host $datadiskId "Located as data disk for" $Vm.Name
$DataDiskInfo = get-azdisk -ResourceGroupName $vm.ResourceGroupName -DiskName $datadiskId.split('/')[-1]
$RecoveryReplicaDiskAccountType = $datadiskinfo.sku.name
$RecoveryTargetDiskAccountType = $datadiskinfo.sku.name
$datadiskconfig = New-AzRecoveryServicesAsrAzureToAzureDiskReplicationConfig -ManagedDisk -LogStorageAccountId $CacheStorageAccount.Id -DiskId $datadiskId -RecoveryResourceGroupId $TargetRecoveryRG.ResourceId -RecoveryReplicaDiskAccountType $RecoveryReplicaDiskAccountType -RecoveryTargetDiskAccountType $RecoveryTargetDiskAccountType
$datadisksconfigs = $datadiskconfigs + $datadiskconfig
}
$ProtectVMCount=$ProtectVMCount+1
$diskconfigs = @()
$diskconfigs += $OSDiskReplicationConfig
$diskconfigs += $datadiskconfigs
$TempASRJob = New-AzRecoveryServicesAsrReplicationProtectedItem -AzureToAzure -AzureVmId $VM.Id -Name (New-Guid).Guid -ProtectionContainerMapping $Source2TargetMapping -AzureToAzureDiskReplicationConfiguration $diskconfigs -RecoveryResourceGroupId $TargetRecoveryRG.ResourceId -RecoveryProximityPlacementGroupId $TargetPPG.Id -RecoveryAvailabilitySetId $targetavset
#Track Job status to check for completion
while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){
sleep 10;
$TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob
#Check if the Job completed successfully. The updated job state of a successfully completed job should be "Succeeded"
Write-Output $TempASRJob.State "On VM:" $VM.Name
}
}
}