From bd4764c24365b9222989f078a3d1d813f525deb3 Mon Sep 17 00:00:00 2001 From: singhabh27 Date: Thu, 19 Aug 2021 20:11:20 +0530 Subject: [PATCH 1/2] Subnets not in same Vnet fix --- .../custom/Start-AzMigrateTestMigration.ps1 | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Migrate/custom/Start-AzMigrateTestMigration.ps1 b/src/Migrate/custom/Start-AzMigrateTestMigration.ps1 index 70551e3835a7..560f8455d09c 100644 --- a/src/Migrate/custom/Start-AzMigrateTestMigration.ps1 +++ b/src/Migrate/custom/Start-AzMigrateTestMigration.ps1 @@ -117,7 +117,6 @@ function Start-AzMigrateTestMigration { $FabricName = $MachineIdArray[10] $ProtectionContainerName = $MachineIdArray[12] $MachineName = $MachineIdArray[14] - $null = $PSBoundParameters.Add("ResourceGroupName", $ResourceGroupName) $null = $PSBoundParameters.Add("ResourceName", $VaultName) @@ -126,6 +125,42 @@ function Start-AzMigrateTestMigration { $null = $PSBoundParameters.Add("ProtectionContainerName", $ProtectionContainerName) $ReplicationMigrationItem = Az.Migrate.internal\Get-AzMigrateReplicationMigrationItem @PSBoundParameters + $AvSetId = $ReplicationMigrationItem.ProviderSpecificDetail.TargetAvailabilitySetId + if ($AvSetId) + { + Import-module -Name Az.Compute + Import-module -Name Az.Network + $AvSetName = $AvSetId.Split("/")[-1]; + $AvSetRg = $AvSetId.Split("/")[-5]; + $AvSet = Get-AzAvailabilitySet -ResourceGroupName $AvSetRg -Name $AvSetName -ErrorVariable notPresent -ErrorAction SilentlyContinue + if (!$AvSet) + { + throw "Availability Set '$($AvSetId)' does not exist." + } + $VminAvSet = $AvSet.VirtualMachinesReferences[0].Id + if ($VminAvSet) + { + $VmNameinAvSet = $VminAvSet.Split("/")[-1] + $VM = Get-AzVM -ResourceGroupName $AvSetRg -Name $VmNameinAvSet -ErrorVariable notPresent -ErrorAction SilentlyContinue + if ($VM) + { + $NicId = $VM.NetworkProfile.NetworkInterfaces[0].Id + $Nic = Get-Aznetworkinterface -resourceid $NicId -ErrorVariable notPresent -ErrorAction SilentlyContinue + if($Nic -And ($Nic.IpConfigurations) -And ($Nic.IpConfigurations[0].Subnet) -And ($Nic.IpConfigurations[0].Subnet.Id)) + { + $Subnet = $Nic.IpConfigurations[0].Subnet.Id.Split("/") + $VnetID = "/$($Subnet[1])/$($Subnet[2])/$($Subnet[3])/$($Subnet[4])/$($Subnet[5])/$($Subnet[6])/$($Subnet[7])/$($Subnet[8])" + } + $TestNetworkID = $TestNetworkID.TrimEnd("/") + + if ($TestNetworkID -ne $VnetID) + { + throw "Virtual Machines in the availability set '$AvSetName' can only be connected to virtual network '$VnetID'. Change the virtual network selected for the test or update the availability set for the machines, and retry the operation." + } + } + } + } + if ($ReplicationMigrationItem -and ($ReplicationMigrationItem.ProviderSpecificDetail.InstanceType -eq 'VMwarecbt') -and ($ReplicationMigrationItem.AllowedOperation -contains 'TestMigrate' )) { $ProviderSpecificDetailInput = [Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20210210.VMwareCbtTestMigrateInput]::new() $ProviderSpecificDetailInput.InstanceType = 'VMwareCbt' From 32a4ba4785d59df9aa8dc6d72936b5dca9b341da Mon Sep 17 00:00:00 2001 From: singhabh27 Date: Fri, 27 Aug 2021 19:33:13 +0530 Subject: [PATCH 2/2] Added null check --- .../custom/Start-AzMigrateTestMigration.ps1 | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/Migrate/custom/Start-AzMigrateTestMigration.ps1 b/src/Migrate/custom/Start-AzMigrateTestMigration.ps1 index 560f8455d09c..4874e015362e 100644 --- a/src/Migrate/custom/Start-AzMigrateTestMigration.ps1 +++ b/src/Migrate/custom/Start-AzMigrateTestMigration.ps1 @@ -137,25 +137,27 @@ function Start-AzMigrateTestMigration { { throw "Availability Set '$($AvSetId)' does not exist." } - $VminAvSet = $AvSet.VirtualMachinesReferences[0].Id - if ($VminAvSet) + if ($AvSet.VirtualMachineReferences -And ($AvSet.VirtualMachineReferences.Count -gt 0)) { - $VmNameinAvSet = $VminAvSet.Split("/")[-1] - $VM = Get-AzVM -ResourceGroupName $AvSetRg -Name $VmNameinAvSet -ErrorVariable notPresent -ErrorAction SilentlyContinue - if ($VM) + $VminAvSet = $AvSet.VirtualMachinesReferences[0].Id + if ($VminAvSet) { - $NicId = $VM.NetworkProfile.NetworkInterfaces[0].Id - $Nic = Get-Aznetworkinterface -resourceid $NicId -ErrorVariable notPresent -ErrorAction SilentlyContinue - if($Nic -And ($Nic.IpConfigurations) -And ($Nic.IpConfigurations[0].Subnet) -And ($Nic.IpConfigurations[0].Subnet.Id)) + $VmNameinAvSet = $VminAvSet.Split("/")[-1] + $VM = Get-AzVM -ResourceGroupName $AvSetRg -Name $VmNameinAvSet -ErrorVariable notPresent -ErrorAction SilentlyContinue + if ($VM) { - $Subnet = $Nic.IpConfigurations[0].Subnet.Id.Split("/") - $VnetID = "/$($Subnet[1])/$($Subnet[2])/$($Subnet[3])/$($Subnet[4])/$($Subnet[5])/$($Subnet[6])/$($Subnet[7])/$($Subnet[8])" - } - $TestNetworkID = $TestNetworkID.TrimEnd("/") + $NicId = $VM.NetworkProfile.NetworkInterfaces[0].Id + $Nic = Get-Aznetworkinterface -resourceid $NicId -ErrorVariable notPresent -ErrorAction SilentlyContinue + if($Nic -And ($Nic.IpConfigurations) -And ($Nic.IpConfigurations[0].Subnet) -And ($Nic.IpConfigurations[0].Subnet.Id)) + { + $Subnet = $Nic.IpConfigurations[0].Subnet.Id.Split("/") + $VnetID = "/$($Subnet[1])/$($Subnet[2])/$($Subnet[3])/$($Subnet[4])/$($Subnet[5])/$($Subnet[6])/$($Subnet[7])/$($Subnet[8])" + } - if ($TestNetworkID -ne $VnetID) - { - throw "Virtual Machines in the availability set '$AvSetName' can only be connected to virtual network '$VnetID'. Change the virtual network selected for the test or update the availability set for the machines, and retry the operation." + if ($TestNetworkID -ne $VnetID) + { + throw "Virtual Machines in the availability set '$AvSetName' can only be connected to virtual network '$VnetID'. Change the virtual network selected for the test or update the availability set for the machines, and retry the operation." + } } } }