系统日志中的示例(我从 aws 控制台视图中获得)
2016/12/26 14:36:12Z:AMI 来源名称:Windows_Server-2016-English-Full-Base
等效的powershell是:
获取 EC2ConsoleOutput
所以下面是完整的(我是powershell的新手,但我很确定有人可以以某种方式将它包装成一个字符)
# read the system console log
$consoleLog = Get-EC2ConsoleOutput $currentInstanceObj.InstanceId
$consoleLogOutput = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($consoleLog.Output));
# extract the lines that contain AMI Origin in them (there should be 2) - sort them so name i first and version is second
$originLines = $consoleLogOutput -split '[\r\n]' | Where-Object {$_.Contains("AMI Origin")} | Sort-Object
# get the running name
$originLine = $originLines[0]
$originLineParts = $originLine.Split(':')
$originName = $originLineParts[$originLineParts.Length - 1].Trim()
"The origin name is $originName"
# get the running version (slighly pointless since the code below doesn't care as we want the latest - but it's for verbosity)
$originLine = $originLines[1]
$originLineParts = $originLine.Split(':')
$originVersion = $originLineParts[$originLineParts.Length - 1].Trim()
"The origin version is $originVersion"
# concatenate to get the original origin name (note: amazon have a naming pattern here - (name-version)
$amiName = $originName + "-" + $originVersion
"The original origin ami name is $amiName"
#find the latest of the same name and report the difference
$latestOriginObj = (Get-EC2Image -Filter @{ Name="name"; Values=($originName + "*")} | Sort-Object CreationDate -Descending | Select-Object -First 1)
if($latestOriginObj.ImageId -ne $currentInstanceObj.ImageId)
{
"The ami has been upgraded from " + ($currentInstanceObj.ImageId) + " to " + ($latestOriginObj.ImageId)
}
#....so go ahead and use the $latestOriginObj.ImageId when you create a new instance
而这方面的知识来源来自这些亚马逊文档
摘录如下:
AWS 管理控制台提供有关您用于创建 Amazon EC2 实例的 AMI 的详细信息。描述选项卡上的 AMI ID 字段包含的信息包括 Windows Server SKU、架构(32 位或 64 位)、AMI 的创建日期和 AMI ID。
如果 AMI 已设为私有或被更高版本替换并且不再列在目录中,则 AMI ID 字段会显示“无法加载 ami-xxxxx 的详细信息。您可能无权查看它。” 要确定使用哪个 AMI 创建实例,您必须打开系统日志。在 EC2 控制台中,选择一个实例,然后从上下文菜单(右键单击)中选择实例设置,然后选择获取系统日志。AMI 的创建日期和 SKU 列在 AMI 原始版本和 AMI 原始名称字段中。