我在弄清楚如何在 Nano Server 下使用 PowerShell 下载文件时遇到了一些重大困难。
挑战如下:
没有 Invoke-WebRequest
没有 System.Net.WebClient
没有 Start-BitsTransfer
没有 bitsadmin
有人知道如何完成这个(看似简单的)任务吗?
答案1
这里有一个使用 Nano 上的 PowerShell 下载 zip 文件的示例,您可能需要根据您的目的对其进行一些修改;
(从这里:https://docs.asp.net/en/latest/tutorials/nano-server.html#installing-the-asp-net-core-module-ancm)
$SourcePath = "https://dotnetcli.blob.core.windows.net/dotnet/beta/Binaries/Latest/dotnet-win-x64.latest.zip"
$DestinationPath = "C:\dotnet"
$EditionId = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name 'EditionID').EditionId
if (($EditionId -eq "ServerStandardNano") -or
($EditionId -eq "ServerDataCenterNano") -or
($EditionId -eq "NanoServer") -or
($EditionId -eq "ServerTuva")) {
$TempPath = [System.IO.Path]::GetTempFileName()
if (($SourcePath -as [System.URI]).AbsoluteURI -ne $null)
{
$handler = New-Object System.Net.Http.HttpClientHandler
$client = New-Object System.Net.Http.HttpClient($handler)
$client.Timeout = New-Object System.TimeSpan(0, 30, 0)
$cancelTokenSource = [System.Threading.CancellationTokenSource]::new()
$responseMsg = $client.GetAsync([System.Uri]::new($SourcePath), $cancelTokenSource.Token)
$responseMsg.Wait()
if (!$responseMsg.IsCanceled)
{
$response = $responseMsg.Result
if ($response.IsSuccessStatusCode)
{
$downloadedFileStream = [System.IO.FileStream]::new($TempPath, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write)
$copyStreamOp = $response.Content.CopyToAsync($downloadedFileStream)
$copyStreamOp.Wait()
$downloadedFileStream.Close()
if ($copyStreamOp.Exception -ne $null)
{
throw $copyStreamOp.Exception
}
}
}
}
else
{
throw "Cannot copy from $SourcePath"
}
[System.IO.Compression.ZipFile]::ExtractToDirectory($TempPath, $DestinationPath)
Remove-Item $TempPath
}
答案2
Invoke-WebRequest
被添加到纳米服务器作为2016 年 9 月 26 日 Windows Server 2016 累积更新。
答案3
疯狂的是,用于支持云工作负载的服务器操作系统竟然没有内置方便的方法来处理简单的 REST/Web 请求 :O
无论如何,你可以尝试这个 powershell 脚本wget.ps1这是对 Microsoft 的修改。为方便起见,复制粘贴到这里
<#
.SYNOPSIS
Downloads a file
.DESCRIPTION
Downloads a file
.PARAMETER Url
URL to file/resource to download
.PARAMETER Filename
file to save it as locally
.EXAMPLE
C:\PS> .\wget.ps1 https://dist.nuget.org/win-x86-commandline/latest/nuget.exe
#>
Param(
[Parameter(Position=0,mandatory=$true)]
[string]$Url,
[string]$Filename = ''
)
# Get filename
if (!$Filename) {
$Filename = [System.IO.Path]::GetFileName($Url)
}
Write-Host "Download: $Url to $Filename"
# Make absolute local path
if (![System.IO.Path]::IsPathRooted($Filename)) {
$FilePath = Join-Path (Get-Item -Path ".\" -Verbose).FullName $Filename
}
if (($Url -as [System.URI]).AbsoluteURI -ne $null)
{
# Download the bits
$handler = New-Object System.Net.Http.HttpClientHandler
$client = New-Object System.Net.Http.HttpClient($handler)
$client.Timeout = New-Object System.TimeSpan(0, 30, 0)
$cancelTokenSource = [System.Threading.CancellationTokenSource]::new()
$responseMsg = $client.GetAsync([System.Uri]::new($Url), $cancelTokenSource.Token)
$responseMsg.Wait()
if (!$responseMsg.IsCanceled)
{
$response = $responseMsg.Result
if ($response.IsSuccessStatusCode)
{
$downloadedFileStream = [System.IO.FileStream]::new($FilePath, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write)
$copyStreamOp = $response.Content.CopyToAsync($downloadedFileStream)
# TODO: Progress bar? Total size?
Write-Host "Downloading ..."
$copyStreamOp.Wait()
$downloadedFileStream.Close()
if ($copyStreamOp.Exception -ne $null)
{
throw $copyStreamOp.Exception
}
}
}
}
else
{
throw "Cannot download from $Url"
}
答案4
自从微软发布新的 Windows Nano Server 1709 容器映像以来,不使用 PowerShell 下载和提取文件一直很具挑战性。
近日,微软虚拟化部门宣布将在Windows 10和Windows Nano Server中添加两个命令行工具。
这些工具包括:
Tar.exe – 允许我们在没有 PowerShell 的情况下提取文件并创建档案,这意味着您可以从命令行使用它。
Curl .exe – 允许我们从互联网源和位于我们网络上的服务器下载文件。
curl.exe -o node.zip https://nodejs.org/dist/v9.2.0/node-v9.2.0-win-x64.zip
参考:https://www.ntweekly.com/2019/10/27/tar-and-curl-come-to-windows-nano-server-insider-image/