Rest 使用Powershell并调用WebRequest将2.5 GB上载到web服务器
我是一名网络工程师,没有任何脚本或powershell方面的背景,但我最近开始处理它,因为我需要创建两个脚本。我为VMware工作,希望创建一个脚本来自动升级NSX环境,第一步是将升级包上载到NSX Manager(最后是一个web服务器)。 从NSX()的API指南中,您应该使用下面的API上传升级包帖子 因此,我正在尝试创建一个脚本,该脚本将首先要求您升级到所需的版本,并在此基础上将文件路径放入一个变量中,然后使用invoke webrequest和参数-infle上传升级包 但是,当我这样做时,上载过程开始,但在几分钟后,我总是收到以下错误(调用WebRequest:基础连接已关闭:发送时发生意外错误。) 经过一些搜索后,我了解到invoke webrequest将首先缓冲2.5GB的文件,然后上载它,这可能就是为什么连接会关闭,因为NSX管理器尚未收到任何内容 我还了解到,我可以使用以下方法禁用缓冲(作为示例): $webRequest.AllowWriteStreamBuffering=$false $webRequest.SendChunked=$true 但是,我不知道如何将此添加到脚本中 以下是我目前的完整脚本: PS:我使用的powershell版本是5Rest 使用Powershell并调用WebRequest将2.5 GB上载到web服务器,rest,powershell,invoke-command,Rest,Powershell,Invoke Command,我是一名网络工程师,没有任何脚本或powershell方面的背景,但我最近开始处理它,因为我需要创建两个脚本。我为VMware工作,希望创建一个脚本来自动升级NSX环境,第一步是将升级包上载到NSX Manager(最后是一个web服务器)。 从NSX()的API指南中,您应该使用下面的API上传升级包帖子 因此,我正在尝试创建一个脚本,该脚本将首先要求您升级到所需的版本,并在此基础上将文件路径放入一个变量中,然后使用invoke webrequest和参数-infle上传升级包 但是,当我这样
[CmdletBinding()]
$NSXUsername = "admin"
$NSXPassword = "VMware1!"
$uriP = "https://HQ-NSX-01a.nsx.gss"
# Start time.
$startclock = (Get-Date)
Write-Host -BackgroundColor:Black -ForegroundColor:Green "Hello"
Write-Host -BackgroundColor:Black -ForegroundColor:Green "This script will help you to automate a full NSX environment UPgrade"
Write-Host -BackgroundColor:Black -ForegroundColor:Green "FULL NSX Tier UPgrade for Single-VC is starting, This UPgrade proccess will take an average of 40 min
========================================================================================
"
# Create NSX authorization string and store in $head and used in API Calls
# $nsxcreds = New-Object System.Management.Automation.PSCredential $NSXUsername,$NSXPassword
$auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($NSXUsername + ":" + $NSXPassword))
$head = @{"Authorization"="Basic $auth"}
# Allow untrusted SSL certs else will error out
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
##################################################
# Choosing the required NSX Version to be deployed
##################################################
[int]$menuoptions = 0
while ( $menuoptions -lt 1 -or $menuoptions -gt 12 ) {
Write-host -BackgroundColor:Black -ForegroundColor:Red " Please choose the required version of NSX to be deployed (Accepted inputs are a number from 1 to 12) "
Write-host -BackgroundColor:Black -ForegroundColor:Yellow " 1.NSX-6.2.0 "
Write-host -BackgroundColor:Black -ForegroundColor:Yellow " 2.NSX-6.2.1 "
Write-host -BackgroundColor:Black -ForegroundColor:Yellow " 3.NSX-6.2.1a "
Write-host -BackgroundColor:Black -ForegroundColor:Yellow " 4.NSX-6.2.2 "
Write-host -BackgroundColor:Black -ForegroundColor:Yellow " 5.NSX-6.2.2a "
Write-host -BackgroundColor:Black -ForegroundColor:Yellow " 6.NSX-6.2.2b "
Write-host -BackgroundColor:Black -ForegroundColor:Yellow " 7.NSX-6.2.3 "
Write-host -BackgroundColor:Black -ForegroundColor:Yellow " 8.NSX-6.2.3a "
Write-host -BackgroundColor:Black -ForegroundColor:Yellow " 9.NSX-6.2.3b "
Write-host -BackgroundColor:Black -ForegroundColor:Yellow " 10.NSX-6.2.4 "
Write-host -BackgroundColor:Black -ForegroundColor:Yellow " 11.NSX-6.2.5 "
Write-host -BackgroundColor:Black -ForegroundColor:Yellow " 12.NSX-6.3.6 "
[int]$menuoptions = read-host
if ($menuoptions -lt 1 -or $menuoptions -gt 12) {Write-host -BackgroundColor:Black -ForegroundColor:Red " Invalid Input Detected, Please choose a valid input"}
}
Switch( $menuoptions ) {
1{$nsxpath = "D:\NSX-6.2.x-UPGrade-Bundles\VMware-NSX-Manager-upgrade-bundle-6.2.0-2986609.tar.gz"}
2{$nsxpath = "D:\NSX-6.2.x-UPGrade-Bundles\VMware-NSX-Manager-upgrade-bundle-6.2.1-3300239.tar.gz"}
3{$nsxpath = "D:\NSX-6.2.x-UPGrade-Bundles\VMware-NSX-Manager-upgrade-bundle-6.2.1a-3496286.tar.gz"}
4{$nsxpath = "D:\NSX-6.2.x-UPGrade-Bundles\VMware-NSX-Manager-upgrade-bundle-6.2.2-3604087.tar.gz"}
5{$nsxpath = "D:\NSX-6.2.x-UPGrade-Bundles\VMware-NSX-Manager-upgrade-bundle-6.2.2a-3638734.tar.gz"}
6{$nsxpath = "D:\NSX-6.2.x-UPGrade-Bundles\VMware-NSX-Manager-upgrade-bundle-6.2.2b-3755950.tar.gz"}
7{$nsxpath = "D:\NSX-6.2.x-UPGrade-Bundles\VMware-NSX-Manager-upgrade-bundle-6.2.3-3979471.tar.gz"}
8{$nsxpath = "D:\NSX-6.2.x-UPGrade-Bundles\VMware-NSX-Manager-upgrade-bundle-6.2.3a-4167369.tar.gz"}
9{$nsxpath = "D:\NSX-6.2.x-UPGrade-Bundles\VMware-NSX-Manager-upgrade-bundle-6.2.3b-4287432.tar.gz"}
10{$nsxpath = "D:\NSX-6.2.x-UPGrade-Bundles\VMware-NSX-Manager-upgrade-bundle-6.2.4-4292526.tar.gz"}
11{$nsxpath = "D:\NSX-6.2.x-UPGrade-Bundles\VMware-NSX-Manager-upgrade-bundle-6.2.5-4818372.tar.gz"}
12{$nsxpath = "D:\NSX-6.2.x-UPGrade-Bundles\VMware-NSX-Manager-upgrade-bundle-6.2.6-4977495.tar.gz"}
}
Switch( $menuoptions ) {
1{$nsxversion = "NSX-6.2.0"}
2{$nsxversion = "NSX-6.2.1"}
3{$nsxversion = "NSX-6.2.1a"}
4{$nsxversion = "NSX-6.2.2"}
5{$nsxversion = "NSX-6.2.2a"}
6{$nsxversion = "NSX-6.2.2b"}
7{$nsxversion = "NSX-6.2.3"}
8{$nsxversion = "NSX-6.2.3a"}
9{$nsxversion = "NSX-6.2.3b"}
10{$nsxversion = "NSX-6.2.4"}
11{$nsxversion = "NSX-6.2.5"}
12{$nsxversion = "NSX-6.3.6"}
}
#===========================================================================================================================================
#===========================================================================================================================================
#########################
# Upgrading NSX Manager
#########################
Write-Host -BackgroundColor:Black -ForegroundColor:Green "1. Deploying NSX Manager with version $nsxversion"
Write-Host -BackgroundColor:Black -ForegroundColor:Yellow " UPloading the required upgrade bundel to NSX Manager"
$r = Invoke-WebRequest -Uri "$uriP/api/1.0/appliance-management/upgrade/uploadbundle/NSX" -Method:Post -Headers $head -ContentType "application/xml" -InFile $nsxpath -TimeoutSec 66000 -DisableKeepAlive
任何想法都将非常有帮助,非常感谢
谢谢你的帮助。我想以前有人问过这样的问题: 建议的解决方案似乎是使用[System.Net.HttpWebRequest].Net类 我还没有对此进行测试,但我认为您可以用以下内容替换
$r=Invoke WebRequest
:
$webRequest = [System.Net.HttpWebRequest]::Create("$uriP/api/1.0/appliance-management/upgrade/uploadbundle/NSX")
$webRequest.Timeout = 66000
$webRequest.Method = "POST"
$webRequest.ContentType = "application/xml"
$webRequest.AllowWriteStreamBuffering=$false
$webRequest.SendChunked=$true
$webRequest.Credentials = $auth
$webRequest.Headers["Authorization"] = "Basic $auth"
$requestStream = $webRequest.GetRequestStream()
$fileStream = [System.IO.File]::OpenRead($nsxpath)
$chunk = New-Object byte[] $bufSize
while( $bytesRead = $fileStream.Read($chunk,0,$bufsize) )
{
$requestStream.write($chunk, 0, $bytesRead)
$requestStream.Flush()
}
$responseStream = $webRequest.getresponse()
$FileStream.Close()
$requestStream.Close()
$responseStream.Close()
$responseStream
$responseStream.GetResponseHeader("Content-Length")
$responseStream.StatusCode
马克,非常感谢你的帮助。我以前尝试过这一点,但是在您的帮助下,我能够更好地理解它,但是我面临着一个问题,因为当脚本到达带有“3”参数的while循环“Exception calling”Write”时,我遇到了以下错误:无法将数据写入传输连接:远程主机强制关闭了一个现有连接。“这与脚本的这一部分有关:while($bytesRead=$fileStream.Read($chunk,0,$bufsize)){$requestStream.write($chunk,0,$bytesRead)$requestStream.Flush()}