2015/12/11

Nano Server 用 wget スクリプト

Nano Server には wget (Invoke-Webrequest) がありません。「Azure IaaS 上の Nano Server を Container Host としてセットアップする」では、https://aka.ms/tp4/Install-ContainerHost に含まれる PowerShell のコードを拝借して何とかしましたが、その拝借したコードをスクリプト化してみました。ほぼ https://aka.ms/tp4/Install-ContainerHost からのコピペです。

[wget4nano.ps1] (2016/5/:TP5でも使えるように簡単にしました Test-Nano Function とっちゃいました)
#
# Most of the code included in this script is the copy from
# https://aka.ms/tp4/Install-ContainerHos
#
param(
    [string]
    [ValidateNotNullOrEmpty()]
    $Uri,
    [string]
    [ValidateNotNullOrEmpty()]
    $OutFile
)


$SourcePath = $Uri
$DestinationPath = $OutFile


$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($DestinationPath, [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
        }
    }
}
使い方は...

.\wget4nano.ps1 -uri <URL> -outfile <ファイルパス>

[<- Nano Server TP4 まとめ

0 件のコメント:

コメントを投稿

注: コメントを投稿できるのは、このブログのメンバーだけです。