How to access Kudu in Azure using power shell script

前端 未结 3 595
执念已碎
执念已碎 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条回答
  •  -上瘾入骨i
    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
        }
    

提交回复
热议问题