-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
Add_Remove_Files_Folders_Defender.ps1
212 lines (193 loc) · 6.74 KB
/
Add_Remove_Files_Folders_Defender.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Enable Controlled folder access and add protected folders
function AddProtectedFolders
{
$Title = $Localization.ControlledFolderAccess
$Message = $Localization.ProtectedFoldersRequest
$Add = $Localization.Add
$Skip = $Localization.Skip
$Options = "&$Add", "&$Skip"
$DefaultChoice = 1
do
{
$Result = $Host.UI.PromptForChoice($Title, $Message, $Options, $DefaultChoice)
switch ($Result)
{
"0"
{
Add-Type -AssemblyName System.Windows.Forms
$FolderBrowserDialog = New-Object -TypeName System.Windows.Forms.FolderBrowserDialog
$FolderBrowserDialog.Description = $Localization.FolderSelect
$FolderBrowserDialog.RootFolder = "MyComputer"
# Force move the open file dialog to the foreground
$Focus = New-Object -TypeName System.Windows.Forms.Form -Property @{TopMost = $true}
$FolderBrowserDialog.ShowDialog($Focus)
if ($FolderBrowserDialog.SelectedPath)
{
Set-MpPreference -EnableControlledFolderAccess Enabled
Add-MpPreference -ControlledFolderAccessProtectedFolders $FolderBrowserDialog.SelectedPath -Force
Write-Verbose -Message ("{0}" -f $FolderBrowserDialog.SelectedPath) -Verbose
}
}
"1"
{
Write-Verbose -Message $Localization.Skipped -Verbose
}
}
}
until ($Result -eq 1)
}
# Remove all added protected folders
function RemoveProtectedFolders
{
if ($null -ne (Get-MpPreference).ControlledFolderAccessProtectedFolders)
{
(Get-MpPreference).ControlledFolderAccessProtectedFolders | Format-Table -AutoSize -Wrap
Remove-MpPreference -ControlledFolderAccessProtectedFolders (Get-MpPreference).ControlledFolderAccessProtectedFolders -Force
Write-Verbose -Message $Localization.ProtectedFoldersListRemoved -Verbose
}
}
# Allow an app through Controlled folder access
function AddAppControlledFolder
{
$Title = $Localization.ControlledFolderAccess
$Message = $Localization.AppControlledFolderRequest
$Add = $Localization.Add
$Skip = $Localization.Skip
$Options = "&$Add", "&$Skip"
$DefaultChoice = 1
do
{
$Result = $Host.UI.PromptForChoice($Title, $Message, $Options, $DefaultChoice)
switch ($Result)
{
"0"
{
Add-Type -AssemblyName System.Windows.Forms
$OpenFileDialog = New-Object -TypeName System.Windows.Forms.OpenFileDialog
$OpenFileDialog.Filter = $Localization.EXEFilesFilter
$OpenFileDialog.InitialDirectory = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
$OpenFileDialog.Multiselect = $false
# Force move the open file dialog to the foreground
$Focus = New-Object -TypeName System.Windows.Forms.Form -Property @{TopMost = $true}
$OpenFileDialog.ShowDialog($Focus)
if ($OpenFileDialog.FileName)
{
Add-MpPreference -ControlledFolderAccessAllowedApplications $OpenFileDialog.FileName -Force
Write-Verbose -Message ("{0}" -f $OpenFileDialog.FileName) -Verbose
}
}
"1"
{
Write-Verbose -Message $Localization.Skipped -Verbose
}
}
}
until ($Result -eq 1)
}
# Remove all allowed apps through Controlled folder access
function RemoveAllowedAppsControlledFolder
{
if ($null -ne (Get-MpPreference).ControlledFolderAccessAllowedApplications)
{
(Get-MpPreference).ControlledFolderAccessAllowedApplications | Format-Table -AutoSize -Wrap
Remove-MpPreference -ControlledFolderAccessAllowedApplications (Get-MpPreference).ControlledFolderAccessAllowedApplications -Force
Write-Verbose -Message $Localization.AllowedControlledFolderAppsRemoved -Verbose
}
}
# Add a folder to the exclusion from Microsoft Defender scanning
function AddDefenderExclusionFolder
{
$Title = "Microsoft Defender"
$Message = $Localization.DefenderExclusionFolderRequest
$Add = $Localization.Add
$Skip = $Localization.Skip
$Options = "&$Add", "&$Skip"
$DefaultChoice = 1
do
{
$Result = $Host.UI.PromptForChoice($Title, $Message, $Options, $DefaultChoice)
switch ($Result)
{
"0"
{
Add-Type -AssemblyName System.Windows.Forms
$FolderBrowserDialog = New-Object -TypeName System.Windows.Forms.FolderBrowserDialog
$FolderBrowserDialog.Description = $Localization.FolderSelect
$FolderBrowserDialog.RootFolder = "MyComputer"
# Force move the open file dialog to the foreground
$Focus = New-Object -TypeName System.Windows.Forms.Form -Property @{TopMost = $true}
$FolderBrowserDialog.ShowDialog($Focus)
if ($FolderBrowserDialog.SelectedPath)
{
Add-MpPreference -ExclusionPath $FolderBrowserDialog.SelectedPath -Force
Write-Verbose -Message ("{0}" -f $FolderBrowserDialog.SelectedPath) -Verbose
}
}
"1"
{
Write-Verbose -Message $Localization.Skipped -Verbose
}
}
}
until ($Result -eq 1)
}
# Remove all excluded folders from Microsoft Defender scanning
function RemoveDefenderExclusionFolders
{
if ($null -ne (Get-MpPreference).ExclusionPath)
{
$ExcludedFolders = (Get-Item -Path (Get-MpPreference).ExclusionPath -Force -ErrorAction Ignore | Where-Object -FilterScript {$_.Attributes -match "Directory"}).FullName
$ExcludedFolders | Format-Table -AutoSize -Wrap
Remove-MpPreference -ExclusionPath $ExcludedFolders -Force
Write-Verbose -Message $Localization.DefenderExclusionFoldersListRemoved -Verbose
}
}
# Add a file to the exclusion from Microsoft Defender scanning
function AddDefenderExclusionFile
{
$Title = "Microsoft Defender"
$Message = $Localization.AddDefenderExclusionFileRequest
$Add = $Localization.Add
$Skip = $Localization.Skip
$Options = "&$Add", "&$Skip"
$DefaultChoice = 1
do
{
$Result = $Host.UI.PromptForChoice($Title, $Message, $Options, $DefaultChoice)
switch ($Result)
{
"0"
{
Add-Type -AssemblyName System.Windows.Forms
$OpenFileDialog = New-Object -TypeName System.Windows.Forms.OpenFileDialog
$OpenFileDialog.Filter = $Localization.AllFilesFilter
$OpenFileDialog.InitialDirectory = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
$OpenFileDialog.Multiselect = $false
# Force move the open file dialog to the foreground
$Focus = New-Object -TypeName System.Windows.Forms.Form -Property @{TopMost = $true}
$OpenFileDialog.ShowDialog($Focus)
if ($OpenFileDialog.FileName)
{
Add-MpPreference -ExclusionPath $OpenFileDialog.FileName -Force
Write-Verbose -Message ("{0}" -f $OpenFileDialog.FileName) -Verbose
}
}
"1"
{
Write-Verbose -Message $Localization.Skipped -Verbose
}
}
}
until ($Result -eq 1)
}
# Remove all excluded files from Microsoft Defender scanning
function RemoveDefenderExclusionFiles
{
if ($null -ne (Get-MpPreference).ExclusionPath)
{
$ExcludedFiles = (Get-Item -Path (Get-MpPreference).ExclusionPath -Force -ErrorAction Ignore | Where-Object -FilterScript {$_.Attributes -notmatch "Directory"}).FullName
$ExcludedFiles | Format-Table -AutoSize -Wrap
Remove-MpPreference -ExclusionPath $ExcludedFiles -Force
Write-Verbose -Message $Localization.DefenderExclusionFilesRemoved -Verbose
}
}