Functions/Set-CmEc2DnsName.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
Function Set-CmEc2DnsName            {
    Param (
        [Parameter(
            Mandatory=$true,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true
        )]
        [string[]]  $InstanceId,
        [Parameter(Mandatory=$true)]
        [string]    $DomainName,
        [string]    $Region,
        [string]    $InstanceName
    )
    BEGIN 
    {
        $ErrorActionPreference      = "Stop"
        If ($Region)
        {
            $AllRegions = (Get-AWSRegion).Region
            If ($AllRegions -notcontains $Region) 
            {
                Write-Error "$Region is not a valid AWS Region, Valid regions are $AllRegions"
            }
        }
    }
    PROCESS 
    {
        foreach ($Instance in $InstanceId)
        {
            $Parameters       = @{InstanceId  = $Instance}
            If ($Region)        {$Parameters.add('Region',$Region)}
            $RunningInstance  = (Get-EC2Instance @Parameters).RunningInstance
            $CurrentState     = $RunningInstance.State.Name.Value
            if ($CurrentState -like "stop*" -or $CurrentState -like "term*"-or $CurrentState -like "shut*" -or !$CurrentState)
            {
                Write-Error "Instance $Instance stopped, stopping or terminated or does not exist, Please start or specify a valid instance"
            }
            If ((Get-Ec2Subnet -SubnetId $RunningInstance.SubnetId -Region $Region).MapPublicIpOnLaunch -eq $true -and !$RunningInstance.PublicIpAddress)
            {
                $Counter          = 1
                While ($CurrentState -ne "running" -and !$RunningInstance.PublicIpAddress)
                {
                    
                    Start-Sleep -Seconds 1
                    $Counter++
                    $RunningInstance  = (Get-EC2Instance @Parameters).RunningInstance
                    $CurrentState     = $RunningInstance.State.Name.Value
                    if ($Counter -ge 30)
                    {
                        Write-Error "Instance $Instance took too long to start, aborting. WARNING Instance may still start and incur charges."
                    }
                }
            }
            If ($InstanceName) {$HostName  = $InstanceName+"."+$DomainName}
            Else 
            {
                $InstanceName          = $RunningInstance.Tags | Where-Object {$_.Key -eq "Name"} | Select -ExpandProperty Value
            
                If (!$InstanceName) 
                {
                    Write-Error "No Name Tag on instance $Instance, can't apply DNS Name."
                    $HostName     = $RunningInstance.PublicDnsName
                } 
                Else 
                {
                    $HostName  = $InstanceName+"."+$DomainName
                }
            }
            
            If ($RunningInstance.PublicIpAddress) 
            {
                Set-R53Record -Domain $DomainName -Type A -Name $InstanceName -Value $RunningInstance.PublicIpAddress -TTL 30 | Out-Null
            } 
            Else 
            {
                Set-R53Record -Domain $DomainName -Type A -Name $InstanceName -Value $RunningInstance.PrivateIpAddress -TTL 30 | Out-Null
            }
            $ObjProperties = @{
                InstanceId   = $Instance
                HostName     = $HostName
                CurrentState = $CurrentState
            }
            New-Object -TypeName PsObject -Property $ObjProperties
        }
    }
    END{}
}