-
Notifications
You must be signed in to change notification settings - Fork 15
/
Remediate-AutoTriggerDisabledProfile.ps1
203 lines (134 loc) · 5.58 KB
/
Remediate-AutoTriggerDisabledProfile.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
<#
.SYNOPSIS
PowerShell script to remediate an Always On VPN profile listed in the AutoTriggerDisbledProfiles list. This script is designed to be deployed using Intune remediations. In addition, it requires the user to have administrative rights when remediating a user tunnel profile not deployed in the All Users context.
.EXAMPLE
.\Remediate-AutoTriggerDisabledProfile.ps1
.DESCRIPTION
When a user unchecks the 'Connect Automatically' box on their Always On VPN profile, the VPN profile is added to the AutoTriggerDisabledProfilesList registry key. This prevents the VPN profile from automatically connecting when the user's device starts up or when the user signs in. This PowerShell script remediates the issue by removing the VPN profile from the AutoTriggerDisabledProfilesList registry key.
.LINK
https://github.com/richardhicks/endpointmanager/blob/main/Remediate-AutoTriggerDisabledProfile.ps1
.LINK
https://learn.microsoft.com/en-us/mem/intune/fundamentals/remediations
.LINK
https://directaccess.richardhicks.com/
.NOTES
Version: 1.1
Creation Date: December 29, 2023
Last Updated: February 22, 2024
Author: Richard Hicks
Organization: Richard M. Hicks Consulting, Inc.
Contact: rich@richardhicks.com
Website: https://directaccess.richardhicks.com/
#>
[CmdletBinding()]
Param (
# Update the $ProfileName value to reflect the name of the Always On VPN profile to remove from the AutoTriggerDisabledProfilesList registry key
[string]$ProfileName = 'Always On VPN',
# Remove the comment in the next line to remediate the profile in the 'all users' context
[switch]$AllUserConnection # = [switch]::Present
)
# Enable logging
Start-Transcript -Path $env:TEMP\Remediate-AutoTriggerDisabledProfile.log
# Use transaction for registry updates
Start-Transaction
# Search AutoTriggerDisabledProfilesList for VPN profile
$Path = 'HKLM:\System\CurrentControlSet\Services\RasMan\Config\'
$Name = 'AutoTriggerDisabledProfilesList'
Write-Verbose "Searching $Name in $Path for VPN profile `"$ProfileName`"..."
Try {
# Get the current registry values as an array of strings
[string[]]$DisabledProfiles = Get-ItemPropertyValue -Path $Path -Name $Name -ErrorAction Stop
}
Catch {
Write-Verbose "$Name does not exist in $Path. No action required."
Return
}
If ($DisabledProfiles) {
# Create ordered hashtable
$List = [Ordered]@{}
$DisabledProfiles | ForEach-Object { $List.Add("$($_.ToLower())", $_) }
# Search hashtable for matching VPN profile and remove if present
If ($List.Contains($ProfileName)) {
Write-Verbose 'Profile found. Removing entry...'
$List.Remove($ProfileName)
Write-Verbose 'Updating the registry...'
Set-ItemProperty -Path $Path -Name $Name -Value $List.Values -UseTransaction
}
}
Else {
Write-Verbose "No profiles found matching `"$ProfileName`"."
Return
}
# Add user SID to registry
If ($AllUserConnection) {
$SID = 'S-1-1-0'
Write-Verbose "Adding SYSTEM SID $SID to registry..."
}
Else {
Try {
$SID = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value
Write-Verbose "Adding user SID $SID to registry..."
}
Catch {
Write-Warning $_.Exception.Message
Return
}
}
$Parameters = @{
Path = 'HKLM:\SYSTEM\CurrentControlSet\Services\RasMan\Config\'
Name = 'UserSID'
PropertyType = 'String'
Value = $SID
UseTransaction = $True
}
New-ItemProperty @Parameters -Force | Out-Null
# Add VPN profile name to registry
$Parameters = @{
Path = 'HKLM:\SYSTEM\CurrentControlSet\Services\RasMan\Config\'
Name = 'AutoTriggerProfileEntryName'
PropertyType = 'String'
Value = $ProfileName
UseTransaction = $True
}
New-ItemProperty @Parameters | Out-Null
# Add VPN profile GUID to registry
Write-Verbose "Adding VPN GUID $GUID to registry..."
[guid]$Guid = $Vpn | Select-Object -ExpandProperty Guid
$Binary = $Guid.ToByteArray()
$Parameters = @{
Path = 'HKLM:\SYSTEM\CurrentControlSet\Services\RasMan\Config\'
Name = 'AutoTriggerProfileGUID'
PropertyType = 'Binary'
Value = $Binary
UseTransaction = $True
}
New-ItemProperty @Parameters | Out-Null
# Add phonebook path to registry
If ($AllUserConnection) {
$Path = Join-Path -Path $env:programdata -ChildPath Microsoft\Network\Connections\Pbk\rasphone.pbk
Write-Verbose "RAS phonebook path is $Path."
}
Else {
$Path = Join-Path -Path $env:userprofile -ChildPath AppData\Roaming\Microsoft\Network\Connections\Pbk\rasphone.pbk
Write-Verbose "RAS phonebook path is $Path."
}
$Parameters = @{
Path = 'HKLM:\SYSTEM\CurrentControlSet\Services\RasMan\Config\'
Name = 'AutoTriggerProfilePhonebookPath'
PropertyType = 'String'
Value = $Path
UseTransaction = $True
}
New-ItemProperty @Parameters | Out-Null
# Commit registry changes
Complete-Transaction
# Stop the RasMan service
$Id = Get-CimInstance -ClassName win32_service | Where-Object Name -eq 'RasMan' | Select-Object -ExpandProperty ProcessId
Write-Verbose "RasMan process ID is $Id..."
Write-Verbose 'Restarting the RasMan service...'
Stop-Process -Id $Id -Force
# Pause before restarting the RasMan service
Start-Sleep -Seconds 5
Start-Service RasMan
# Stop logging
Stop-Transcript