-
Notifications
You must be signed in to change notification settings - Fork 15
/
Set-AzDdos.ps1
164 lines (145 loc) · 7.76 KB
/
Set-AzDdos.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
<#
.SYNOPSIS
Manage DDoS on Azure Virtual Networks
.DESCRIPTION
REQUIRED : Internet access & Already connected to an Azure tenant
REQUIRED : PowerShell modules, see variables
.PARAMETER logFile
Optional
Log file path
.NOTES
AUTHOR: James Dumont le Douarec
.LINK
.EXAMPLE
#Define the existing target DDoS protection plan Id
$DdosProtectionId = "/subscriptions/xxxxx/resourceGroups/xxxxx/providers/Microsoft.Network/ddosProtectionPlans/xxxxx""
#Audit which Virtual Network are compliant, compliant means that where have the correct $DdosProtectionId
./Set-AzDdos.ps1 -Audit -DdosProtectionId $DdosProtectionId
#Audit and Remediate which Virtual Network are compliant, compliant means that where have the correct $DdosProtectionId
./Set-AzDdos.ps1 -Audit -Remediate -DdosProtectionId $DdosProtectionId
#>
param(
[Parameter(Mandatory = $false, HelpMessage = 'Log file path')]
[string]$logFile = $null,
[switch]$Audit,
[switch]$Remediate,
[Parameter(Mandatory = $true, HelpMessage = 'The DDoS protection plan plan like /subscriptions/xxxxx/resourceGroups/xxxxx/providers/Microsoft.Network/ddosProtectionPlans/xxxxx')]
[string]$DdosProtectionId
)
################################################################################
# Function
################################################################################
#region function
Function Generate_Log_Action([string]$Action, [ScriptBlock]$Command, [string]$LogFile) {
$Output = "Info : $Action ... "
Write-Host $Output -ForegroundColor Cyan
((Get-Date -UFormat "[%d-%m-%Y %H:%M:%S] : ") + "Info" + " : " + $Action) | Out-File -FilePath $LogFile -Append -Force
Try {
$Result = Invoke-Command -ScriptBlock $Command
}
Catch {
$ErrorMessage = $_.Exception.Message
$Output = "On action $Action : $ErrorMessage"
((Get-Date -UFormat "[%d-%m-%Y %H:%M:%S] : ") + "Error" + " : " + $Output) | Out-File -FilePath $LogFile -Append -Force
Write-Error $Output
$Result = "Error"
}
Return $Result
}
Function virtualNetwork_array {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)][String] $AzureSubscriptionName,
[Parameter(Mandatory = $true, ValueFromPipeline = $true)][String] $Name,
[Parameter(Mandatory = $true, ValueFromPipeline = $true)][String] $ResourceGroupName,
[Parameter(Mandatory = $true, ValueFromPipeline = $true)][String] $Tag_application,
[Parameter(Mandatory = $true, ValueFromPipeline = $true)][String] $DdosProtectionPlan
)
Process {
$private:tableObj = New-Object PSObject
$tableObj | Add-Member -Name AzureSubscriptionName -MemberType NoteProperty -Value $AzureSubscriptionName
$tableObj | Add-Member -Name Name -MemberType NoteProperty -Value $Name
$tableObj | Add-Member -Name ResourceGroupName -MemberType NoteProperty -Value $ResourceGroupName
$tableObj | Add-Member -Name Tag_application -MemberType NoteProperty -Value $Tag_application
$tableObj | Add-Member -Name DdosProtectionPlan -MemberType NoteProperty -Value $DdosProtectionPlan
return $tableObj
}
}
#endregion
################################################################################
# Variable
################################################################################
Set-StrictMode -Version 2
$ErrorActionPreference = "Stop"
$AzureRmSubscriptions = Get-AzSubscription
$virtualNetwork_array = @()
$workfolder = Split-Path $script:MyInvocation.MyCommand.Path
$date = Get-Date -UFormat "%d-%m-%Y"
#Module Name, Minimum Version
$PowerShellModules = @(
@{
Name = "Az.Accounts"
MinimumVersion = "2.5.2"
},
@{
Name = "Az.Network"
MinimumVersion = "4.10.0"
}
)
#If not provided, creating the log file
if ($logFile -eq "") {
$LogPath = $workfolder + "/logs"
Write-Host "$LogPath" -ForegroundColor Cyan
if (!(Test-Path $LogPath)) { mkdir $LogPath }
$logFile = $LogPath + "/$date-" + $MyInvocation.MyCommand.Name + ".log"
}
ForEach ($PowerShellModule in $PowerShellModules) {
$Action = "Importing Module $($PowerShellModule.Name) with MinimumVersion $($PowerShellModule.MinimumVersion)"
$Command = { Import-Module -Name $($PowerShellModule.Name) -MinimumVersion $($PowerShellModule.MinimumVersion) -ErrorAction Stop }
$Result = Generate_Log_Action -Action $Action -Command $Command -LogFile $logFile
if ($Result -eq "Error") { Exit 1 }
}
#endregion
################################################################################
# Action
################################################################################
foreach ($AzureRmSubscription in $AzureRmSubscriptions) {
$Action = "Getting the AzureRm context for the SubscriptionName : $($AzureRmSubscription.Name)"
$Command = { Get-AzSubscription -SubscriptionName $AzureRmSubscription.Name | Set-AzContext -ErrorAction Stop }
$AzureRmContext = Generate_Log_Action -Action $Action -Command $Command -LogFile $logFile
if ($AzureRmContext -eq "Error") { Exit 1 }
$Action = "Selecting the AzureRm SubscriptionName : $($AzureRmSubscription.Name)"
$Command = { Select-AzSubscription -Name $AzureRmSubscription.Name -Context $AzureRmContext -Force -ErrorAction Stop }
$Result = Generate_Log_Action -Action $Action -Command $Command -LogFile $logFile
if ($Result -eq "Error") { Exit 1 }
$Action = "Getting the Virtual Networks from the SubscriptionName : $($AzureRmSubscription.Name)"
$Command = { Get-AzVirtualNetwork -ErrorAction Stop }
$virtualNetworks = Generate_Log_Action -Action $Action -Command $Command -LogFile $logFile
if ($virtualNetworks -eq "Error") { Exit 1 }
foreach ($virtualNetwork in $virtualNetworks) {
$Compliant = if ($virtualNetwork.DdosProtectionPlan) { if ($virtualNetwork | Where-Object { $_.DdosProtectionPlan.Id -eq $DdosProtectionId }) { "Compliant" }else { "Not Compliant" } }else { "Not Compliant" }
if ($Remediate -and $Compliant -eq "Not Compliant") {
$virtualNetwork.DdosProtectionPlan = New-Object Microsoft.Azure.Commands.Network.Models.PSResourceId
$virtualNetwork.DdosProtectionPlan.Id = $DdosProtectionId
$virtualNetwork.EnableDdosProtection = $true
$Action = "Associating on Virtual Network: $($virtualNetwork.Name) the DDoS plan id: $DdosProtectionId"
$Command = { $virtualNetwork | Set-AzVirtualNetwork -ErrorAction Stop }
$Result = Generate_Log_Action -Action $Action -Command $Command -LogFile $logFile
if ($Result -eq "Error") { Exit 1 }
}
$virtualNetwork_array += virtualNetwork_array -AzureSubscriptionName $AzureRmSubscription.Name `
-Name $virtualNetwork.Name `
-ResourceGroupName $virtualNetwork.ResourceGroupName `
-Tag_application $(if ($virtualNetwork.Tag.Item("application")) { $virtualNetwork.Tag.Item("application") } else { "unknown tag key" }) `
-DdosProtectionPlan $Compliant
}
}
################################################################################
# Output
################################################################################
if ($Audit) {
$Action = "Exporting the Virtual Network summary audit result in to the file : $($workfolder + "\logs" + "\$date-Summary-$($MyInvocation.MyCommand.Name).csv")"
$Command = { $virtualNetwork_array | export-csv $($workfolder + "\logs" + "\$date-Summary-$($MyInvocation.MyCommand.Name).csv") -notypeinformation -ErrorAction Stop }
$Result = Generate_Log_Action -Action $Action -Command $Command -LogFile $logFile
if ($Result -eq "Error") { Exit 1 }
}