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

如何使用PowerShell将文件复制到Azure App Service的bin目录?

自动化将文件上传到Azure App Service的bin目录

前置准备

  • 安装Azure PowerShell模块:执行 Install-Module -Name Az -AllowClobber -Scope CurrentUser
  • 登录Azure账号:运行 Connect-AzAccount

完整可执行脚本

你提供的代码依赖两个自定义函数,先补上这两个函数,并指定上传路径为bin目录:

# 获取Kudu API授权头
function Get-KuduApiAuthorisationHeaderValue($resourceGroupName, $webAppName) {
    $webApp = Get-AzWebApp -Name $webAppName -ResourceGroupName $resourceGroupName
    $publishingCredentials = Get-AzWebAppPublishingProfile -WebApp $webApp -OutputFile null
    $username = $publishingCredentials.Properties.PublishingUserName
    $password = $publishingCredentials.Properties.PublishingPassword
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))
    return "Basic $base64AuthInfo"
}

# 上传文件到App Service的bin目录
function Upload-FileToWebApp($accessToken, $webAppName, $fileName, $localFilePath) {
    $kuduApiUrl = "https://$webAppName.scm.azurewebsites.net/api/vfs/site/bin/$fileName"
    Invoke-RestMethod -Uri $kuduApiUrl -Headers @{Authorization = $accessToken} -Method Put -InFile $localFilePath -ContentType "multipart/form-data"
}

# 配置自定义变量
$tempFolder = "C:\本地文件存放路径"  # 替换为你要上传文件的本地文件夹路径
$resourceGroupName = "你的资源组名称"    # 替换为App Service所属的资源组名称
$webappName = "你的App Service名称"      # 替换为目标App Service的名称

# 批量执行上传
$localFiles = Get-ChildItem $tempFolder
$accessToken = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webappName
 
$localFiles | % {
    Write-Host "Uploading $($_.Name)" -NoNewline
    Upload-FileToWebApp $accessToken $webappName $_.Name $_.FullName 
    Write-Host -f Green " [Done]"
}

操作步骤

  1. 将脚本中的三个变量$tempFolder、$resourceGroupName、$webappName替换为你的实际信息
  2. 打开PowerShell,先执行登录命令完成Azure账号验证
  3. 粘贴完整脚本并运行,脚本会自动遍历目标本地文件夹,将所有文件上传到App Service的site/bin目录

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 18:22:34