0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PowerShellベーシック認証接続メモ

Last updated at Posted at 2018-11-30

Credential作成

Get-Credential でユーザーとパスワードの認証を入力するようにした
Invoke-WebRequestを利用してBasic認証を行う。

ベーシック認証を利用したダウンローダー


# Base64 encode
function Get-BasicAuthCreds {
  param([string]$Username,[string]$Password)
  $AuthString = "{0}:{1}" -f $Username,$Password
  $AuthBytes  = [System.Text.Encoding]::Ascii.GetBytes($AuthString)
  return [Convert]::ToBase64String($AuthBytes)
}

$tmpcred = Get-Credential
$BasicCreds = Get-BasicAuthCreds -Username $tmpcred.UserName -Password $tmpcred.GetNetworkCredential().Password
    #取り出したURLからファイル名を取り出し、変数$fileにセット
    $uri = New-Object System.Uri($url)
    $file = Split-Path $uri.AbsolutePath -Leaf

try {
    Invoke-WebRequest -Uri $url -Headers @{"Authorization"="Basic $BasicCreds"} -OutFile $file
} catch {
    $_.Exception.Response.StatusCode.Value__
    if ($_.Exception.Response.StatusCode.Value__ -eq 404) {Write-host "エラー 404: $urlにアクセスできません" -foregroundcolor red}
    exit
}
Write-host $file "をダウンロード完了しました。" -foregroundcolor green
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?