问题描述
使用PowerShell脚本上传文件至App Service目录的示例
脚本示例
对文件进行上传,使用的 WebClient.UploadFile 方法进行上传。当文件夹中包含子目录,执行以下脚本就会报错。
$url="ftp://cnws-prod-xxxxx-00x.ftp.chinacloudsites.chinacloudapi.cn/site/wwwroot/" $webappname="your web app name" $username="your web app name\$web app name" $password="xxxxxxxxxxxx" $appdirectory="C:\WebSite" ##local directory name Set-Location $appdirectory $webclient = New-Object -TypeName System.Net.WebClient $webclient.Credentials = New-Object System.Net.NetworkCredential($username,$password) $files = Get-ChildItem -Path $appdirectory -Recurse | Where-Object{!($_.PSIsContainer)} foreach ($file in $files) { $relativepath = (Resolve-Path -Path $file.FullName -Relative).Replace(".\", "").Replace('\', '/') $uri = New-Object System.Uri("$url/$relativepath") "Uploading to " + $uri.AbsoluteUri $webclient.UploadFile($uri, $file.FullName) } $webclient.Dispose()
- url,username, $password 等信息都可以在App Service的Overview页面通过 Get Publish Profile 获取
通过以上代码,上传文件到App Service的时候,如果遇见存在子目录时候,可以先将子目录压缩为一个文件,等上传到App Service后,登录Kudu高级管理工具后,通过 unzip 解压到指定目录。如:
其他的PowerShell方式:
1) 使用 Publish-AzWebApp (Deploys an Azure Web App from a ZIP, JAR, or WAR file using zipdeploy.):https://docs.microsoft.com/en-us/powershell/module/az.websites/publish-azwebapp?view=azps-6.3.0&viewFallbackFrom=azps-6.1.0
Publish-AzWebApp -ArchivePath <String> [-AsJob] [-ResourceGroupName] <String> [-Name] <String> [[-Slot] <String>] [-Force] [-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
2) 使用Msdeploy : (https://stackoverflow.com/questions/45155581/how-to-deploy-web-app-zip-package-to-azure-using-msdeploy-from-powershell)
$PackagePath = "c:\temp\package.zip" $ResourceGroupName = "resource-group-where-my-app-resides" $AppName = "my-cool-web-app" if (!(Test-Path $PackagePath)) { throw "Package file $PackagePath does not exist" } echo "Getting publishing profile for $AppName app" $xml = Get-AzureRmWebAppPublishingProfile -Name $AppName ` -ResourceGroupName $ResourceGroupName ` -OutputFile temp.xml -Format WebDeploy -ErrorAction Stop $username = ([xml]$xml).SelectNodes("//publishProfile[@publishMethod=`"MSDeploy`"]/@userName").value $password = ([xml]$xml).SelectNodes("//publishProfile[@publishMethod=`"MSDeploy`"]/@userPWD").value $url = ([xml]$xml).SelectNodes("//publishProfile[@publishMethod=`"MSDeploy`"]/@publishUrl").value $siteName = ([xml]$xml).SelectNodes("//publishProfile[@publishMethod=`"MSDeploy`"]/@msdeploySite").value del temp.xml echo "Got publishing profile XML." $msdeployArguments = '-verb:sync ' + "-source:package='$PackagePath' " + "-dest:auto,ComputerName=https://$url/msdeploy.axd?site=$siteName,UserName=$username,Password=$password,AuthType='Basic',includeAcls='False' " + "-setParam:name='IIS Web Application Name',value=$siteName" $commandLine = '&"C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe" --% ' + $msdeployArguments Invoke-Expression $commandLine
3) 通过PowerShell加载 WinSCPnet.dll 的连接 FTPS,使用其中的 session.putFiles 的方法可以传递子目录。
参考资料
Publish-AzWebApp: https://docs.microsoft.com/en-us/powershell/module/az.websites/publish-azwebapp?view=azps-6.3.0&viewFallbackFrom=azps-6.1.0
How to deploy web app zip package to Azure using MSDeploy from Powershell? : https://stackoverflow.com/questions/45155581/how-to-deploy-web-app-zip-package-to-azure-using-msdeploy-from-powershell
使用 FTP 将文件上传到 Web 应用: https://docs.azure.cn/zh-cn/app-service/scripts/powershell-deploy-ftp?toc=%2Fpowershell%2Fmodule%2Ftoc.json&view=azs-2102