-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathRemove-AzMigrateLocalServerReplication.ps1
More file actions
167 lines (145 loc) · 8 KB
/
Remove-AzMigrateLocalServerReplication.ps1
File metadata and controls
167 lines (145 loc) · 8 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# ----------------------------------------------------------------------------------
#
# Copyright Microsoft Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------------
<#
.Synopsis
Stops replication for the migrated server.
.Description
The Remove-AzMigrateLocalServerReplication cmdlet stops the replication for a migrated server.
.Link
https://learn.microsoft.com/powershell/module/az.migrate/remove-azmigratelocalserverreplication
#>
function Remove-AzMigrateLocalServerReplication {
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Runtime.PreviewMessageAttribute("This cmdlet is based on a preview API version and may experience breaking changes in future releases.")]
[OutputType([Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.Api20260201.IJobModel])]
[CmdletBinding(DefaultParameterSetName = 'ByID', PositionalBinding = $false, SupportsShouldProcess, ConfirmImpact='Medium')]
param(
[Parameter(ParameterSetName = 'ByID', Mandatory)]
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Category('Path')]
[System.String]
# Specifies the replcating server for which the replication needs to be disabled. The ID should be retrieved using the Get-AzMigrateLocalServerReplication cmdlet.
${TargetObjectID},
[Parameter(ParameterSetName = 'ByInputObject', Mandatory, ValueFromPipeline)]
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Category('Path')]
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Models.IMigrateIdentity]
# Specifies the replicating server for which the replication needs to be disabled. The server object can be retrieved using the Get-AzMigrateLocalServerReplication cmdlet.
${InputObject},
[Parameter()]
[ValidateSet("true" , "false")]
[ArgumentCompleter( { "true" , "false" })]
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Category('Query')]
[System.String]
# Specifies whether the replication needs to be force removed. Default to "false".
${ForceRemove} = "false",
[Parameter()]
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Category('Path')]
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Runtime.DefaultInfo(Script = '(Get-AzContext).Subscription.Id')]
[System.String]
# Azure Subscription ID.
${SubscriptionId},
[Parameter()]
[Alias('AzureRMContext', 'AzureCredential')]
[ValidateNotNull()]
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Category('Azure')]
[System.Management.Automation.PSObject]
# The credentials, account, tenant, and subscription used for communication with Azure.
${DefaultProfile},
[Parameter(DontShow)]
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Category('Runtime')]
[System.Management.Automation.SwitchParameter]
# Wait for .NET debugger to attach
${Break},
[Parameter(DontShow)]
[ValidateNotNull()]
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Category('Runtime')]
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Runtime.SendAsyncStep[]]
# SendAsync Pipeline Steps to be appended to the front of the pipeline
${HttpPipelineAppend},
[Parameter(DontShow)]
[ValidateNotNull()]
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Category('Runtime')]
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Runtime.SendAsyncStep[]]
# SendAsync Pipeline Steps to be prepended to the front of the pipeline
${HttpPipelinePrepend},
[Parameter(DontShow)]
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Category('Runtime')]
[System.Uri]
# The URI for the proxy server to use
${Proxy},
[Parameter(DontShow)]
[ValidateNotNull()]
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Category('Runtime')]
[System.Management.Automation.PSCredential]
# Credentials for a proxy server to use for the remote call
${ProxyCredential},
[Parameter(DontShow)]
[Microsoft.Azure.PowerShell.Cmdlets.Migrate.Category('Runtime')]
[System.Management.Automation.SwitchParameter]
# Use the default credentials for the proxy
${ProxyUseDefaultCredentials}
)
process {
$helperPath = [System.IO.Path]::Combine($PSScriptRoot, "Helper", "AzLocalCommonSettings.ps1")
Import-Module $helperPath
$helperPath = [System.IO.Path]::Combine($PSScriptRoot, "Helper", "AzLocalCommonHelper.ps1")
Import-Module $helperPath
$shouldForceRemove = [System.Convert]::ToBoolean($ForceRemove)
$null = $PSBoundParameters.Remove('ForceRemove')
$null = $PSBoundParameters.Remove('TargetObjectID')
$null = $PSBoundParameters.Remove('InputObject')
$null = $PSBoundParameters.Remove('WhatIf')
$null = $PSBoundParameters.Remove('Confirm')
# Set common ErrorVariable and ErrorAction for get behaviors
$null = $PSBoundParameters.Add('ErrorVariable', 'notPresent')
$null = $PSBoundParameters.Add('ErrorAction', 'SilentlyContinue')
$parameterSet = $PSCmdlet.ParameterSetName
if ($parameterSet -eq 'ByInputObject') {
$TargetObjectID = $InputObject.Id
}
$protectedItemIdArray = $TargetObjectID.Split("/")
$resourceGroupName = $protectedItemIdArray[4]
$vaultName = $protectedItemIdArray[8]
$protectedItemName = $protectedItemIdArray[10]
# Get protected item with ResourceGroupName, VaultName, Name
$null = $PSBoundParameters.Add("ResourceGroupName", $resourceGroupName)
$null = $PSBoundParameters.Add("VaultName", $vaultName)
$null = $PSBoundParameters.Add("Name", $protectedItemName)
$ProtectedItem = Az.Migrate.Internal\Get-AzMigrateProtectedItem @PSBoundParameters
if ($null -eq $ProtectedItem)
{
throw New-AzMigrateProtectedItemNotFoundException -Id $TargetObjectID
}
$null = $PSBoundParameters.Remove('Name')
if ("DisableProtection" -notin $ProtectedItem.Property.AllowedJob)
{
throw "Replication item with Id '$TargetObjectID' cannot be removed at this moment. Current protection state is '$($protectedItem.Property.ProtectionStateDescription)'. Allowed jobs are: $($ProtectedItem.Property.AllowedJob -join ', ')."
}
# Remove common ErrorVariable and ErrorAction for get behaviors
$null = $PSBoundParameters.Remove('ErrorVariable')
$null = $PSBoundParameters.Remove('ErrorAction')
if ($PSCmdlet.ShouldProcess($TargetObjectID, "Stop/Complete VM replication.")) {
# Remove protected item with ResourceGroupName, VaultName, ProtectedItemName
$null = $PSBoundParameters.Add('ProtectedItemName', $protectedItemName)
$null = $PSBoundParameters.Add('NoWait', $true)
$null = $PSBoundParameters.Add('ForceDelete', $shouldForceRemove)
$operation = Az.Migrate.Internal\Remove-AzMigrateProtectedItem @PSBoundParameters
$null = $PSBoundParameters.Remove('ForceDelete')
$null = $PSBoundParameters.Remove('NoWait')
$null = $PSBoundParameters.Remove('ProtectedItemName')
# Get job with ResourceGroupName, VaultName, JobName
$jobName = $operation.Target.Split("/")[-1].Split("?")[0].Split("_")[0]
$null = $PSBoundParameters.Add('JobName', $jobName)
return Az.Migrate.Internal\Get-AzMigrateLocalReplicationJob @PSBoundParameters
}
}
}