2017-02-16 55 views
3

我是一名網絡工程師,沒有任何關於腳本或PowerShell的背景,但是最近我開始處理它,因爲我需要創建幾個腳本。我爲VMware工作,並希望創建一個腳本來自動升級NSX環境,並且第一步是將升級包上傳到NSX Manager(最後是Web服務器)。 從NSX的API指南(https://pubs.vmware.com/NSX-62/topic/com.vmware.ICbase/PDF/nsx_62_api.pdf)你應該使用下面的API來上傳升級包POST https://NSX-Manager-IP-Address/api/1.0/appliance-management/upgrade/uploadbundle/NSX 所以我想創建一個腳本,將首先問你所需的版本升級到和基於我會將文件路徑放在一個變量中,然後使用帶參數-Infile的invoke-webrequest上載升級包。使用Powershell和Invoke-WebRequest將2.5 GB上傳到網絡服務器

然而,當我這樣做上傳proccess啓動,但夫婦分鐘後,我總是得到以下錯誤(調用-的WebRequest:基礎連接已關閉:上一個發送發生意外的錯誤。)

經過一番搜索我明白,invoke-webrequest將首先緩衝2.5GB的文件,然後上傳它,可能這就是爲什麼連接關閉,因爲NSX Manager尚未收到任何東西。

此外,我明白,我可以使用下面的(作爲一個例子)禁用緩衝: $ webRequest.AllowWriteStreamBuffering = $假 $ webRequest.SendChunked = $真實如何添加

但是我不知道這到腳本。

以下是完整的劇本我都在時刻: PS:我使用PowerShell的版本是5

[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 

任何想法將是非常有益的,非常讚賞

感謝您幫幫我。

回答

0

我覺得這種問題以前這裏有人問:Upload BIG files via HTTP

看起來建議的解決方案是使用[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 
+1

馬克,非常感謝你的幫助。我試過這之前,但與你的幫助我能夠更好地理解它,但是我面臨一個問題,因爲我得到以下錯誤時腳本到達while循環「異常調用」寫「」3「參數:「無法將數據寫入傳輸連接:現有連接被遠程主機強制關閉」,這與腳本的這一部分有關:while($ bytesRead = $ fileStream.Read($ chunk ,0,$ bufsize)) { $ requestStream.write($ chunk,0,$ bytesRead) $ requestStream.Flush() } –