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

您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

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

03/20/2017

本文内容

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

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

示例脚本

备注

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

$filePath=""

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

$location="West Europe"

# 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 file

$file = Get-Item -Path $filePath

$uri = New-Object System.Uri("$url/$($file.Name)")

$request = [System.Net.FtpWebRequest]([System.net.WebRequest]::Create($uri))

$request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile

$request.Credentials = New-Object System.Net.NetworkCredential($username,$password)

# Enable SSL for FTPS. Should be $false if FTP.

$request.EnableSsl = $true;

# Write the file to the request object.

$fileBytes = [System.IO.File]::ReadAllBytes($filePath)

$request.ContentLength = $fileBytes.Length;

$requestStream = $request.GetRequestStream()

try {

$requestStream.Write($fileBytes, 0, $fileBytes.Length)

}

finally {

$requestStream.Dispose()

}

Write-Host "Uploading to $($uri.AbsoluteUri)"

try {

$response = [System.Net.FtpWebResponse]($request.GetResponse())

Write-Host "Status: $($response.StatusDescription)"

}

finally {

if ($null -ne $response) {

$response.Close()

}

}

清理部署

运行脚本示例后,可以使用以下命令删除资源组、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、付费专栏及课程。

余额充值