Skip to content

Commit f12f669

Browse files
committed
Removed all Force stuff from Set-Azs*, capture CloudException and format
1 parent 6940811 commit f12f669

File tree

31 files changed

+823
-897
lines changed

31 files changed

+823
-897
lines changed

src/StackAdmin/Azs.AzureBridge.Admin/Module/Azs.AzureBridge.Admin/Get-TaskResult.ps1

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,43 @@ Microsoft.PowerShell.Core\Set-StrictMode -Version Latest
2828
function Get-TaskResult {
2929
[CmdletBinding()]
3030
param(
31-
[Parameter(Mandatory=$true)]
31+
[Parameter(Mandatory = $true)]
3232
[object]
3333
$TaskResult,
3434

35-
[Parameter(Mandatory=$false)]
35+
[Parameter(Mandatory = $false)]
3636
[PSCustomObject]
3737
$SkipInfo,
3838

39-
[Parameter(Mandatory=$false)]
39+
[Parameter(Mandatory = $false)]
4040
[PSCustomObject]
4141
$TopInfo,
4242

43-
[Parameter(Mandatory=$false)]
43+
[Parameter(Mandatory = $false)]
4444
[PSCustomObject]
4545
$PageResult,
4646

47-
[Parameter(Mandatory=$false)]
47+
[Parameter(Mandatory = $false)]
4848
[Type]
4949
$PageType
5050
)
5151

52-
$ErrorActionPreference = 'Stop'
53-
$null = $TaskResult.AsyncWaitHandle.WaitOne()
52+
function Get-Exception {
53+
param(
54+
$Exception
55+
)
56+
try {
57+
$cloudException = "Code = $($Exception.Body.Code)`nMessage = $($Exception.Body.Message)"
58+
$Exception = $cloudException
59+
} catch {
60+
# Catch exception so no output.
61+
} finally {
62+
throw $Exception
63+
}
64+
}
65+
66+
$ErrorActionPreference = 'Stop'
67+
$null = $TaskResult.AsyncWaitHandle.WaitOne()
5468
Write-Debug -Message "$($TaskResult | Out-String)"
5569

5670
if ((Get-Member -InputObject $TaskResult -Name 'Result') -and
@@ -66,44 +80,34 @@ function Get-TaskResult {
6680
}
6781
foreach ($item in $result) {
6882
if ($SkipInfo -and ($SkipInfo.Count++ -lt $SkipInfo.Max)) {
69-
}
70-
else {
83+
} else {
7184
if ((-not $TopInfo) -or ($TopInfo.Max -eq -1) -or ($TopInfo.Count++ -lt $TopInfo.Max)) {
7285
$item
73-
}
74-
else {
86+
} else {
7587
break
7688
}
7789
}
7890
}
7991
} else {
8092
$result
8193
}
82-
}
83-
elseif ($TaskResult.IsFaulted)
84-
{
94+
} elseif ($TaskResult.IsFaulted) {
8595
Write-Verbose -Message 'Operation failed.'
8696
if ($TaskResult.Exception) {
8797
if ((Get-Member -InputObject $TaskResult.Exception -Name 'InnerExceptions') -and $TaskResult.Exception.InnerExceptions) {
8898
foreach ($ex in $TaskResult.Exception.InnerExceptions) {
89-
Write-Error -Exception $ex
99+
Get-Exception -Exception $ex
90100
}
91-
}
92-
elseif ((Get-Member -InputObject $TaskResult.Exception -Name 'InnerException') -and $TaskResult.Exception.InnerException) {
93-
Write-Error -Exception $TaskResult.Exception.InnerException
94-
}
95-
else {
96-
Write-Error -Exception $TaskResult.Exception
101+
} elseif ((Get-Member -InputObject $TaskResult.Exception -Name 'InnerException') -and $TaskResult.Exception.InnerException) {
102+
Get-Exception -Exception $TaskResult.Exception.InnerException
103+
} else {
104+
Get-Exception -Exception $TaskResult.Exception
97105
}
98106
}
99-
}
100-
elseif ($TaskResult.IsCanceled)
101-
{
107+
} elseif ($TaskResult.IsCanceled) {
102108
Write-Verbose -Message 'Operation got cancelled.'
103109
Throw 'Operation got cancelled.'
104-
}
105-
else
106-
{
110+
} else {
107111
Write-Verbose -Message 'Operation completed successfully.'
108112
}
109113
}

