كيف يمكنني إجراء عمليات تحميل HTTP PUT إلى خادم VMware ESX في PowerShell؟

StackOverflow https://stackoverflow.com/questions/65856

  •  09-06-2019
  •  | 
  •  

سؤال

من المفترض أن يكون VMware ESX وESXi وVirtualCenter قادرين على دعم تحميلات HTTP PUT منذ الإصدار 3.5.أعرف كيفية القيام بالتنزيلات، هذا سهل.لم أفعل PUT من قبل.

المعلومات الأساسية حول الموضوع هنا: http://communities.vmware.com/thread/117504

هل كانت مفيدة؟

المحلول

في ال ملحقات مجموعة الأدوات السادسة استخدم Copy-TkeDatastoreFile.وسوف تعمل مع الثنائيات.

نصائح أخرى

ينبغي عليك إلقاء نظرة على Send-PoshCode وظيفة في com.PoshCode وحدة البرنامج النصي cmdlets ...فهو يستخدم POST، وليس PUT، ولكن التقنية متطابقة عمليًا.ليس لدي خادم PUT يمكنني التفكير في اختباره، ولكن في الأساس، قم بتعيين عنوان url $ الخاص بك وبياناتك $، وافعل شيئًا مثل:

param($url,$data,$filename,[switch]$quiet)

$request = [System.Net.WebRequest]::Create($url)
$data = [Text.Encoding]::UTF8.GetBytes( $data )

## Be careful to set your content type appropriately...
## This is what you're going to SEND THEM
$request.ContentType = 'text/xml;charset="utf-8"' # "application/json"; # "application/x-www-form-urlencoded"; 
## This is what you expect back
$request.Accept = "text/xml" # "application/json";

$request.ContentLength = $data.Length
$request.Method = "PUT"
## If you need Credentials ...
# $request.Credentials = (Get-Credential).GetNetworkCredential()

$put = new-object IO.StreamWriter $request.GetRequestStream()
$put.Write($data,0,$data.Length)
$put.Flush()
$put.Close()

## This is the "simple" way ...
# $reader = new-object IO.StreamReader $request.GetResponse().GetResponseStream() ##,[Text.Encoding]::UTF8
# write-output $reader.ReadToEnd()
# $reader.Close()

## But there's code in PoshCode.psm1 for doing a progress bar, something like ....

$res = $request.GetResponse();
if($res.StatusCode -eq 200) {
   [int]$goal = $res.ContentLength
   $reader = $res.GetResponseStream()
   if($fileName) {
      $writer = new-object System.IO.FileStream $fileName, "Create"
   }

   [byte[]]$buffer = new-object byte[] 4096
   [int]$total = [int]$count = 0
   do
   {
      $count = $reader.Read($buffer, 0, $buffer.Length);
      if($fileName) {
         $writer.Write($buffer, 0, $count);
      } else {
         $output += $encoding.GetString($buffer,0,$count)
      }
      if(!$quiet) {
         $total += $count
         if($goal -gt 0) {
            Write-Progress "Downloading $url" "Saving $total of $goal" -id 0 -percentComplete (($total/$goal)*100)
         } else {
            Write-Progress "Downloading $url" "Saving $total bytes..." -id 0
         }
      }
   } while ($count -gt 0)

   $reader.Close()
   if($fileName) {
       $writer.Flush()
       $writer.Close()
   } else {
       $output
   }
}
$res.Close();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top