Functions/Get-CmEc2ImageId.ps1

Function Get-CmEc2ImageId            {
    [CmdletBinding(DefaultParameterSetName='Base')]
    Param(
        [Parameter(Position=0)]
        [ValidateSet(1709, 1803, 2016, "2012R2", 2012, "2008R2", 2008, 2003)]
        [string] $OsVersion="2012R2",

        [Parameter(ParameterSetName='Base')]
        [switch] $Base=$true,

        [Parameter(ParameterSetName='SQL',
                   Position=1)]
        [ValidateSet("2014SP1","2014SP2","2016SP1","2017","2012SP3","2012SP2","2008R2SP3","2008SP4","2005SP4")]
        [string] $SqlVersion,

        [Parameter(ParameterSetName='SQL',
                   Position=2)]
        [ValidateSet("Express", "Web","Standard","Enterprise")]
        [string] $SqlEdition = "Standard",

        [switch] $Core,

        [string] $Region,

        [ValidateSet(
            "Chinese_Traditional",
            "Chinese_Simplified",
            "Czech",
            "Dutch",
            "English",
            "French",
            "German",
            "Hungarian",
            "Korean",
            "Japanese",
            "Polish",
            "Portuguese_Brazil",
            "Russian",
            "Spanish",
            "Swedish",
            "Turkish"
            )]
        [string] $Language = "English"
    )
    $ErrorActionPreference = "Stop"
    if ($SqlVersion) 
    {
        Clear-Variable Base
        $SqlVersion = $SqlVersion.ToUpper()
        $SqlEdition = $SqlEdition.Substring(0,1).ToUpper() + $SqlEdition.Substring(1).ToLower()
    }
    $OSVersion             = $OSVersion.ToUpper()
    $Language              = $Language.Substring(0,1).ToUpper() + $Language.Substring(1).ToLower()
    
    $BaseText = "/aws/service/ami-windows-latest/Windows_Server-"
    if ($SqlVersion.Length -eq 7) 
    {
        $SqlYear = $SqlVersion.Substring(0,4)
        $SqlSp   = $SqlVersion.Substring($SqlVersion.Length-3)
        $SqlText =  "SQL_"+$SqlYear+"_"+$SqlSp+"_"+$SqlEdition
    }
    elseif ($SqlVersion -eq "2008R2SP3")
    {
        $SqlText =  "SQL_2008_R2_SP3_"+$SqlEdition
    }
    elseif  ($SqlVersion.Length -eq 4)
    {
        $SqlYear = $SqlVersion.Substring(0,4)
        $SqlText =  "SQL_"+$SqlYear+"_"+$SqlEdition
    }
     
    if ($OsVersion -ne "2016" -and $SqlYear -eq 2017)                                        {Write-Error "SQL Server 2017 and higher only supported on Windows Server 2016"}
    if (!($OsVersion -eq "2016" -or $OsVersion -eq "2012R2") -and $SqlYear -eq 2016)         {Write-Error "SQL Server 2016 only supported on Windows Server 2012R2 and 2016"}
    if (!($OsVersion -eq "2012R2" -or $OsVersion -eq "2012") -and $SqlYear -eq 2014)         {Write-Error "SQL Server 2014 only supported on Windows Server 2012 and 2012R2"}
    if (!($OsVersion -eq "2008R2" -or $OsVersion -eq "2012") -and $SqlYear -eq 2012)         {Write-Error "SQL Server 2012 only supported on Windows Server 2008R2 and 2012"}
    if ($OsVersion -ne "2008" -and $SqlYear -eq 2008)                                        {Write-Error "SQL Server 2008 only supported on Windows Server 2008"}
    if ($OsVersion -ne "2003" -and $SqlYear -eq 2005)                                        {Write-Error "SQL Server 2005 only supported on Windows Server 2003"}
    if ($Core -and -not ($OsVersion -ne "2012R2" -or $OsVersion -ne 2016 -or $OsVersion -ne "2008R2")) {Write-Error "Core AMIs only available for Windows Server 2008R2, 2012R2 and 2016"}
    
    if ($OsVersion -eq "1709" -or $OsVersion -eq "1803")     
    {
        $SearchString = $BaseText+$OsVersion+"-"+$Language+"-Core"
        if ($Base) {$SearchString = $SearchString+"-Base"}
        else       {$SearchString = $SearchString+"-"+$SqlText}
    }
    if ($OsVersion -eq "2016")     
    {
        if ($Core) {$SearchString = $BaseText+$OsVersion+"-"+$Language+"-Core"}
        else       {$SearchString = $BaseText+$OsVersion+"-"+$Language+"-Full"}
        if ($Base) {$SearchString = $SearchString+"-Base"}
        else       {$SearchString = $SearchString+"-"+$SqlText}
    }
    if ($OsVersion -eq "2012R2")   
    {
        if ($Base) 
        {
            if     ($Core) {$SearchString = $BaseText+"2012-R2_RTM-"+$Language+"-64Bit-Core"}
            else {$SearchString = $BaseText+"2012-R2_RTM-"+$Language+"-64Bit-Base"}
        }
           
        else 
        {
            if     ($Core) {Write-Error "SQL Server not available on Windows 2012 R2 Core"}
            else {$SearchString = $BaseText+"2012-R2_RTM-"+$Language+"-64Bit-"+$SqlText}
        }
    }

    if ($OsVersion -eq "2012")     
    {
        if ($Base) {$SearchString = $BaseText+"2012-RTM-"+$Language+"-64Bit-Base"}
        else       {$SearchString = $BaseText+"2012-RTM-"+$Language+"-64Bit-"+$SqlText}
    }
    if ($OsVersion -eq "2008R2")   
    {
        if     ($Core) {$SearchString = $BaseText+"2008-R2_SP1-"+$Language+"-64Bit-Core"}
        elseif ($Base) {$SearchString = $BaseText+"2008-R2_SP1-"+$Language+"-64Bit-Base"}
        else           {$SearchString = $BaseText+"2008-R2_SP1-"+$Language+"-64Bit-"+$SqlText}
    }
    if ($OsVersion -eq "2008")     
    {
        if ($Base) {$SearchString = $BaseText+"2008-SP2-"+$Language+"-64Bit-Base"}
        else       {$SearchString = $BaseText+"2008-SP2-"+$Language+"-64Bit-"+$SqlText}
    }
    if ($OsVersion -eq "2003")     
    {
        if ($Base) {$SearchString = $BaseText+"2003-R2_SP2-"+$Language+"-64Bit-Base"}
        else       {$SearchString = $BaseText+"2003-R2_SP2-"+$Language+"-64Bit-"+$SqlText}
    }
    $Parameters = @{Name = $SearchString}
    if ($Region) {$Parameters.Add('Region', $Region)}
    (Get-SSMParameter @Parameters).Value
}