【问题标题】:How to delete Azure app service folder using powershell or any script before the Azure Devops deployment如何在 Azure Devops 部署之前使用 powershell 或任何脚本删除 Azure 应用服务文件夹
【发布时间】:2019-09-20 12:52:15
【问题描述】:

你能帮我解决下面的问题吗 1.我已经在 Azure DevOps 中配置了 CI 2.在 CD 中,我需要一个 Powerhell 或任何脚本来在部署之前删除 Azure App 服务文件夹。 3.我也知道工藤的流程,但是这个流程是手动的。

【问题讨论】:

    标签: powershell directory delete-file azure-web-app-service


    【解决方案1】:

    $WebAppName = “insert Web App name”
    $slotName = “insert Slot Name”
    $username = ‘insert the username from the publish profile’ #From the publish profile
    $password = “insert the password from the publish profile” #From the publish profile
    # Initialize parameters for Invoke-RestMethod
    $apiUrl = “https://$WebAppName.scm.azurewebsites.net/api/vfs/site/wwwroot/”
    if ($slotName -ne “”){
        $apiUrl = “https://$webAppName`-$slotName.scm.azurewebsites.net/api/vfs/site/wwwroot/”
    }
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes((“$($username):$($password)”)))
    $headers = @{
        Authorization=“Basic $($base64AuthInfo)”
        ‘If-Match’ = ‘*’
    }
    $userAgent = “powershell/2.0”
    # Define a reursive function to delete files
    function DeleteKuduDir ($content, $dir)
    {
        foreach($c in $content)
        {      
            if($c.mime -eq “inode/directory”)
            {
                # Get listing of directory as an array
                $childContent = Invoke-RestMethod –Uri $c.href –Headers $headers –UserAgent $userAgent –Method GET –ContentType “application/json”
               
                # Delete directory
                $newDir = $dir + (Split-Path $c.href –leaf) + “\”
                DeleteKuduDir –content $childContent –dir $newDir
            }
            # Delete file
            $file = Split-Path $c.href –leaf
            Write-Host “Deleting” $dir$file    
            $result = Invoke-RestMethod –Uri $c.href –Headers $headers –UserAgent $userAgent –Method DELETE –ContentType “application/json”
        }
    }
    # Get listing of wwwroot as an array
    $rootContent = Invoke-RestMethod –Uri $apiUrl –Headers $headers –UserAgent $userAgent –Method GET –ContentType “application/json”
    # Delete files and directory in wwwroot
    DeleteKuduDir –content $rootContent –dir “\”
    Write-Host “Done!”

    【讨论】:

    • 我遇到以下错误 Invoke-RestMethod : 401 - Unauthorized: 由于凭据无效,访问被拒绝。服务器错误 401 - 未经授权:由于凭据无效,访问被拒绝。您无权使用您提供的凭据查看此目录或页面。在 line:33 char:16
    • + ... otContent = Invoke-RestMethod –Uri $apiUrl –Headers $headers –UserAge ... + ~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest: HttpWebRequest)[Invoke-RestMethod],WebException + FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand 拆分路径:无法将参数绑定到参数“路径”,因为它为空。在 line:27 char:28
    • + CategoryInfo : InvalidData: (:) [Split-Path], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SplitPathCommand 删除 \ Invoke-RestMethod : 无法验证参数“Uri”上的参数。参数为 null 或空。提供一个不为 null 或空的参数,然后重试该命令。在 line:29 char:42
    • + $result = Invoke-RestMethod –Uri $c.href –Headers $headers –U ... + ~~~~~~~ + CategoryInfo : InvalidData: (:) [Invoke-RestMethod ], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
    猜你喜欢
    • 2020-12-09
    • 2020-01-16
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2019-08-02
    • 2021-08-01
    • 2021-11-04
    相关资源
    最近更新 更多