【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.)://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 : (//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=//$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: //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? : //stackoverflow.com/questions/45155581/how-to-deploy-web-app-zip-package-to-azure-using-msdeploy-from-powershell

使用 FTP 將文件上傳到 Web 應用//docs.azure.cn/zh-cn/app-service/scripts/powershell-deploy-ftp?toc=%2Fpowershell%2Fmodule%2Ftoc.json&view=azs-2102