src/StackAdmin/Azs.Backup.Admin/Module/Azs.Backup.Admin/Generated.PowerShell.Commands/SwaggerPathCommands/Restore-AzsBackup.ps1

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,7 @@ function Restore-AzsBackup {
142142
$GetTaskResult_params = @{
143143
TaskResult = $TaskResult
144144
}
145-
try {
146-
Get-TaskResult @GetTaskResult_params
147-
} catch {
148-
@{
149-
"Code" = $_.Exception.Body.Code;
150-
"Message" = $_.Exception.Body.Message
151-
}
152-
}
145+
Get-TaskResult @GetTaskResult_params
153146
}
154147
}
155148

src/StackAdmin/Azs.Backup.Admin/Module/Azs.Backup.Admin/Generated.PowerShell.Commands/SwaggerPathCommands/Set-AzsBackupShare.ps1

Lines changed: 68 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Licensed under the MIT License. See License.txt in the project root for license
2020
The resource id.
2121
2222
.PARAMETER InputObject
23-
The input object of type Microsoft.AzureStack.Management.Backup.Admin.Models.BackupLocation.
23+
Backup location configuration returned by Get-AzsBackupLocation.
2424
2525
.PARAMETER BackupShare
2626
Location where backups will be stored.
@@ -37,9 +37,6 @@ Licensed under the MIT License. See License.txt in the project root for license
3737
.PARAMETER AsJob
3838
Run asynchronous as a job and return the job object.
3939
40-
.PARAMETER Force
41-
Don't ask for confirmation.
42-
4340
.EXAMPLE
4441
4542
PS C:\> Set-AzsBackupShare -BackupShare "\\***.***.***.***\Share" -Username "asdomain1\azurestackadmin" -Password $password -EncryptionKey $encryptionKey
@@ -101,11 +98,7 @@ function Set-AzsBackupShare {
10198

10299
[Parameter(Mandatory = $false)]
103100
[switch]
104-
$AsJob,
105-
106-
[Parameter(Mandatory = $false)]
107-
[switch]
108-
$Force
101+
$AsJob
109102
)
110103

