使用Nano服务器上的PowerShell下载文件?

我有一些重大的困难,搞清楚如何在Nano服务器下使用PowerShell下载文件。

挑战如下:

有谁知道如何做到这一点(看似简单)的任务?

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

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

devise用于为云工作负载提供支持的服务器操作系统对于简单的REST / Web请求没有内置的便捷方法,这实在太疯狂了:O

无论如何,你可以试试这个powershell脚本wget.ps1 ,这是微软的一个修改。 复制粘贴在这里为了方便

 <# .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" }