You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure DevOps发布管道中用命令删除Kudu站点文件夹文件

解决Kudu API执行删除复制命令的参数错误问题

问题分析

你遇到的A positional parameter cannot be found that accepts argument 'C:\home\site\wwwroot\License\*'错误,本质是PowerShell解析哈希表时的语法问题:$bodyToPOST里的command字段没有将整个命令作为字符串包裹,导致PowerShell误将del当作独立命令执行,而非赋值给command键的字符串值。

修复方案

修正命令字符串格式

将整个命令用单引号包裹,同时使用cmd /c明确指定在CMD环境下执行命令链(因为del和xcopy是CMD命令),并用&&确保删除成功后再执行复制操作(替代原管道符,符合先删后复制的逻辑)。

完整修正后的脚本

$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 = 'cmd /c "del /Q C:\home\site\wwwroot\License\* && xcopy C:\home\License\*.* C:\home\site\wwwroot\License\ /Y"'
    dir = "C:\home\"  
}  

# Splat all parameters together in $param  
$param = @{  
    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

关键修改点说明

  1. 命令包裹方式:用单引号将整个command值包裹,避免PowerShell解析内部命令为独立操作。
  2. CMD环境执行:添加cmd /c确保命令在CMD shell中运行,适配del、xcopy这类CMD原生命令。
  3. 命令链逻辑:用&&替代管道符|,只有删除命令执行成功后才会触发复制操作,更符合业务需求。
  4. xcopy参数优化:添加/Y参数,避免复制时弹出覆盖确认提示,实现无交互执行。

内容的提问来源于stack exchange,提问作者Vaishnavi Vaishu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 19:33:18