Search code examples
azurepowershellaz

Forced Conversion from AzureRM to AZ powershell


We have found that our AzureRM scripts have started to fail with Request to a Error downlevel service failed. This has forced us to change our scripts to start using the AZ powershell module, https://learn.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-1.6.0. The conversion has worked really well except I haven't found the replacement for New-AzureWebsiteJob. Has anyone else run into this?


Solution

  • For New-AzureWebsiteJob cmdlet, there is no direct equivalent in the Az or ARM PowerShell Cmdlets.

    You can follow this blog to achieve your purpose, and note that if you are using Az powershell module, please modify ARM Powershell to Az powershell respectively.

    Sample code for Az powershell like below:

    #Resource details :
    $resourceGroupName = "<Resourcegroup name>";
    $webAppName = "<WebApp name>";
    $Apiversion = 2015-08-01
    
    #Function to get Publishing credentials for the WebApp :
    function Get-PublishingProfileCredentials($resourceGroupName, $webAppName){
    
    $resourceType = "Microsoft.Web/sites/config"
    $resourceName = "$webAppName/publishingcredentials"
    $publishingCredentials = Invoke-AzResourceAction -ResourceGroupName $resourceGroupName -ResourceType 
    $resourceType -ResourceName $resourceName -Action list -ApiVersion $Apiversion -Force
       return $publishingCredentials
    }
    
    #Pulling authorization access token :
    function Get-KuduApiAuthorisationHeaderValue($resourceGroupName, $webAppName){
    
    $publishingCredentials = Get-PublishingProfileCredentials $resourceGroupName $webAppName
    return ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f 
    $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))))
    }
    
    $accessToken = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppname
    #Generating header to create and publish the Webjob :
    $Header = @{
    'Content-Disposition'='attachment; attachment; filename=Copy.zip'
    'Authorization'=$accessToken
            }
    $apiUrl = "https://$webAppName.scm.azurewebsites.net/api/<Webjob-type>/<Webjob-name>"
    $result = Invoke-RestMethod -Uri $apiUrl -Headers $Header -Method put -InFile "<Complete path of the file>\
    <filename>.zip" -ContentType 'application/zip'