gpt4 book ai didi

http - 如何在 PowerShell 中执行 HTTP PUT 上传到 VMware ESX 服务器?

转载 作者:可可西里 更新时间:2023-11-01 16:26:13 26 4
gpt4 key购买 nike

VMware ESX、ESXi 和 VirtualCenter 从版本 3.5 开始应该能够支持 HTTP PUT 上传。我知道如何下载,这很容易。我以前从未做过 PUT。

有关该主题的背景信息位于此处:http://communities.vmware.com/thread/117504

最佳答案

你应该看看 PoshCode 中的 Send-PoshCode 函数cmdlets 脚本模块...它使用 POST,而不是 PUT,但技术实际上是相同的。我没有 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();

关于http - 如何在 PowerShell 中执行 HTTP PUT 上传到 VMware ESX 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com