如何使用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]" }
操作步骤
- 将脚本中的三个变量
$tempFolder、$resourceGroupName、$webappName替换为你的实际信息 - 打开PowerShell,先执行登录命令完成Azure账号验证
- 粘贴完整脚本并运行,脚本会自动遍历目标本地文件夹,将所有文件上传到App Service的
site/bin目录
内容的提问来源于stack exchange,提问作者Stacey
相关产品推荐
相关产品推荐

