首页 技术 正文
技术 2022年11月10日
0 收藏 632 点赞 4,876 浏览 3240 个字

问题描述

使用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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,494
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297