PowerShell에서 VMware ESX Server에 대한 HTTP PUT 업로드를 어떻게 수행할 수 있나요?

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

  •  09-06-2019
  •  | 
  •  

문제

VMware ESX, ESXi 및 VirtualCenter는 버전 3.5부터 HTTP PUT 업로드를 지원할 수 있다고 가정됩니다.다운로드하는 방법을 알고 있습니다. 쉽습니다.저는 PUT을 한번도 해본 적이 없습니다.

주제에 대한 배경 정보는 다음과 같습니다. http://communities.vmware.com/thread/117504

도움이 되었습니까?

해결책

에서 VI 툴킷 확장 Copy-TkeDatastoreFile을 사용하십시오.바이너리와 함께 작동합니다.

다른 팁

당신은 Send-PoshCode 에서 기능 포쉬코드 cmdlet 스크립트 모듈...PUT이 아닌 POST를 사용하지만 기술은 사실상 동일합니다.테스트할 수 있는 PUT 서버가 없지만 기본적으로 $url 및 $data를 설정하고 다음과 같이 수행하십시오.

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