111104
Begin {
@@ -140,94 +133,85 @@ function Set-AzsBackupShare {
140133

141134
# Should process
142135
if ($PSCmdlet.ShouldProcess("$Location" , "Set backup configuration for location.")) {
143-
if ($Force.IsPresent -or $PSCmdlet.ShouldContinue("Set backup configuration backup for location?", "Performing operation set backup configuration at $Location.")) {
144136

145-
if ([System.String]::IsNullOrEmpty($Location)) {
146-
$Location = (Get-AzureRMLocation).Location
147-
}
148-
if ([System.String]::IsNullOrEmpty($ResourceGroupName)) {
149-
$ResourceGroupName = "System.$($Location)"
150-
}
137+
if ([System.String]::IsNullOrEmpty($Location)) {
138+
$Location = (Get-AzureRMLocation).Location
139+
}
140+
if ([System.String]::IsNullOrEmpty($ResourceGroupName)) {
141+
$ResourceGroupName = "System.$($Location)"
142+
}
151143

152-
$NewServiceClient_params = @{
153-
FullClientTypeName = 'Microsoft.AzureStack.Management.Backup.Admin.BackupAdminClient'
154-
}
144+
$NewServiceClient_params = @{
145+
FullClientTypeName = 'Microsoft.AzureStack.Management.Backup.Admin.BackupAdminClient'
146+
}
155147

156-
$GlobalParameterHashtable = @{}
157-
$NewServiceClient_params['GlobalParameterHashtable'] = $GlobalParameterHashtable
148+
$GlobalParameterHashtable = @{}
149+
$NewServiceClient_params['GlobalParameterHashtable'] = $GlobalParameterHashtable
158150

159-
$GlobalParameterHashtable['SubscriptionId'] = $null
160-
if ($PSBoundParameters.ContainsKey('SubscriptionId')) {
161-
$GlobalParameterHashtable['SubscriptionId'] = $PSBoundParameters['SubscriptionId']
162-
}
151+
$GlobalParameterHashtable['SubscriptionId'] = $null
152+
if ($PSBoundParameters.ContainsKey('SubscriptionId')) {
153+
$GlobalParameterHashtable['SubscriptionId'] = $PSBoundParameters['SubscriptionId']
154+
}
163155

164-
$BackupAdminClient = New-ServiceClient @NewServiceClient_params
156+
$BackupAdminClient = New-ServiceClient @NewServiceClient_params
165157

166-
if ('InputObject' -eq $PsCmdlet.ParameterSetName -or 'Update' -eq $PsCmdlet.ParameterSetName -or 'ResourceId' -eq $PsCmdlet.ParameterSetName) {
158+
if ('InputObject' -eq $PsCmdlet.ParameterSetName -or 'Update' -eq $PsCmdlet.ParameterSetName -or 'ResourceId' -eq $PsCmdlet.ParameterSetName) {
167159

168-
if ($InputObject -eq $null) {
169-
$InputObject = Get-AzsBackupLocation -ResourceGroupName $ResourceGroupName -Location $Location
170-
}
160+
if ($InputObject -eq $null) {
161+
$InputObject = Get-AzsBackupLocation -ResourceGroupName $ResourceGroupName -Location $Location
162+
}
171163

172-
$InputObject.Path = $BackupShare
173-
$InputObject.UserName = $Username
174-
$InputObject.Password = ConvertTo-String -SecureString $Password
175-
$InputObject.EncryptionKeyBase64 = ConvertTo-String $EncryptionKey
164+
$InputObject.Path = $BackupShare
165+
$InputObject.UserName = $Username
166+
$InputObject.Password = ConvertTo-String -SecureString $Password
167+
$InputObject.EncryptionKeyBase64 = ConvertTo-String $EncryptionKey
176168

177-
Write-Verbose -Message 'Performing operation UpdateWithHttpMessagesAsync on $BackupAdminClient.'
178-
$TaskResult = $BackupAdminClient.BackupLocations.UpdateWithHttpMessagesAsync($ResourceGroupName, $Location, $InputObject)
179-
} else {
180-
Write-Verbose -Message 'Failed to map parameter set to operation method.'
181-
throw 'Module failed to find operation to execute.'
182-
}
169+
Write-Verbose -Message 'Performing operation UpdateWithHttpMessagesAsync on $BackupAdminClient.'
170+
$TaskResult = $BackupAdminClient.BackupLocations.UpdateWithHttpMessagesAsync($ResourceGroupName, $Location, $InputObject)
171+
} else {
172+
Write-Verbose -Message 'Failed to map parameter set to operation method.'
173+
throw 'Module failed to find operation to execute.'
174+
}
183175

184-
Write-Verbose -Message "Waiting for the operation to complete."
185-
186-
$PSSwaggerJobScriptBlock = {
187-
[CmdletBinding()]
188-
param(
189-
[Parameter(Mandatory = $true)]
190-
[System.Threading.Tasks.Task]
191-
$TaskResult,
192-
193-
[Parameter(Mandatory = $true)]
194-
[System.String]
195-
$TaskHelperFilePath
196-
)
197-
if ($TaskResult) {
198-
. $TaskHelperFilePath
199-
$GetTaskResult_params = @{
200-
TaskResult = $TaskResult
201-
}
202-
try {
203-
Get-TaskResult @GetTaskResult_params
204-
} catch {
205-
@{
206-
"Code" = $_.Exception.Body.Code;
207-
"Message" = $_.Exception.Body.Message
208-
}
209-
}
176+
Write-Verbose -Message "Waiting for the operation to complete."
177+
178+
$PSSwaggerJobScriptBlock = {
179+
[CmdletBinding()]
180+
param(
181+
[Parameter(Mandatory = $true)]
182+
[System.Threading.Tasks.Task]
183+
$TaskResult,
184+
185+
[Parameter(Mandatory = $true)]
186+
[System.String]
187+
$TaskHelperFilePath
188+
)
189+
if ($TaskResult) {
190+
. $TaskHelperFilePath
191+
$GetTaskResult_params = @{
192+
TaskResult = $TaskResult
210193
}
194+
Get-TaskResult @GetTaskResult_params
211195
}
196+
}
212197

213-
$PSCommonParameters = Get-PSCommonParameter -CallerPSBoundParameters $PSBoundParameters
214-
$TaskHelperFilePath = Join-Path -Path $ExecutionContext.SessionState.Module.ModuleBase -ChildPath 'Get-TaskResult.ps1'
215-
if ($AsJob) {
216-
$ScriptBlockParameters = New-Object -TypeName 'System.Collections.Generic.Dictionary[string,object]'
217-
$ScriptBlockParameters['TaskResult'] = $TaskResult
218-
$ScriptBlockParameters['AsJob'] = $true
219-
$ScriptBlockParameters['TaskHelperFilePath'] = $TaskHelperFilePath
220-
$PSCommonParameters.GetEnumerator() | ForEach-Object { $ScriptBlockParameters[$_.Name] = $_.Value }
221-
222-
Start-PSSwaggerJobHelper -ScriptBlock $PSSwaggerJobScriptBlock `
223-
-CallerPSBoundParameters $ScriptBlockParameters `
224-
-CallerPSCmdlet $PSCmdlet `
225-
@PSCommonParameters
226-
} else {
227-
Invoke-Command -ScriptBlock $PSSwaggerJobScriptBlock `
228-
-ArgumentList $TaskResult, $TaskHelperFilePath `
229-
@PSCommonParameters
230-
}
198+
$PSCommonParameters = Get-PSCommonParameter -CallerPSBoundParameters $PSBoundParameters
199+
$TaskHelperFilePath = Join-Path -Path $ExecutionContext.SessionState.Module.ModuleBase -ChildPath 'Get-TaskResult.ps1'
200+
if ($AsJob) {
201+
$ScriptBlockParameters = New-Object -TypeName 'System.Collections.Generic.Dictionary[string,object]'
202+
$ScriptBlockParameters['TaskResult'] = $TaskResult
203+
$ScriptBlockParameters['AsJob'] = $true
204+
$ScriptBlockParameters['TaskHelperFilePath'] = $TaskHelperFilePath
205+
$PSCommonParameters.GetEnumerator() | ForEach-Object { $ScriptBlockParameters[$_.Name] = $_.Value }
206+
207+
Start-PSSwaggerJobHelper -ScriptBlock $PSSwaggerJobScriptBlock `
208+
-CallerPSBoundParameters $ScriptBlockParameters `
209+
-CallerPSCmdlet $PSCmdlet `
210+
@PSCommonParameters
211+
} else {
212+
Invoke-Command -ScriptBlock $PSSwaggerJobScriptBlock `
213+
-ArgumentList $TaskResult, $TaskHelperFilePath `
214+
@PSCommonParameters
231215
}
232216
}
233217
}

src/StackAdmin/Azs.Backup.Admin/Module/Azs.Backup.Admin/Generated.PowerShell.Commands/SwaggerPathCommands/Start-AzsBackup.ps1

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function Start-AzsBackup {
8585

8686
# Should process
8787
if ($PSCmdlet.ShouldProcess("$Location" , "Start backup at $Location")) {
88-
if ($Force.IsPresent -or $PSCmdlet.ShouldContinue("Start backup at $Location?", "Performing operation backup at $Location.")) {
88+
if ($Force.IsPresent -or $PSCmdlet.ShouldContinue("Start backup at $($Location)?", "Performing operation backup at $Location.")) {
8989

9090
if ([System.String]::IsNullOrEmpty($Location)) {
9191
$Location = (Get-AzureRMLocation).Location
@@ -134,19 +134,7 @@ function Start-AzsBackup {
134134
$GetTaskResult_params = @{
135135
TaskResult = $TaskResult
136136
}
137-
try {
138-
Get-TaskResult @GetTaskResult_params
139-
} catch {
140-
$ex = $_
141-
try {
142-
throw @{
143-
"Code" = $ex.Exception.Body.Code;
144-
"Message" = $ex.Exception.Body.Message
145-
}
146-
} catch {
147-
throw $ex
148-
}
149-
}
137+
Get-TaskResult @GetTaskResult_params
150138
}
151139
}
152140

0 commit comments

Comments
 (0)