【Azure 应用服务】使用PowerShell脚本上传文件至App Service目录  

简介: 【Azure 应用服务】使用PowerShell脚本上传文件至App Service目录  

问题描述

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

相关文章
|
20天前
|
应用服务中间件 Linux 网络安全
【Azure 应用服务】App Service for Linux 环境中为Tomcat页面修改默认的Azure 404页面
【Azure 应用服务】App Service for Linux 环境中为Tomcat页面修改默认的Azure 404页面
|
20天前
【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded.
【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded.
|
20天前
【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-AzContext' is not recognized as the name
【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-AzContext' is not recognized as the name
|
20天前
|
存储 C# Python
【Azure Storage Account】Azure 存储服务计算Blob的数量和大小的PowerShell代码
【Azure Storage Account】Azure 存储服务计算Blob的数量和大小的PowerShell代码
|
20天前
|
Ubuntu Linux 测试技术
【Azure Function App】Python Function调用Powershell脚本在Azure上执行失败的案例
【Azure Function App】Python Function调用Powershell脚本在Azure上执行失败的案例
|
20天前
|
Docker 容器
【Azure 应用服务】App Service for Container 无法拉取Docker Hub中的镜像替代方案
【Azure 应用服务】App Service for Container 无法拉取Docker Hub中的镜像替代方案
|
10月前
|
Shell Linux 开发工具
windows中cmd和PowerShell批处理命令
之前在 Git 批量删除本地分支,有用到 Linux 或 MacOS 下的批处理命令,这个命令中的 grep、xargs 本身是 Shell script,在 windows 中的 cmd 和 PowerShell 中是不能用的
77 0
|
JavaScript Windows
[Vue]解决 Windows PowerShell 不识别 vue 命令的问题
[Vue]解决 Windows PowerShell 不识别 vue 命令的问题
|
Windows
使用PowerShell获取Windows当前锁屏壁纸
使用PowerShell获取Windows当前锁屏壁纸 如果原始图片丢了,用这段代码就可以提取当前锁屏壁纸了!
159 0
|
应用服务中间件 nginx Windows
Windows PowerShell 中启动 Nginx 报错解决方案
Windows PowerShell 中启动 Nginx 报错解决方案
Windows PowerShell 中启动 Nginx 报错解决方案