How to access Kudu in Azure using power shell script

前端 未结 3 594
执念已碎
执念已碎 2020-12-04 03:51

I am trying to access Kudu through power shell script. Link looks like: https://adc-dev.scm.azurewebsites.net. I need to copy war file located in <

相关标签:
3条回答
  • 2020-12-04 04:20

    You can refer to this thread below to know how to call Kudu API through Azure PowerShell in VSTS build/release:

    Remove files and foldes on Azure before a new deploy from VSTS

    Regarding copy file through Kudu, you can use Command Kudu API (Post /api/command):

    Kudu REST API

    Update:

    Simple sample to call Command through Kudu API:

      function RunCommand($dir,$command,$resourceGroupName, $webAppName, $slotName = $null){
            $kuduApiAuthorisationToken = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName
            $kuduApiUrl="https://$webAppName.scm.azurewebsites.net/api/command"
            $Body = 
              @{
              "command"=$command;
               "dir"=$dir
               } 
            $bodyContent=@($Body) | ConvertTo-Json
            Write-Host $bodyContent
             Invoke-RestMethod -Uri $kuduApiUrl `
                                -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} `
                                -Method POST -ContentType "application/json" -Body $bodyContent
        }
    
    
    RunCommand "site\wwwroot\bin\apache-tomcat-8.0.33\webapps" "copy xx.war ..\xx.war /y" "[resource group]" "[web app]"
    
    0 讨论(0)
  • 2020-12-04 04:40

    You can use below code to access kudu apis from powershell -

     //function to Get webapp's publishing credentials    
        function Get-AzWebAppPublishingCredentials($resourceGroupName, $webAppName, $slotName = $null) {
                if ([string]::IsNullOrWhiteSpace($slotName) -or $slotName.ToLower() -eq "production") {
                    $resourceType = "Microsoft.Web/sites/config"
                    $resourceName = "$webAppName/publishingcredentials"
                }
                else {
                    $resourceType = "Microsoft.Web/sites/slots/config"
                    $resourceName = "$webAppName/$slotName/publishingcredentials"
                }
                $publishingCredentials = Invoke-AzResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force
                return $publishingCredentials
        }
    
     //function to get authorization header from publishing credentials
         function Get-KuduApiAuthorisationHeaderValue($resourceGroupName, $webAppName, $slotName = $null) {
                $publishingCredentials = Get-AzWebAppPublishingCredentials $resourceGroupName $webAppName $slotName
                $ret = @{ }
                $ret.header = ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))))
                $ret.url = $publishingCredentials.Properties.scmUri
                return $ret
            }
    
     //function to call kudu api e.g. to get a file from webapp
        function Get-FileFromWebApp($resourceGroupName, $webAppName, $slotName = "", $kuduPath) {
            $KuduAuth = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName
            $kuduApiAuthorisationToken = $KuduAuth.header
            $kuduApiUrl = $KuduAuth.url + "/api/vfs/$kuduPath"
    
            Write-Host " Downloading File from WebApp. Source: '$kuduApiUrl'." -ForegroundColor DarkGray
            $tmpPath = "$($env:TEMP)\$([guid]::NewGuid()).json"
            $null = Invoke-RestMethod -Uri $kuduApiUrl `
                -Headers @{"Authorization" = $kuduApiAuthorisationToken; "If-Match" = "*" } `
                -Method GET `
                -ContentType "application/json" `
                -OutFile $tmpPath
            $ret = (Get-Content $tmpPath) | Out-String | ConvertFrom-Json
            Remove-Item $tmpPath -Force
            return $ret
        }
    
    0 讨论(0)
  • 2020-12-04 04:42

    To access Kudu API, get your WebApp:

    $app = Get-AzWebApp -ResourceGroupName "your RG" -Name "your App"
    

    Then get publishing credentials for the app:

    $resourceName = "$($app.Name)/publishingcredentials";
    $resourceType = "Microsoft.Web/sites/config";
    $publishingCredentials = Invoke-AzResourceAction `
            -ResourceGroupName $app.ResourceGroup `
            -ResourceType $resourceType `
            -ResourceName $resourceName `
            -Action list `
            -ApiVersion $apiVersion `
            -Force;
    

    Format the username/password suitable for a HTTP-request:

    $user = $publishingCredentials.Properties.PublishingUserName;
    $pass = $publishingCredentials.Properties.PublishingPassword;
    $creds = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("${user}:${pass}")));
    

    Finally, you can access the Kudu API:

    $header = @{
        Authorization = "Basic $creds"
    };
    $kuduApiBaseUrl = "https://$($app.Name).scm.azurewebsites.net";
    

    Example, check if extension is installed:

    $extensionName = "Microsoft.AspNetCore.AzureAppServices.SiteExtension";
    $kuduApiUrl = "$kuduApiBaseUrl/api/siteextensions/$extensionName";
    $response = Invoke-RestMethod -Method 'Get' -Uri $kuduApiUrl -Headers $header;
    

    Example, get list of available extensions:

    $kuduApiUrl = "$kuduApiBaseUrl/api/extensionfeed";
    $response = Invoke-RestMethod -Method 'Get' -Uri $kuduApiUrl -Headers $header;
    

    Example, install an extension:

    $kuduApiUrl = "$kuduApiBaseUrl/api/siteextensions";
    $response = Invoke-RestMethod -Method 'Put' -Uri $kuduApiUrl -Headers $header;
    

    API-details are at https://github.com/projectkudu/kudu/wiki/REST-API

    Also deployment slots can be accessed. App config needs to be retrieved for the slot and a minor modification of the base URL is needed.

    0 讨论(0)
提交回复
热议问题