forked from Mauvlans/ADJoin-to-AADJoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMS_AD2AAD_Standalone.ps1
356 lines (279 loc) · 12.5 KB
/
MMS_AD2AAD_Standalone.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# ***************************************************************************
#
# Purpose: Script to Migrate AD join to Azure AD Join
#
# ------------- DISCLAIMER -------------------------------------------------
# This script code is provided as is with no guarantee or waranty concerning
# the usability or impact on systems and may be used, distributed, and
# modified in any way provided the parties agree and acknowledge the
# Microsoft or Microsoft Partners have neither accountabilty or
# responsibility for results produced by use of this script.
#
# Microsoft will not provide any support through any means.
# ------------- DISCLAIMER -------------------------------------------------
#
# ***************************************************************************
#prep Files
#net localgroup administrators /add "AzureAD\UserUpn"
#Start Transcription
Write-Output "Writing Local Files"
$path = $(Join-Path $env:ProgramData CustomScripts)
if (!(Test-Path $path)) {
New-Item -Path $path -ItemType Directory -Force -Confirm:$false
}
Start-Transcript -Path C:\ProgramData\CustomScripts\AD2AADJ.txt -NoClobber
# Script varibles
$DomainAdmin = "DOMAIN\USERNAME"
$DomainAdminPassword = "PASSWORD"
$TempUserPassword = "P@ssword!"
$TempUser = "MMSTemp"
function Add-Scripts {
# Post Reboot Script
$content = @'
Start-Transcript -Path C:\ProgramData\CustomScripts\AD2AADJ-R1.txt -NoClobber
Write-Output "Writing Run Once for Post Reboot 2"
$RunOnceKey = "HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce"
set-itemproperty $RunOnceKey "NextRun" ('C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe -executionPolicy Unrestricted -File ' + "C:\ProgramData\CustomScripts\PostRunOnce2.ps1")
# Install Provisioning PPKG
Install-ProvisioningPackage -PackagePath "C:\ProgramData\CustomScripts\AAD Join.ppkg" -ForceInstall -QuietInstall
Stop-Transcript
restart-computer
'@
# Creates PS1 and dumps to ProgramData\CustomScripts
Write-Output "Writing Local Files"
$path = $(Join-Path $env:ProgramData CustomScripts)
if (!(Test-Path $path)) {
New-Item -Path $path -ItemType Directory -Force -Confirm:$false
}
Out-File -FilePath $(Join-Path $env:ProgramData CustomScripts\PostRunOnce.ps1) -Encoding unicode -Force -InputObject $content -Confirm:$false
##############################################################################################################################################
# 2nd Post Reboot Script
$content = @'
Start-Transcript -Path C:\ProgramData\CustomScripts\AD2AADJ-R2.txt -NoClobber
Write-Output "Writing Run Once for Post Reboot "
$RunOnceKey = "HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce"
set-itemproperty $RunOnceKey "NextRun" ('C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe -executionPolicy Unrestricted -File ' + "C:\ProgramData\CustomScripts\PostRunOnce3.ps1")
Write-Output "Escrow current Numeric Key"
function Test-Bitlocker ($BitlockerDrive) {
#Tests the drive for existing Bitlocker keyprotectors
try {
Get-BitLockerVolume -MountPoint $BitlockerDrive -ErrorAction Stop
} catch {
Write-Output "Bitlocker was not found protecting the $BitlockerDrive drive. Terminating script!"
}
}
function Get-KeyProtectorId ($BitlockerDrive) {
#fetches the key protector ID of the drive
$BitLockerVolume = Get-BitLockerVolume -MountPoint $BitlockerDrive
$KeyProtector = $BitLockerVolume.KeyProtector | Where-Object { $_.KeyProtectorType -eq 'RecoveryPassword' }
return $KeyProtector.KeyProtectorId
}
function Invoke-BitlockerEscrow ($BitlockerDrive,$BitlockerKey) {
#Escrow the key into Azure AD
try {
BackupToAAD-BitLockerKeyProtector -MountPoint $BitlockerDrive -KeyProtectorId $BitlockerKey -ErrorAction SilentlyContinue
Write-Output "Attempted to escrow key in Azure AD - Please verify manually!"
} catch {
Write-Error "Debug"
}
}
#endregion functions
#region execute
$BitlockerVolumers = Get-BitLockerVolume
$BitlockerVolumers |
ForEach-Object {
$MountPoint = $_.MountPoint
$RecoveryKey = [string]($_.KeyProtector).RecoveryPassword
if ($RecoveryKey.Length -gt 5) {
$DriveLetter = $MountPoint
Write-Output $DriveLetter
Test-Bitlocker -BitlockerDrive $DriveLetter
$KeyProtectorId = Get-KeyProtectorId -BitlockerDrive $DriveLetter
Invoke-BitlockerEscrow -BitlockerDrive $DriveLetter -BitlockerKey $KeyProtectorId
}
}
# Remove AutoLogin
Write-Output "Remove user Auto Login"
$RegPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
Set-ItemProperty $RegPath "AutoAdminLogon" -Value "0" -type String
Stop-Transcript
restart-computer
'@
# Creates PS1 and dumps to ProgramData\CustomScripts
Write-Output "Writing Local Files"
$path = $(Join-Path $env:ProgramData CustomScripts)
if (!(Test-Path $path)) {
New-Item -Path $path -ItemType Directory -Force -Confirm:$false
}
Out-File -FilePath $(Join-Path $env:ProgramData CustomScripts\PostRunOnce2.ps1) -Encoding unicode -Force -InputObject $content -Confirm:$false
##############################################################################################################################################
# Post Reboot Script 3
$content = @'
Start-Transcript -Path C:\ProgramData\CustomScripts\AD2AADJ-R3.txt -NoClobber
$FileName = "C:\ProgramData\CustomScripts\AAD Join.ppkg"
if (Test-Path $FileName) {
Remove-Item $FileName
}
Remove-LocalUser -name MMSTemp
$RegPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
Set-ItemProperty $RegPath "AutoAdminLogon" -Value "0" -type String
Set-ItemProperty $RegPath "DefaultUsername" -Value "null" -type String
Set-ItemProperty $RegPath "DefaultPassword" -Value "null" -type String
Start-Process -FilePath "C:\Program Files (x86)\Microsoft OneDrive\OneDrive.exe"
Stop-Transcript
'@
# Creates PS1 and dumps to ProgramData\CustomScripts
Write-Output "Writing Local Files"
$path = $(Join-Path $env:ProgramData CustomScripts)
if (!(Test-Path $path)) {
New-Item -Path $path -ItemType Directory -Force -Confirm:$false
}
Out-File -FilePath $(Join-Path $env:ProgramData CustomScripts\PostRunOnce3.ps1) -Encoding unicode -Force -InputObject $content -Confirm:$false
##############################################################################################################################################
# User File Copy Script
$content = @'
Start-Transcript -Path C:\ProgramData\CustomScripts\AD2AADJ-UserCopy.txt -NoClobber
# ***************************************************************************
#
# Purpose: Script to copy folder to user profile
#
# ------------- DISCLAIMER -------------------------------------------------
# This script code is provided as is with no guarantee or waranty concerning
# the usability or impact on systems and may be used, distributed, and
# modified in any way provided the parties agree and acknowledge the
# Microsoft or Microsoft Partners have neither accountabilty or
# responsibility for results produced by use of this script.
#
# Microsoft will not provide any support through any means.
# ------------- DISCLAIMER -------------------------------------------------
#
# Robocopy Logic derived from https://newbedev.com/custom-robocopy-progress-bar-in-powershell
#
# Set-Owner Logic derived from https://learn-powershell.net/2014/06/24/changing-ownership-of-file-or-folder-using-powershell/
#
# Get-Folder Logic derived from https://stackoverflow.com/questions/25690038/how-do-i-properly-use-the-folderbrowserdialog-in-powershell
#
# ***************************************************************************
Function Get-Folder($initialDirectory="c:\Users\"){
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
$foldername = New-Object System.Windows.Forms.FolderBrowserDialog
$foldername.Description = "Select a folder"
$foldername.rootfolder = "MyComputer"
$foldername.SelectedPath = $initialDirectory
if($foldername.ShowDialog() -eq "OK")
{
$folder += $foldername.SelectedPath
}
return $folder
}
function Set-Owner {
# Define the owner account/group
$Account = New-Object -TypeName System.Security.Principal.NTAccount -ArgumentList 'BUILTIN\Administrators';
# Get a list of folders and files
$ItemList = Get-ChildItem -Path $ProfileFolder -Recurse;
# Iterate over files/folders
foreach ($Item in $ItemList) {
$Acl = $null; # Reset the $Acl variable to $null
$Acl = Get-Acl -Path $Item.FullName; # Get the ACL from the item
$Acl.SetOwner($Account); # Update the in-memory ACL
Set-Acl -Path $Item.FullName -AclObject $Acl; # Set the updated ACL on the target item
}
}
function Copy-Folder {
Set-Location "C:\Windows\System32"
.\Robocopy.exe $ProfileFolder $Destination /E /XC /XN /XO /r:0 /XD "$ProfileFolder\Appdata"
#.\robocopy $ProfileFolder $Destination /NDL /NJH /NJS /E /XC /XN /XO /r:0 /XD "$ProfileFolder\Appdata" | ForEach-Object{$data = $_.Split([char]9); if("$($data[4])" -ne "") { $file = "$($data[4])"} ;Write-Progress "Percentage $($data[0])" -Activity "Robocopy" -CurrentOperation "$($file)" -ErrorAction SilentlyContinue; }
}
$ProfileFolder = Get-Folder
$Destination = $env:USERPROFILE
Set-Owner
Copy-Folder
Stop-Transcript
'@
# Creates PS1 and dumps to ProgramData\CustomScripts
Write-Output "Writing Local Files"
$path = $(Join-Path $env:ProgramData CustomScripts)
if (!(Test-Path $path)) {
New-Item -Path $path -ItemType Directory -Force -Confirm:$false
}
Out-File -FilePath $(Join-Path $env:ProgramData CustomScripts\Copy-Userprofile.ps1) -Encoding unicode -Force -InputObject $content -Confirm:$false
}
function Move-PPKG {
#Copy PPKG to Localfolder
$path = $(Join-Path $env:ProgramData CustomScripts)
if (!(Test-Path $path)) {
New-Item -Path $path -ItemType Directory -Force -Confirm:$false
}
#Copy-Item -Path ".\AAD Join.ppkg" -Destination "C:\ProgramData\CustomScripts\AAD Join.ppkg"
Copy-Item -Path "$PSScriptRoot\AAD Join.ppkg" -Destination "C:\ProgramData\CustomScripts\AAD Join.ppkg"
}
function Add-LocalUser {
# Create Local Account
Write-Output "Creating Local User Account"
$Password = ConvertTo-SecureString -AsPlainText $TempUserPassword -force
New-LocalUser -Name $TempUser -Password $Password -Description "account for autologin" -AccountNeverExpires
Add-LocalGroupMember -Group "Administrators" -Member $TempUser
}
function Set-Autologin {
# Set Auto login Regestry
Write-Output "Setting user account to Auto Login"
$RegPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
Set-ItemProperty $RegPath "AutoAdminLogon" -Value "1" -type String -Verbose
Set-ItemProperty $RegPath "DefaultUsername" -Value $TempUser -type String -Verbose
Set-ItemProperty $RegPath "DefaultPassword" -Value $TempUserPassword -type String -Verbose
}
function Disable-OOBEPrivacy {
# Set variables to indicate value and key to set
$RegistryPath = 'HKCU:\Software\Policies\Microsoft\Windows\OOBE'
$Name = 'DisablePrivacyExperience'
$Value = '1'
# Create the key if it does not exist
If (-NOT (Test-Path $RegistryPath)) {
New-Item -Path $RegistryPath -Force | Out-Null
}
# Now set the value
New-ItemProperty -Path $RegistryPath -Name $Name -Value $Value -PropertyType DWORD -Force -Verbose
# Set variables to indicate value and key to set
$RegistryPath = 'HKLM:\Software\Policies\Microsoft\Windows\OOBE'
$Name = 'DisablePrivacyExperience'
$Value = '1'
# Create the key if it does not exist
If (-NOT (Test-Path $RegistryPath)) {
New-Item -Path $RegistryPath -Force | Out-Null
}
# Now set the value
New-ItemProperty -Path $RegistryPath -Name $Name -Value $Value -PropertyType DWORD -Force -Verbose
}
function Set-RunOnce {
# Set Run Once Regestry
Write-Host "Changing RunOnce script."
$RunOnceKey = "HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce"
set-itemproperty $RunOnceKey "NextRun" ('C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe -executionPolicy Unrestricted -File ' + "C:\ProgramData\CustomScripts\PostRunOnce.ps1") -Verbose
}
function Set-Bitlocker {
Suspend-BitLocker -MountPoint "C:" -RebootCount 3 -Verbose
}
function Remove-Hybrid {
#Remove machine from Hybrid Join
.\C:\Windows\System32\dsregcmd.exe /leave
}
function Remove-ADJoin {
# Remove Machines from AD
Write-Verbose "Removing computer from domain and forcing restart"
$pw = $DomainAdminPassword | ConvertTo-SecureString -asPlainText -Force
$usr = $DomainAdmin
$pc = "localhost"
$creds = New-Object System.Management.Automation.PSCredential($usr, $pw)
Stop-Transcript
Remove-Computer -ComputerName $pc -Credential $creds -Verbose -Restart -Force
}
# Main Logic
Add-Scripts
Move-PPKG
Add-LocalUser
Set-Autologin
Disable-OOBEPrivacy
Set-RunOnce
Set-Bitlocker
Remove-Hybrid
Remove-ADJoin