在Nano Server上下载具有Powershell的文件?


9

我在弄清楚如何在Nano Server下使用PowerShell精确下载文件时遇到了一些重大困难。

挑战如下:

  • 没有Invoke-WebRequest

  • 没有System.Net.WebClient

  • 没有Start-BitsTransfer

  • 没有bitsadmin

有人知道如何执行此任务(看似简单)吗?

Answers:


4

这里有一个使用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
}

3
谢谢!尽管这是相当复杂的。
另一个用户

1
答:并非所有cmdlet都可用
Jim B

4

Invoke-WebRequest作为Windows Server 2016累积更新2016年9月26日的一部分被添加到nanoserver中。


我相信您所指的Powershell代码示例应在客户端计算机上运行,​​而不是在Nano docker主机上运行(它说“在您将要工作的远程系统上,下载Docker客户端。:Invoke-WebRequest ...”)
qbik

我可能是错的,但我假设@ yet-another-user想要​​在构建期间从docker客户端中使用它。
mikebridge '17

2

疯狂的是,设计用于支持云工作负载的服务器操作系统没有针对简单的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"
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.