Skip to main content

 none
Azure Devops slot swap cloud service classic RRS feed

  • Question

  • Hello,

    need to automate slot swap on cloud classic, can find way to do it,

    anybody know how to do it in azure devops? No rest api solution plz

    Monday, October 21, 2019 6:48 PM

All replies

  • I would suggest asking the DevOps community instead 

    https://developercommunity.visualstudio.com/spaces/21/index.html

    If any Cloud Service users have an idea they can post here but you will likely have better luck in the DevOps forums. 

    The REST APIs for cloud devices can be found here: 

    https://docs.microsoft.com/en-us/rest/api/compute/cloudservices/

    Specifically the swap API

    https://docs.microsoft.com/en-us/rest/api/compute/cloudservices/rest-swap-deployment

    Monday, October 21, 2019 7:36 PM
    Moderator
  • provide me an example how to use REST API with powershell, cuz right after authorization 

    invoke-request tells me that access forbbiden, manually I can swap it at same account, so wtf?

    Connect-AzureRmAccount tells me that authorization is good

    Monday, October 21, 2019 7:40 PM
  • I believe you need to use the Invoke-RestMethod instead

    https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Invoke-RestMethod?view=powershell-5.1&redirectedfrom=MSDN

    Also, you should consider updating your PowerShell module. AzureRM is deprecated and replaced with Az

    https://docs.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-2.8.0

    You also want to make sure if you have multiple subscriptions you set the context for the sub you are using. 

    Select-AzSubscription -Subscription XXXX_XXXXX

    Monday, October 21, 2019 7:46 PM
    Moderator
  • yes invoke-rest my bad in my previous message, its not worked, seems it's need some authorization headers or something
    Monday, October 21, 2019 8:03 PM
  • You could try adding the -credential into the inputs. 

    $cred = Get-Credential 

    Enter the username and password for your azure subscription

    Then retry Invoke-RestMethod -$cred

    Monday, October 21, 2019 8:09 PM
    Moderator
  • it will not work, here is some example which seems will work, but cloud classic not have several of these parameters

    # Variables
    $TenantId = "" # Enter Tenant Id.
    $ClientId = "" # Enter Client Id.
    $ClientSecret = "" # Enter Client Secret.
    $Resource = "https://management.core.windows.net/"
    $SubscriptionId = "" # Enter Subscription Id.
    
    $RequestAccessTokenUri = "https://login.microsoftonline.com/$TenantId/oauth2/token"
    
    $body = "grant_type=client_credentials&client_id=$ClientId&client_secret=$ClientSecret&resource=$Resource"
    
    $Token = Invoke-RestMethod -Method Post -Uri $RequestAccessTokenUri -Body $body -ContentType 'application/x-www-form-urlencoded'
    
    Write-Host "Print Token" -ForegroundColor Green
    Write-Output $Token
    
    # Get Azure Resource Groups
    $ResourceGroupApiUri = "https://management.azure.com/subscriptions/$SubscriptionId/resourcegroups?api-version=2017-05-10"
    
    $Headers = @{}
    
    $Headers.Add("Authorization","$($Token.token_type) "+ " " + "$($Token.access_token)")
    
    $ResourceGroups = Invoke-RestMethod -Method Get -Uri $ResourceGroupApiUri -Headers $Headers

    Monday, October 21, 2019 8:36 PM
  • Oh actually, I think I know the issue. 

    You are connected using AzureRM. Which is only for ARM resources. Not Classic. So it makes sense it cannot find it. You need to connect to the classic resources instead

    https://docs.microsoft.com/en-us/powershell/module/servicemanagement/azure/add-azureaccount?view=azuresmps-4.0.0

    Open a PowerShell Window and run the following

    Add-AzureAccount

    If you have more than one subscription run

    Select-AzureSubscription -SubscriptionName XXXX

    once you connect try the invoke command as you had tried to begin with. 

    Monday, October 21, 2019 8:44 PM
    Moderator
  • nope, still same, 403

    $password = ConvertTo-SecureString '$(password)' -AsPlainText -Force
    $credential = New-Object System.Management.Automation.PSCredential ('login@loginmail', $password)

    Add-AzureAccount -Credential $credential

    Invoke-RestMethod -Uri https://management.core.windows.net/<subs_digits>/services/hostedservices -Method GET

    also I did it in azure powershell where I can choose subscription and az cli working good 
    • Edited by ViktorI86 Monday, October 21, 2019 9:25 PM
    Monday, October 21, 2019 9:24 PM
  • Tried, not work.I tried Invoke-RestMethod -Credentials $credential not work too, 403 error in every time
    Tuesday, November 5, 2019 5:48 AM