Search code examples
azureazure-devopsazure-web-app-serviceazure-pipelinesazure-pipelines-release-pipeline

How to remove files from the kudu site folder using command in the Azure devops release pipeline


Context: After deployment, remove the files from the specific folder in the Kudu Site Console.

enter image description here

I have used a script syntax given in my former question but the command is different here.

I tried with the following commands but didn't worked:

del /Q "C:\home\site\wwwroot\License\*"
Remove-Item -Path 'C:\home\site\wwwroot\License\*' -force

Error: A positional parameter cannot be found that accepts argument 'C:\home\site\wwwroot\License\*'.

Total Script:

$WebApp = Get-AzWebApp -ResourceGroupName "AppServPlan-RG" -Name "testwebapp1013"
$publishingProfile = [xml](Get-AzWebAppPublishingProfile -WebApp $WebApp)
# Create Base64 authorization header
$Username = (Select-Xml -Xml $PublishingProfile -XPath "//publishData/publishProfile[contains(@profileName,'Web Deploy')]/@userName").Node.Value
$Password = (Select-Xml -Xml $PublishingProfile -XPath "//publishData/publishProfile[contains(@profileName,'Web Deploy')]/@userPWD").Node.Value
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Username,$Password)))


$bodyToPOST = @{  
command = del /Q "C:\home\site\wwwroot\License\*" | XCOPY C:\home\License\*.* C:\home\site\wwwroot\License\
                 dir = "C:\home\"  
}  
# Splat all parameters together in $param  
$param = @{  
            # command REST API url  
            Uri = "https://testwebapp1013.scm.azurewebsites.net/api/command"  
            Headers = @{Authorization=("Basic {0}" -f $base64AuthInfo)}  
            UserAgent = "powershell/1.0"  
            Method = "POST"  
            Body = (ConvertTo-Json $bodyToPOST)  
            ContentType = "application/json"  
}  
# Invoke REST call  
Invoke-RestMethod @param

Solution

  • I can reproduce the same issue when using your sample:

    enter image description here

    To Solve this issue, you can use the following sample:

    $WebApp = Get-AzWebApp -ResourceGroupName "AppServPlan-RG" -Name "testwebapp1013"
    $publishingProfile = [xml](Get-AzWebAppPublishingProfile -WebApp $WebApp)
    # Create Base64 authorization header
    $Username = (Select-Xml -Xml $PublishingProfile -XPath "//publishData/publishProfile[contains(@profileName,'Web Deploy')]/@userName").Node.Value
    $Password = (Select-Xml -Xml $PublishingProfile -XPath "//publishData/publishProfile[contains(@profileName,'Web Deploy')]/@userPWD").Node.Value
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Username,$Password)))
    
    
    $bodyToPOST = @{  
    command = "del /Q C:\home\site\wwwroot\License\* | XCOPY C:\home\License\*.* C:\home\site\wwwroot\License\"
                     dir = "C:\home\"  
    }  
    # Splat all parameters together in $param  
    $param = @{  
                # command REST API url  
                Uri = "https://testwebapp1013.scm.azurewebsites.net/api/command"  
                Headers = @{Authorization=("Basic {0}" -f $base64AuthInfo)}  
                UserAgent = "powershell/1.0"  
                Method = "POST"  
                Body = (ConvertTo-Json $bodyToPOST)  
                ContentType = "application/json"  
    }  
    # Invoke REST call  
    Invoke-RestMethod @param
    

    You need to add double quotes to the entire command.

    From

    command = del /Q "C:\home\site\wwwroot\License\*" | XCOPY C:\home\License\*.* C:\home\site\wwwroot\License\
    

    To

    command = "del /Q C:\home\site\wwwroot\License\* | XCOPY C:\home\License\*.* C:\home\site\wwwroot\License\"
    

    Result sample:

    enter image description here