Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
最新バージョンのEC2Launchをインストールするスクリプト
#
# 最新バージョンの 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.2001040"
# の様に固定値を設定しても良いと思われる
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 -eq 'w269aac17c17b9c19b9'} |
ForEach-Object { $_.tr } |
Select-Object -Skip 1 |
ForEach-Object { [Version]($_.td[0].p) }
$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 -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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.