Export/WorkflowFunctions.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# For running the workflows in parellel by subscription
#
workflow Run-BySubscriptionWorkflow
{
    param
    (
        $azSubscriptions,
        $dnsZoneQuery,
        $inputDnsZoneNameRegexFilter,
        $interestedAzureDnsZones,
        $InputSubscriptionIdRegexFilterForAzureDns
    )

    #Get the Azure DNS Zones
    
    $dnsZones = Get-AzResourcesListForWorkFlow -query $dnsZoneQuery

    $interestedZones = $dnsZones| Where-Object { $psitem.Name -match $inputDnsZoneNameRegexFilter -and $psitem.SubscriptionId -match $InputSubscriptionIdRegexFilterForAzureDns}    

    $subsWithZones = ($interestedZones.subscriptionId | Group-Object).Name
    
    $wfNumberOfDnsZones = ($interestedZones | Group-Object type).count

    $wfNumberOfDnsRecordSets = 0

    Foreach($item in $interestedZones)
    {
        $wfNumberOfDnsRecordSets += ($item.Properties.NumberOfRecordSets)
    }
    
    [pscustomObject]@{'Name' = 'ProcessSummaryData'; 'wfNumberOfDnsZones' = $wfNumberOfDnsZones; 'wfNumberOfDnsRecordSets' = $wfNumberOfDnsRecordSets } 

    Foreach -parallel ($subscription in $azSubscriptions)
    {
        If($subscription.subscriptionId -in $subsWithZones)
        {
            Select-AzSubscription -SubscriptionObject $subscription
            
            $azContext = Get-AzContext

            $interestedZones1 = $interestedZones | Where-Object{$psitem.subscriptionId -eq $subscription.subscriptionId}

            Get-DnsRecordsWorkFlow -wfDnsZones $($interestedZones1) -wfContext $azContext -wfInterestedDnsZones $interestedAzureDnsZones -wfSubscription $subscription
        }
    }
}

# For running the workflows in parellel by for each DNS zone
#
workflow Get-DnsRecordsWorkFlow
{
    param
    (
        $wfDnsZones,        
        $wfContext,
        $wfInterestedDnsZones,
        $wfSubscription
    )

    Foreach -parallel ($wfDnsZone in $wfDnsZones)
    {
        inlinescript
        {
          
            $AZModules = ('Az.Accounts', 'Az.Dns')
            Foreach($module in $AZModules)
            {
                If(Get-Module -Name $module)
                {
                    continue
                }elseif(Get-Module -ListAvailable -Name $module)
                {
                    Import-Module -name $module -Scope Local -Force
                }else
                {
                    Install-module -name $module -AllowClobber -Force -Scope CurrentUser -SkipPublisherCheck
                    Import-Module -name $module -Scope Local -Force
                }
            
                If(!$(Get-Module -Name $module))
                {
                    Write-Error "Could not load dependant module: $module"
                    throw
                }
            }

            #Function to return resource provider
            Function Get-ResourceProvider
            {
                param
                (
                    $resourceName
                )
                switch -regex ($resourceName)
                {
                    'azure-api.net$' { $resourceProvider = 'azure-api.net'; break}
                    'azurecontainer.io$' { $resourceProvider = 'azurecontainer.io'; break}
                    'azurefd.net$' { $resourceProvider = 'azurefd.net'; break}
                    'azureedge.net$' { $resourceProvider = 'azureedge.net'; break}
                    'azurewebsites.net$' { $resourceProvider = 'azurewebsites.net'; break}
                    'blob.core.windows.net$' { $resourceProvider = 'blob.core.windows.net'; break}
                    'cloudapp.azure.com$' { $resourceProvider = 'cloudapp.azure.com'; break}
                    'cloudapp.net$' { $resourceProvider = 'cloudapp.net'; break}
                    'trafficmanager.net$' { $resourceProvider = 'trafficmanager.net'; break}
                }
                return $resourceProvider
            }
            
            # Function to add resource provider
            #
            Function Add-ResourceProvider
            {
                param
                (
                    $resourceList
                )
                
                $resourceList | ForEach-Object `
                {       
                    If(!$psitem.resourceProvider)
                    {
                        $psitem | Add-Member -NotePropertyName "resourceProvider" -NotePropertyValue $(Get-ResourceProvider $psitem.Fqdn) -Force
                    }
                }
            }

            # Function to retrive the Azure DNS records
            #
            Function Get-DnsCNameRecords
            {
                param
                (
                    $zone,
                    $interestedDnsZones
                )

                $cNameToDnsMap = [System.Collections.ArrayList]::new()
                    
                $cNameRecords = Get-AzDnsRecordSet -ResourceGroupName $zone.ResourceGroup -ZoneName $zone.Name -RecordType CNAME
                                    
                Foreach($item in $cNameRecords)
                {                    
                    foreach($record in $item.records)
                    {
                        If(![string]::IsNullOrEmpty($record) -and $record -match $interestedDnsZones)
                        {                            
                            [void]$cNameToDnsMap.add([psCustomObject]@{'CName' = $item.Name; 'Fqdn' = $record.CName; 'ZoneName' = $zone.Name; 
                                                    'ResourceGroup' = $zone.ResourceGroup; 'resourceProvider' = $(Get-ResourceProvider $record)})
                        }
                    }
                }
                return $cNameToDnsMap
            }

            $dnszone = $using:wfDnsZone            
            $context = $using:wfContext
            $interestedDnsZones = $using:wfInterestedDnsZones            
            $subscription = $using:wfSubscription

            Select-AzSubscription -SubscriptionObject $subscription

            Select-AzContext -InputObject $context
                        
            Get-DnsCNameRecords $dnsZone $interestedDnsZones
        }
    }
}