-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-usmt.ps1
91 lines (73 loc) · 3.93 KB
/
auto-usmt.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
# Function to get the size of a folder
function Get-FolderSize {
param ([string]$Path)
(Get-ChildItem -Path $Path -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum / 1GB
}
# Function to Export Certificates
function Export-Certificates {
param (
[string]$ExportPath,
[System.Security.SecureString]$ExportPassword
)
$exportType = [System.Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store "My", "CurrentUser"
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadOnly)
foreach ($cert in $store.Certificates) {
if ($cert.HasPrivateKey) {
$exportPathFull = Join-Path $ExportPath ($cert.Thumbprint + ".pfx")
$exportedBytes = $cert.Export($exportType, $ExportPassword)
[System.IO.File]::WriteAllBytes($exportPathFull, $exportedBytes)
Write-Host "Certificate exported: $exportPathFull"
}
}
$store.Close()
}
# USMT Configuration
$USMTPath = "C:\Users\Public\profile_USMT\domain_usmt_bin"
$USMTStorePath = "C:\Users\Public\profile_USMT"
# Get computer information and check domain
$hostname = Get-ComputerInfo | Select-Object -ExpandProperty CsName
$checkdomain = (Get-WmiObject -Class Win32_ComputerSystem).Domain
# Example Variables
$exampleDomain = "domain.dom"
$exampleOrganization = "DOMAIN"
# Domain check and migration process
if ($checkdomain -match $exampleDomain) {
Write-Host "The computer is already in the $exampleOrganization domain."
# Request user information for migration
$actualuser = Read-Host -Prompt "Enter the username (current, local) you want to migrate"
$newuser = Read-Host -Prompt "Enter the username (new, domain) where the profile will be migrated"
# Check disk space before migration
$UserProfilePath = "C:\Users\$actualuser"
$UserProfileSizeGB = Get-FolderSize -Path $UserProfilePath
$RequiredSpaceGB = $UserProfileSizeGB * 2 # Estimate required space
$FreeSpaceGB = (Get-PSDrive C).Free / 1GB
if ($FreeSpaceGB -lt $RequiredSpaceGB) {
Write-Host "Not enough disk space to continue. Free space: $FreeSpaceGB GB. Required space: $RequiredSpaceGB GB."
exit
}
# Export Certificates
$ExportPassword = Read-Host "Enter a secure password to export the certificates" -AsSecureString
Export-Certificates -ExportPath $USMTStorePath -ExportPassword $ExportPassword
# Run ScanState
$ScanStateCommand = "$USMTPath\ScanState.exe $USMTStorePath /ue:`"$hostname\*`" /ue:`"$exampleOrganization\*`" /ui:`"$hostname\$actualuser`" /i:$USMTPath\MigDocs.xml /i:$USMTPath\MigApp.xml /i:$USMTPath\MigUser.xml /i:$USMTPath\only_c.xml /v:13 /l:$USMTPath\ScanState.log /localonly /efs:copyraw /o"
Invoke-Expression $ScanStateCommand
# Run LoadState
$LoadStateCommand = "$USMTPath\LoadState.exe $USMTStorePath /mu:`"$hostname\$actualuser`":`"$exampleOrganization\$newuser`" /i:$USMTPath\MigDocs.xml /i:$USMTPath\MigApp.xml /i:$USMTPath\MigUser.xml /v:13 /l:$USMTPath\LoadState.log /c"
Invoke-Expression $LoadStateCommand
Write-Host "USMT migration process completed."
} else {
$askadddomain = Read-Host -Prompt "The computer is not in the $exampleOrganization domain. Type 'y' to add it to the domain. Press 'n' to cancel the process"
if ($askadddomain -match 'y') {
$addomain = $exampleDomain
$adUserName = "domain\administrator"
$adPassword = "Password" | ConvertTo-SecureString -AsPlainText -Force
$adCredential = New-Object System.Management.Automation.PSCredential -ArgumentList $adUserName, $adPassword
Add-Computer -DomainName $addomain -DomainCredential $adCredential -Restart -Verbose
Write-Host "The computer will be added to the $exampleOrganization domain and will restart."
}
if ($askadddomain -match 'n') {
Write-Host "Process canceled."
exit
}
}