forked from Azure/Microsoft-Defender-for-Cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAzureGuestPolicyHelper.psm1
156 lines (134 loc) · 5.33 KB
/
AzureGuestPolicyHelper.psm1
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
function New-EPDSCAzureGuestConfigurationPolicyPackage
{
[CmdletBinding()]
param()
$ResourceGroupName = Read-Host "Resource Group Name"
$ResourceGroupLocation = Read-Host "Location"
$storageContainerName = Read-Host "Storage Container Name"
$storageAccountName = Read-Host "Storage Account Name"
$storageSKUName = Read-Host "Storage SKU Name"
if ([System.String]::IsNullOrEmpty($storageSKUName))
{
$storageSKUName = "Standard_LRS"
}
if ([System.String]::IsNullOrEmpty($ResourceGroupLocation))
{
$storageSKUName = "eastus"
}
Write-Host "Connecting to Azure..." -NoNewLine
Connect-AzAccount | Out-Null
Write-Host "Done" -ForegroundColor Green
Write-Host "Compiling Configuration into a MOF file..." -NoNewLine
if (Test-Path 'MonitorAntivirus')
{
Remove-Item "MonitorAntivirus" -Recurse -Force -Confirm:$false
}
& "$PSScriptRoot/Configurations/MonitorAntivirus.ps1" | Out-Null
Write-Host "Done" -ForegroundColor Green
Write-Host "Generating Guest Configuration Package..." -NoNewLine
$package = New-GuestConfigurationPackage -Name MonitorAntivirus `
-Configuration "$env:Temp/MonitorAntivirus/MonitorAntivirus.mof"
Write-Host "Done" -ForegroundColor Green
Write-Host "Publishing Package to Azure Storage..." -NoNewLine
$Url = Publish-EPDSCPackage -ResourceGroupName $ResourceGroupName `
-StorageAccountName $StorageAccountName.ToLower() `
-StorageContainerName $StorageContainerName.ToLower() `
-StorageSKUName $StorageSKUName `
-ResourceGroupLocation $ResourceGroupLocation
Write-Host "Done" -ForegroundColor Green
Write-Host "Generating Guest Configuration Policy..." -NoNewLine
if (Test-Path 'policies')
{
Remove-Item "policies" -Recurse -Force -Confirm:$false
}
Import-LocalizedData -BaseDirectory "$PSScriptRoot/ParameterFiles/" `
-FileName "EPAntivirusStatus.Params.psd1" `
-BindingVariable ParameterValues
$policy = New-GuestConfigurationPolicy `
-ContentUri $Url `
-DisplayName 'Monitor Antivirus' `
-Description 'Audit if a given Antivirus Software is not enabled on Windows machine.' `
-Path './policies' `
-Platform 'Windows' `
-Version 1.0.0 `
-Parameter $ParameterValues -Verbose
Write-Host "Done" -ForegroundColor Green
Write-Host "Publishing Guest Configuration Policy..." -NoNewLine
$publishedPolicies = Publish-GuestConfigurationPolicy -Path ".\policies" -Verbose
Write-Host "Done" -ForegroundColor Green
}
function Publish-EPDSCPackage
{
[CmdletBinding()]
[OutputType([System.String])]
param(
[Parameter(Mandatory = $true)]
[System.String]
$ResourceGroupName,
[Parameter(Mandatory = $true)]
[System.String]
$StorageAccountName,
[Parameter(Mandatory = $true)]
[System.String]
$StorageContainerName,
[Parameter(Mandatory = $true)]
[System.String]
$StorageSKUName,
[Parameter(Mandatory = $true)]
[System.String]
$ResourceGroupLocation
)
$resourceGroup = Get-AzResourceGroup $ResourceGroupName -ErrorAction "SilentlyContinue"
if ($null -eq $resourceGroup)
{
$resourceGroup = New-AzResourceGroup -Name $ResourceGroupName `
-Location $ResourceGroupLocation
}
$storageAccount = Get-AzStorageAccount -Name $StorageAccountName `
-ResourceGroupName $ResourceGroupName -ErrorAction "SilentlyContinue"
if ($null -eq $storageAccount)
{
$storageAccount = New-AzStorageAccount -Name $StorageAccountName `
-ResourceGroupName $ResourceGroupName `
-SkuName $StorageSKUName `
-Location $ResourceGroupLocation
}
# Get Storage Context
$storageContext = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName `
-Name $StorageAccountName | `
ForEach-Object { $_.Context }
$storageContainer = Get-AzStorageContainer $StorageContainerName `
-Context $storageContext -ErrorAction "SilentlyContinue"
if ($null -ne $storageContainer)
{
while ($null -ne $storageContainer)
{
Start-Sleep 2
$storageContainer = Get-AzStorageContainer $StorageContainerName `
-Context $storageContext -ErrorAction "SilentlyContinue"
}
Remove-AzStorageContainer -Name $StorageContainerName `
-Context $storageContext -Force -Confirm:$false
}
$storageContainer = New-AzStorageContainer -Name $StorageContainerName `
-Context $storageContext -Permission Container
# Upload file
$blobName = "MonitorAntivirus.zip"
$Blob = Set-AzStorageBlobContent -Context $storageContext `
-Container $StorageContainerName `
-File $($env:Temp + "/MonitorAntivirus/MonitorAntivirus.zip") `
-Blob $blobName `
-Force
# Get url with SAS token
$StartTime = (Get-Date)
$ExpiryTime = $StartTime.AddYears('3') # THREE YEAR EXPIRATION
$SAS = New-AzStorageBlobSASToken -Context $storageContext `
-Container $StorageContainerName `
-Blob $blobName `
-StartTime $StartTime `
-ExpiryTime $ExpiryTime `
-Permission rl `
-FullUri
# Output
return $SAS
}