-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDetect-Remediate-Disable-Prevent-New-Outlook.ps1
More file actions
315 lines (309 loc) · 13.3 KB
/
Copy pathDetect-Remediate-Disable-Prevent-New-Outlook.ps1
File metadata and controls
315 lines (309 loc) · 13.3 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<#
.SYNOPSIS
This script disables and prevents the migration to the new Outlook for logged-on users by modifying specific registry keys.
.DESCRIPTION
The script performs the following actions:
1. Detects the logged-on users and retrieves their SIDs.
2. Checks the registry keys for each logged-on user to ensure they match the expected values.
3. If discrepancies are found, the script can optionally remediate the registry keys to enforce the expected values.
.PARAMETER runDetection
Indicates whether the script should run the detection phase. Default is $true.
.PARAMETER runRemediation
Indicates whether the script should run the remediation phase if discrepancies are found. Default is $true.
.FUNCTIONS
- Test-RegistryKeyValue: Checks if a registry key value exists.
- Get-RegistryKeyValue: Retrieves the value of a registry key.
- Remove-RegistryKeyValue: Removes a registry key value.
- Install-RegistryKey: Creates a registry key if it does not exist.
- Set-RegistryKeyValue: Sets the value of a registry key.
- Invoke-RegistryComplianceCheck: Checks and optionally remediates a registry key value.
- Get-LoggedOnUser: Retrieves the username of the logged-on user.
- Get-AccountSID: Retrieves the SID of a user.
.EXAMPLE
.\Disable-Prevent-New-Outlook.ps1 -runDetection $true -runRemediation $true
This command runs the script with both detection and remediation phases enabled.
.NOTES
- The script requires administrative privileges to modify registry keys.
- Filename: Detect-Remediate-Disable-Prevent-New-Outlook.ps1
- Author: Martin Bengtsson
- Blog: www.imab.dk
- Twitter: @mwbengtsson
#>
#Requires -RunAsAdministrator
[CmdletBinding()]
param (
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[bool]$runDetection = $true,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[bool]$runRemediation = $true
)
begin {
#region Functions to modify registry keys
function Test-RegistryKeyValue() {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$registryPath,
[Parameter(Mandatory=$true)]
[string]$registryName
)
if (-NOT(Test-Path -Path $registryPath -PathType Container)) {
return $false
}
$registryProperties = Get-ItemProperty -Path $registryPath
if (-NOT($registryProperties)) {
return $false
}
$member = Get-Member -InputObject $registryProperties -Name $registryName
if (-NOT([string]::IsNullOrEmpty($member))) {
return $true
}
else {
return $false
}
}
function Get-RegistryKeyValue() {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$registryPath,
[Parameter(Mandatory=$true)]
[string]$registryName,
[Parameter(Mandatory=$false)]
[string]$registryType
)
if (-NOT(Test-RegistryKeyValue -registryPath $registryPath -registryName $registryName)) {
return $null
}
if ($registryType -eq "Binary") {
$value = [string]::join(' ',((Get-ItemProperty -Path $registryPath -Name $registryName).$registryName|ForEach-Object{'{0:x2}' -f $_}))
$value = $value.Replace(' ',',')
return $value
}
$registryProperties = Get-ItemProperty -Path $registryPath -Name *
$value = $registryProperties.$registryName
return $value
}
function Remove-RegistryKeyValue() {
[CmdletBinding(SupportsShouldProcess=$true)]
param (
[Parameter(Mandatory=$true)]
[string]$registryPath,
[Parameter(Mandatory=$true)]
[string]$registryName
)
if (Test-RegistryKeyValue -registryPath $registryPath -registryName $registryName) {
if ($pscmdlet.ShouldProcess(('Item: {0} Property: {1}' -f $registryPath,$registryName),'Remove Property')) {
Remove-ItemProperty -Path $registryPath -Name $registryName
}
}
}
function Install-RegistryKey() {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]
$registryPath
)
if (-NOT(Test-Path -Path $registryPath -PathType Container)) {
New-Item -Path $registryPath -ItemType RegistryKey -Force | Out-String | Write-Verbose
}
}
function Set-RegistryKeyValue() {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$registryPath,
[Parameter(Mandatory=$true)]
[string]$registryName,
[Parameter(Mandatory=$true,ParameterSetName='String')]
[AllowEmptyString()]
[AllowNull()]
[string]$String,
[Parameter(ParameterSetName='String')]
[switch]$Expand,
[Parameter(Mandatory=$true,ParameterSetName='Binary')]
[string]$Binary,
[Parameter(Mandatory=$true,ParameterSetName='DWord')]
[int]$DWord,
[Parameter(Mandatory=$true,ParameterSetName='DWordAsUnsignedInt')]
[uint32]$UDWord,
[Parameter(Mandatory=$true,ParameterSetName='QWord')]
[long]$QWord,
[Parameter(Mandatory=$true,ParameterSetName='QWordAsUnsignedInt')]
[uint64]$UQWord,
[Parameter(Mandatory=$true,ParameterSetName='MultiString')]
[string[]]$Strings,
[switch]$Force
)
$value = $null
$type = $pscmdlet.ParameterSetName
switch -Exact ($pscmdlet.ParameterSetName) {
'String' {
$value = $String
if ($Expand) {
$type = 'ExpandString'
}
}
'Binary' { [byte[]]$value = $Binary.Split(',') | ForEach-Object { "0x$_"} }
'DWord' { $value = $DWord }
'QWord' { $value = $QWord }
'DWordAsUnsignedInt' {
$value = $UDWord
$type = 'DWord'
}
'QWordAsUnsignedInt' {
$value = $UQWord
$type = 'QWord'
}
'MultiString' { $value = $Strings }
}
Install-RegistryKey -registryPath $registryPath
if ($Force) {
Remove-RegistryKeyValue -registryPath $registryPath -registryName $registryName
}
if (Test-RegistryKeyValue -registryPath $registryPath -registryName $registryName) {
$currentValue = Get-RegistryKeyValue -registryPath $registryPath -registryName $registryName
if ($currentValue -ne $value) {
Set-ItemProperty -Path $registryPath -Name $registryName -Value $value
}
}
else {
$null = New-ItemProperty -Path $registryPath -Name $registryName -Value $value -PropertyType $type
}
}
function Invoke-RegistryComplianceCheck() {
param (
[Parameter(Mandatory=$true)]
[string]$registryPath,
[Parameter(Mandatory=$true)]
[string]$registryName,
[Parameter(Mandatory=$true)]
[string]$registryExpectedValue,
[Parameter(Mandatory=$true)]
[string]$registryExpectedType
)
if ($registryExpectedType -eq "Binary") {
$getValue = Get-RegistryKeyValue -registryPath $registryPath -registryName $registryName -registryType $registryExpectedType
}
else {
$getValue = Get-RegistryKeyValue -registryPath $registryPath -registryName $registryName
}
if (($getValue -ne $registryExpectedValue) -or ($null -eq $getValue)) {
if ($null -eq $getValue) { $getValue = "null. (key does not exist)" }
if ($runDetection -eq $true) { $global:needsRemediation += $true }
Write-Output "[WARNING] Value of registry key: $registryPath\$registryName is not as expected. Value is: $getValue. Expected value is: $registryExpectedValue."
if ($runRemediation -eq $true) {
Write-Output "[INFO] runRemediation is set to $runRemediation. Remediation for $registryPath\$registryName will run."
try {
switch ($registryExpectedType) {
"String" { Set-RegistryKeyValue -registryPath $registryPath -registryName $registryName -String $registryExpectedValue -Force -ErrorAction Stop }
"Binary" { Set-RegistryKeyValue -registryPath $registryPath -registryName $registryName -Binary $registryExpectedValue -Force -ErrorAction Stop }
"Dword" { Set-RegistryKeyValue -registryPath $registryPath -registryName $registryName -DWord $registryExpectedValue -Force -ErrorAction Stop }
"MultiString" { Set-RegistryKeyValue -registryPath $registryPath -registryName $registryName -Strings $registryExpectedValue -Force -ErrorAction Stop }
default { throw "Unsupported registry type: $registryExpectedType" }
}
$getValue = Get-RegistryKeyValue -registryPath $registryPath -registryName $registryName
if ($getValue -eq $registryExpectedValue) {
Write-Output "[INFO] Remediation was successful. Value of registry key: $registryPath\$registryName is now as expected."
$global:remediationSuccess += $true
}
}
catch {
Write-Output "[ERROR] An error occurred while trying to remediate $registryPath\$registryName. Error: $_"
$global:remediationSuccess += $false
}
}
}
else {
$global:needsRemediation += $false
}
}
function Get-LoggedOnUser() {
try {
$loggedOnUser = (Get-Process -Name explorer -IncludeUserName | Select-Object UserName -Unique).UserName
if (-NOT[string]::IsNullOrEmpty($loggedOnUser)) {
Write-Output $loggedOnUser
}
else {
$null = $loggedOnUser
}
}
catch {
Write-Output "[ERROR] Failed to retrieve the username of the logged on user: $_"
}
}
function Get-AccountSID() {
param (
[string]$commonName
)
try {
$account = New-Object System.Security.Principal.NTAccount($commonName)
$accountSID = $account.Translate([System.Security.Principal.SecurityIdentifier])
if (-NOT[string]::IsNullOrEmpty($accountSID)) {
Write-Output $accountSID
}
else {
$null = $accountSID
}
}
catch {
Write-Output "[ERROR] Failed to retrieve the SID of the user: $_"
}
}
#endregion
if ($runDetection -eq $false) {
Write-Output "[ERROR] runDetection cannot be set to false. As a minimum runDetection must be set to true."
exit 1
}
}
process {
#region Execution
$global:needsRemediation = @()
$global:remediationSuccess = @()
$loggedonUserSID = @()
$loggedOnUsers = Get-LoggedOnUser
foreach ($loggedOnUser in $loggedOnUsers) {
$loggedonUserSID += Get-AccountSID -commonName $loggedOnUser
}
foreach ($userSID in $loggedonUserSID) {
$registryEntries = @(
@{
# Hide the new Outlook toggle
Path = "Registry::HKEY_USERS\$($userSID)\SOFTWARE\Microsoft\Office\16.0\Outlook\Options\General"
Name = "HideNewOutlookToggle"
Value = "1"
Type = "Dword"
},
@{
# Prevent migrating to new Outlook
Path = "Registry::HKEY_USERS\$($userSID)\SOFTWARE\Policies\Microsoft\office\16.0\outlook\preferences"
Name = "NewOutlookMigrationUserSetting"
Value = "0"
Type = "Dword"
}
)
foreach ($registryEntry in $registryEntries) {
Invoke-RegistryComplianceCheck -registryPath $registryEntry.Path -registryName $registryEntry.Name -registryExpectedValue $registryEntry.Value -registryExpectedType $registryEntry.Type
}
}
#endregion
}
end {
if ($runDetection -eq $true) {
if ($global:needsRemediation -contains $true -AND $global:remediationSuccess -notcontains $true) {
Write-Output "[WARNING] Values of one or more policies in registry are not as expected. Remediation is needed."
exit 1
}
elseif ($global:remediationSuccess -contains $true -AND $global:remediationSuccess -notcontains $false) {
Write-Output "[OK] Remediation was run successfully. Values of all policies in registry are now as expected"
exit 0
}
else {
Write-Output "[OK] Values of all policies in registry are as expected. Doing nothing."
exit 0
}
}
}