@@ -63,8 +63,18 @@ function Initialize-AzureSubscription {
6363 if ($Endpoint.Auth.Scheme -eq ' ServicePrincipal' -and ! $script :azureModule -and (Get-Command - Name " Clear-AzureRmContext" - ErrorAction " SilentlyContinue" )) {
6464 Write-Host " ##[command]Clear-AzureRmContext -Scope Process"
6565 $null = Clear-AzureRmContext - Scope Process
66- Write-Host " ##[command]Clear-AzureRmContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue"
67- $null = Clear-AzureRmContext - Scope CurrentUser - Force - ErrorAction SilentlyContinue
66+ }
67+
68+ if (Get-Command - Name " Disable-AzureRmContextAutosave" - ErrorAction " SilentlyContinue" )
69+ {
70+ try {
71+ Write-Host " ##[command]Disable-AzureRmContextAutosave -ErrorAction Stop"
72+ $null = Disable-AzureRmContextAutosave - ErrorAction Stop
73+ }
74+ catch {
75+ $message = $_.Exception.Message
76+ Write-Verbose " Unable to disable Azure RM context save: $message "
77+ }
6878 }
6979
7080 $environmentName = " AzureCloud"
@@ -181,37 +191,55 @@ function Initialize-AzureSubscription {
181191 } else {
182192 # Else, this is AzureRM.
183193 try {
184- if (Get-Command - Name " Add-AzureRmAccount" - ErrorAction " SilentlyContinue" ) {
194+ if (Get-Command - Name " Add-AzureRmAccount" - ErrorAction " SilentlyContinue" ) {
195+ If (CmdletHasMember - cmdlet " Add-AzureRmAccount" - memberName " Scope" )
196+ {
197+ $processScope = @ { Scope = " Process" }
198+ }
199+ else
200+ {
201+ $processScope = @ {}
202+ }
203+
185204 if (CmdletHasMember - cmdlet " Add-AzureRMAccount" - memberName " EnvironmentName" ) {
186205
187206 if ($Endpoint.Auth.Parameters.AuthenticationType -eq " SPNCertificate" ) {
188- Write-Host " ##[command]Add-AzureRMAccount -ServicePrincipal -Tenant $ ( $Endpoint.Auth.Parameters.TenantId ) -CertificateThumbprint ****** -ApplicationId $ ( $Endpoint.Auth.Parameters.ServicePrincipalId ) -EnvironmentName $environmentName "
189- $null = Add-AzureRmAccount - ServicePrincipal - Tenant $Endpoint.Auth.Parameters.TenantId - CertificateThumbprint $servicePrincipalCertificate.Thumbprint - ApplicationId $Endpoint.Auth.Parameters.ServicePrincipalId - EnvironmentName $environmentName
207+ Write-Host " ##[command]Add-AzureRMAccount -ServicePrincipal -Tenant $ ( $Endpoint.Auth.Parameters.TenantId ) -CertificateThumbprint ****** -ApplicationId $ ( $Endpoint.Auth.Parameters.ServicePrincipalId ) -EnvironmentName $environmentName @processScope "
208+ $null = Add-AzureRmAccount - ServicePrincipal - Tenant $Endpoint.Auth.Parameters.TenantId - CertificateThumbprint $servicePrincipalCertificate.Thumbprint - ApplicationId $Endpoint.Auth.Parameters.ServicePrincipalId - EnvironmentName $environmentName @processScope
190209 }
191210 else {
192- Write-Host " ##[command]Add-AzureRMAccount -ServicePrincipal -Tenant $ ( $Endpoint.Auth.Parameters.TenantId ) -Credential $psCredential -EnvironmentName $environmentName "
193- $null = Add-AzureRMAccount - ServicePrincipal - Tenant $Endpoint.Auth.Parameters.TenantId - Credential $psCredential - EnvironmentName $environmentName
211+ Write-Host " ##[command]Add-AzureRMAccount -ServicePrincipal -Tenant $ ( $Endpoint.Auth.Parameters.TenantId ) -Credential $psCredential -EnvironmentName $environmentName @processScope "
212+ $null = Add-AzureRMAccount - ServicePrincipal - Tenant $Endpoint.Auth.Parameters.TenantId - Credential $psCredential - EnvironmentName $environmentName @processScope
194213 }
195214 }
196215 else {
197216 if ($Endpoint.Auth.Parameters.AuthenticationType -eq " SPNCertificate" ) {
198- Write-Host " ##[command]Add-AzureRMAccount -ServicePrincipal -Tenant $ ( $Endpoint.Auth.Parameters.TenantId ) -CertificateThumbprint ****** -ApplicationId $ ( $Endpoint.Auth.Parameters.ServicePrincipalId ) -Environment $environmentName "
199- $null = Add-AzureRmAccount - ServicePrincipal - Tenant $Endpoint.Auth.Parameters.TenantId - CertificateThumbprint $servicePrincipalCertificate.Thumbprint - ApplicationId $Endpoint.Auth.Parameters.ServicePrincipalId - Environment $environmentName
217+ Write-Host " ##[command]Add-AzureRMAccount -ServicePrincipal -Tenant $ ( $Endpoint.Auth.Parameters.TenantId ) -CertificateThumbprint ****** -ApplicationId $ ( $Endpoint.Auth.Parameters.ServicePrincipalId ) -Environment $environmentName @processScope "
218+ $null = Add-AzureRmAccount - ServicePrincipal - Tenant $Endpoint.Auth.Parameters.TenantId - CertificateThumbprint $servicePrincipalCertificate.Thumbprint - ApplicationId $Endpoint.Auth.Parameters.ServicePrincipalId - Environment $environmentName @processScope
200219 }
201220 else {
202- Write-Host " ##[command]Add-AzureRMAccount -ServicePrincipal -Tenant $ ( $Endpoint.Auth.Parameters.TenantId ) -Credential $psCredential -Environment $environmentName "
203- $null = Add-AzureRMAccount - ServicePrincipal - Tenant $Endpoint.Auth.Parameters.TenantId - Credential $psCredential - Environment $environmentName
221+ Write-Host " ##[command]Add-AzureRMAccount -ServicePrincipal -Tenant $ ( $Endpoint.Auth.Parameters.TenantId ) -Credential $psCredential -Environment $environmentName @processScope "
222+ $null = Add-AzureRMAccount - ServicePrincipal - Tenant $Endpoint.Auth.Parameters.TenantId - Credential $psCredential - Environment $environmentName @processScope
204223 }
205224 }
206225 }
207226 else {
227+ If (CmdletHasMember - cmdlet " Connect-AzureRMAccount" - memberName " Scope" )
228+ {
229+ $processScope = @ { Scope = " Process" }
230+ }
231+ else
232+ {
233+ $processScope = @ {}
234+ }
235+
208236 if ($Endpoint.Auth.Parameters.AuthenticationType -eq " SPNCertificate" ) {
209- Write-Host " ##[command]Connect-AzureRMAccount -ServicePrincipal -Tenant $ ( $Endpoint.Auth.Parameters.TenantId ) -CertificateThumbprint ****** -ApplicationId $ ( $Endpoint.Auth.Parameters.ServicePrincipalId ) -Environment $environmentName "
210- $null = Connect-AzureRmAccount - ServicePrincipal - Tenant $Endpoint.Auth.Parameters.TenantId - CertificateThumbprint $servicePrincipalCertificate.Thumbprint - ApplicationId $Endpoint.Auth.Parameters.ServicePrincipalId - Environment $environmentName
237+ Write-Host " ##[command]Connect-AzureRMAccount -ServicePrincipal -Tenant $ ( $Endpoint.Auth.Parameters.TenantId ) -CertificateThumbprint ****** -ApplicationId $ ( $Endpoint.Auth.Parameters.ServicePrincipalId ) -Environment $environmentName @processScope "
238+ $null = Connect-AzureRmAccount - ServicePrincipal - Tenant $Endpoint.Auth.Parameters.TenantId - CertificateThumbprint $servicePrincipalCertificate.Thumbprint - ApplicationId $Endpoint.Auth.Parameters.ServicePrincipalId - Environment $environmentName @processScope
211239 }
212240 else {
213- Write-Host " ##[command]Connect-AzureRMAccount -ServicePrincipal -Tenant $ ( $Endpoint.Auth.Parameters.TenantId ) -Credential $psCredential -Environment $environmentName "
214- $null = Connect-AzureRMAccount - ServicePrincipal - Tenant $Endpoint.Auth.Parameters.TenantId - Credential $psCredential - Environment $environmentName
241+ Write-Host " ##[command]Connect-AzureRMAccount -ServicePrincipal -Tenant $ ( $Endpoint.Auth.Parameters.TenantId ) -Credential $psCredential -Environment $environmentName @processScope "
242+ $null = Connect-AzureRMAccount - ServicePrincipal - Tenant $Endpoint.Auth.Parameters.TenantId - Credential $psCredential - Environment $environmentName @processScope
215243 }
216244 }
217245 }
@@ -643,24 +671,22 @@ function Disconnect-AzureAndClearContext {
643671 if ($authScheme -eq ' ServicePrincipal' ) {
644672 Write-Verbose " Trying to disconnect from Azure and clear context"
645673
646- if (Get-Command - Name " Disconnect-AzureRmAccount" - ErrorAction " SilentlyContinue" ) {
647- Write-Host " ##[command]Disconnect-AzureRmAccount"
648- $null = Disconnect-AzureRmAccount
674+ if (Get-Command - Name " Disconnect-AzureRmAccount" - ErrorAction " SilentlyContinue" -and CmdletHasMember - cmdlet Disconnect-AzureRmAccount - memberName Scope ) {
675+ Write-Host " ##[command]Disconnect-AzureRmAccount -Scope Process -ErrorAction Stop "
676+ $null = Disconnect-AzureRmAccount - Scope Process - ErrorAction Stop
649677 }
650- elseif (Get-Command - Name " Remove-AzureRmAccount" - ErrorAction " SilentlyContinue" ) {
651- Write-Host " ##[command]Remove-AzureRmAccount"
652- $null = Remove-AzureRmAccount
678+ elseif (Get-Command - Name " Remove-AzureRmAccount" - ErrorAction " SilentlyContinue" -and CmdletHasMember - cmdlet Remove-AzureRmAccount - memberName Scope ) {
679+ Write-Host " ##[command]Remove-AzureRmAccount -Scope Process -ErrorAction Stop "
680+ $null = Remove-AzureRmAccount - Scope Process - ErrorAction Stop
653681 }
654- elseif (Get-Command - Name " Logout-AzureRmAccount" - ErrorAction " SilentlyContinue" ) {
655- Write-Host " ##[command]Logout-AzureRmAccount"
656- $null = Logout- AzureRmAccount
682+ elseif (Get-Command - Name " Logout-AzureRmAccount" - ErrorAction " SilentlyContinue" -and CmdletHasMember - cmdlet Logout - AzureRmAccount - memberName Scope ) {
683+ Write-Host " ##[command]Logout-AzureRmAccount -Scope Process -ErrorAction Stop "
684+ $null = Logout- AzureRmAccount - Scope Process - ErrorAction Stop
657685 }
658686
659687 if (Get-Command - Name " Clear-AzureRmContext" - ErrorAction " SilentlyContinue" ) {
660- Write-Host " ##[command]Clear-AzureRmContext -Scope Process"
661- $null = Clear-AzureRmContext - Scope Process
662- Write-Host " ##[command]Clear-AzureRmContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue"
663- $null = Clear-AzureRmContext - Scope CurrentUser - Force - ErrorAction SilentlyContinue
688+ Write-Host " ##[command]Clear-AzureRmContext -Scope Process -Scope Process -ErrorAction Stop"
689+ $null = Clear-AzureRmContext - Scope Process - ErrorAction Stop
664690 }
665691 }
666692 } catch {
0 commit comments