powershell上传文件到服务器,PowerShell:使用 FTP 上传文件 - Azure App Service | Azure Docs...

使用 FTP 将文件上传到 Web 应用

12/21/2020

本文内容

此示例脚本使用其相关资源,在应用服务中创建 Web 应用,并使用 FTP(通过 WebClient.UploadFile())部署 Web 应用代码。

必要时,请使用 Azure PowerShell 指南中的说明安装 Azure PowerShell,并运行 Connect-AzAccount -Environment AzureChinaCloud 创建与 Azure 的连接。

示例脚本

备注

本文已经过更新,以便使用 Azure Az PowerShell 模块。 若要与 Azure 交互,建议使用的 PowerShell 模块是 Az PowerShell 模块。 若要开始使用 Az PowerShell 模块,请参阅安装 Azure PowerShell。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az。

$appdirectory=""

$webappname="mywebapp$(Get-Random)"

$location="China North"

# Create a resource group.

New-AzResourceGroup -Name myResourceGroup -Location $location

# Create an App Service plan in `Free` tier.

New-AzAppServicePlan -Name $webappname -Location $location `

-ResourceGroupName myResourceGroup -Tier Free

# Create a web app.

New-AzWebApp -Name $webappname -Location $location -AppServicePlan $webappname `

-ResourceGroupName myResourceGroup

# Get publishing profile for the web app

$xml = [xml](Get-AzWebAppPublishingProfile -Name $webappname `

-ResourceGroupName myResourceGroup `

-OutputFile null)

# Extract connection information from publishing profile

$username = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@userName").value

$password = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@userPWD").value

$url = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@publishUrl").value

# Upload files recursively

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()

清理部署

运行脚本示例后,可以使用以下命令删除资源组、Web 应用以及所有相关资源。

Remove-AzResourceGroup -Name myResourceGroup -Force

脚本说明

此脚本使用以下命令。 表中的每条命令均链接到特定于命令的文档。

后续步骤

有关 Azure PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档。

可以在 Azure PowerShell 示例中找到 Azure 应用服务 Web 应用的其他 Azure Powershell 示例。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值