Last active
June 1, 2023 04:53
-
-
Save stknohg/ab3f7e6d65fa5a069d20f45d1a45ee3a to your computer and use it in GitHub Desktop.
最新バージョンのEC2Launchをインストールするスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 最新バージョンの EC2Launch をインストールするスクリプト | |
# Ref : https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2launch-download.html | |
# | |
# ※ EC2Launch 設定ファイルのバックアップには対応していません。 | |
# | |
<# | |
.SYNOPSIS | |
EC2Launchの最新バージョンを取得します | |
#> | |
function Get-LatestEC2LaunchVersion() { | |
# https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2launch-version-details.html | |
# より最新バージョンを取得 | |
# | |
# ※いつHTMLの構造が変わるかわからないので最悪 | |
# return [Version]"1.3.2003150" | |
# の様に固定値を設定しても良いと思われる | |
try { | |
$res = Invoke-WebRequest -Uri 'https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2launch-version-details.html' -UseBasicParsing | |
$versions = ([Xml]$res.Content).html.body.GetElementsByTagName("table") | | |
Where-Object { $_.id -like 'w???????????????????' } | | |
ForEach-Object { $_.tr } | | |
ForEach-Object { | |
if ($_.td[0].p) { | |
[Version]($_.td[0].p) | |
} | |
else { | |
[Version]($_.td[0]) | |
} | |
} | |
$latestVersion = $versions | | |
Sort-Object -Descending | | |
Select-Object -First 1 | |
return $latestVersion | |
} | |
catch { | |
Write-Host $_ | |
return $null | |
} | |
} | |
<# | |
.SYNOPSIS | |
現在インストールされているEC2Launchのバージョンを取得します。 | |
EC2Launchがインストールされていない場合は $null を返します。 | |
#> | |
function Get-EC2LaunchVersion() { | |
$psdPath = 'C:\ProgramData\Amazon\EC2-Windows\Launch\Module\Ec2Launch.psd1' | |
if (-not (Test-Path -Path $psdPath -PathType Leaf)) { | |
Write-Warning "Ec2Launch isn't installed." | |
return $null | |
} | |
return [Version](Import-PowerShellDataFile $psdPath).ModuleVersion | |
} | |
<# | |
.SYNOPSIS | |
EC2Launchの最新パッケージをダウンロードします。 | |
#> | |
function Save-LatestEC2LaunchPackage([string]$SavePath = $env:TEMP) { | |
$packages = @( | |
@{FileName = 'EC2-Windows-Launch.zip'; Uri = 'https://s3.amazonaws.com/ec2-downloads-windows/EC2Launch/latest/EC2-Windows-Launch.zip' }, | |
@{FileName = 'install.ps1'; Uri = 'https://s3.amazonaws.com/ec2-downloads-windows/EC2Launch/latest/install.ps1' } | |
) | |
foreach ($package in $packages) { | |
Write-Host "Download $($package.FileName) ..." | |
$outFile = Join-Path $SavePath ($package.FileName) | |
Invoke-WebRequest -Uri $package.Uri -OutFile $outFile | |
} | |
} | |
<# | |
.SYNOPSIS | |
ダウンロードしたEC2Launchの最新パッケージを削除します。 | |
#> | |
function Clear-LatestEC2LaunchPackage([string]$InstallerPath = $env:TEMP) { | |
$items = @( | |
(Join-Path $InstallerPath 'EC2-Windows-Launch.zip'), | |
(Join-Path $InstallerPath 'install.ps1') | |
) | |
foreach ($item in $items) { | |
if (Test-Path -LiteralPath $item) { | |
Write-Host "Remove $item ..." | |
Remove-Item -LiteralPath $item -Force | |
} | |
} | |
} | |
<# | |
.SYNOPSIS | |
EC2Launchのインストーラーを実行します。 | |
#> | |
function Invoke-EC2LaunchInstaller([string]$InstallerPath = $env:TEMP) { | |
$installer = Join-Path $InstallerPath 'install.ps1' | |
try { | |
Push-Location $InstallerPath | |
Write-Host "Execute $installer ..." | |
& $installer | |
} | |
finally { | |
Pop-Location | |
} | |
} | |
<# | |
.SYNOPSIS | |
EC2LaunchがサポートされているOS(2016 Server以降)か判定します。 | |
#> | |
function Test-SupportedVersion() { | |
$ProductType_WorkStation = 1 | |
try { | |
$versionInfo = Get-CimInstance -ClassName Win32_OperatingSystem -Property Version, ProductType, Caption | |
Write-Host "OS : $($versionInfo.Caption)" | |
if (([Version]$versionInfo.Version).Major -lt 10) { | |
return $false | |
} | |
if ($versionInfo.ProductType -eq $ProductType_WorkStation ) { | |
return $false | |
} | |
return $true | |
} | |
catch { | |
return $false | |
} | |
} | |
<# | |
.SYNOPSIS | |
Main関数です。 | |
#> | |
function Main() { | |
if (-not (Test-SupportedVersion)) { | |
Write-Host "This is unsupported OS." | |
return -1 | |
} | |
# Check EC2Launch version | |
$latestVersion = Get-LatestEC2LaunchVersion | |
if ($null -eq $latestVersion) { | |
Write-Host 'Failed to get the latest version.' | |
return -1 | |
} | |
Write-Host "Latest EC2Launch version is $latestVersion ..." | |
$currentVersion = Get-EC2LaunchVersion | |
Write-Host "Current EC2Launch version is $currentVersion ..." | |
if ($currentVersion -gt $latestVersion) { | |
Write-Host "Failed to get the latest version information." | |
return -1 | |
} | |
if ($currentVersion -eq $latestVersion) { | |
Write-Host "EC2Launch is the latest version." | |
return 0 | |
} | |
# Update EC2Launch | |
try { | |
Save-LatestEC2LaunchPackage | |
Invoke-EC2LaunchInstaller | |
} | |
finally { | |
Clear-LatestEC2LaunchPackage | |
} | |
$currentVersion = Get-EC2LaunchVersion | |
Write-Host "Current EC2Launch version is $currentVersion ." | |
} | |
exit Main |
このスクリプトはスクレイピングでEC2Launch v1のバージョンを取得しており、AWSサイト側の構造が変わると失敗します。
このため定期的にスクリプトの内容を修正しています。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
このスクリプトは以下の記事で使用しています