-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathMigrate-All.ps1
More file actions
178 lines (95 loc) · 3.86 KB
/
Migrate-All.ps1
File metadata and controls
178 lines (95 loc) · 3.86 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
# Functions
function Set-XMLAttributeValue
{
param ([Parameter(Mandatory=$true)][string]$XML, [Parameter(Mandatory=$true)][string]$Attribute, [Parameter(Mandatory=$true)][string]$Value)
$Search = "<$Attribute>"
$Start = $XML.IndexOf($Search)
If($Start -eq -1){
return $XML
}
$End = $XML.IndexOf("</$Attribute>")
$Pre = $XML.Substring(0, $Start + $Search.Length)
$Post = $XML.Substring($End)
$New = $Pre + $Value + $Post
return $New
}
function Get-XMLAttributeValue
{
param ([Parameter(Mandatory=$true)][string]$XML, [Parameter(Mandatory=$true)][string]$Attribute)
$Search = "<$Attribute>"
$Start = $XML.IndexOf($Search)
If($Start -eq -1){
return
}
$End = $XML.IndexOf("</$Attribute>")
$Value = $XML.Substring($Start + $Search.Length, $End - ($Start + $Search.Length))
return $Value
}
#Set variables in this section
#Enter the flag file name
$MachineFlagName="ForensiTMigrated"
#Set $Debug to true to see debug messages
$Debug=$true
# This script will write a flag file if it has already been run successfully. Here we check if the flag file exists.
$FlagFile = [Environment]::GetFolderPath([Environment+SpecialFolder]::ApplicationData) + "\" + $MachineFlagName
If(Test-Path -Path $FlagFile){
if($Debug) {
$delete = Read-Host "The 'migrated' flag file has been set. Do you want to delete the flag file and continue? (Y/n)"
if($delete -eq '' -Or $delete -eq 'Y' -Or $delete -eq 'Yes'){
Remove-item $FlagFile
}
else{
exit
}
}
else{
exit
}
}
# Copy the migration files locally to C:\ProgramData\ForensiT\Migrate
$TargetFolder = [Environment]::GetFolderPath([Environment+SpecialFolder]::CommonApplicationData) + "\\ForensiT\\Migrate"
New-Item -Path $TargetFolder -ItemType directory -Force | Out-Null
Copy-Item ".\Profwiz.exe" "$TargetFolder\\Profwiz.exe"
$Config = [String](Get-Content -Path ".\Profwiz.config" -Raw)
# User lookup file
$Value = Get-XMLAttributeValue $Config "UserLookupFile"
If(-Not [string]::IsNullOrEmpty($Value)){
$LookFile = Split-Path $Value -leaf
$Config = Set-XMLAttributeValue $Config "UserLookupFile" $LookFile
Copy-Item $Value "$TargetFolder\\$LookFile"
}
# Device Lookup File
$Value = Get-XMLAttributeValue $Config "MachineLookupFile"
If(-Not [string]::IsNullOrEmpty($Value)){
$LookFile = Split-Path $Value -leaf
$Config = Set-XMLAttributeValue $Config "MachineLookupFile" $LookFile
Copy-Item $Value "$TargetFolder\\$LookFile"
}
# Follow-0n File
$Value = Get-XMLAttributeValue $Config "RunAs"
If(-Not [string]::IsNullOrEmpty($Value)){
$LookFile = Split-Path $Value -leaf
$Config = Set-XMLAttributeValue $Config "RunAs" $LookFile
Copy-Item $Value "$TargetFolder\\$LookFile"
}
# Azure ID File
$Value = Get-XMLAttributeValue $Config "AzureObjectIDFile"
If(-Not [string]::IsNullOrEmpty($Value)){
$LookFile = Split-Path $Value -leaf
$Config = Set-XMLAttributeValue $Config "AzureObjectIDFile" $LookFile
Copy-Item $Value "$TargetFolder\\$LookFile"
}
# Write the updated config file
Set-Content -Path "$TargetFolder\\Profwiz.config" -Value $Config -Force
# Call Profwiz.exe and wait for it to finish
$profwiz = Start-Process -FilePath "$TargetFolder\\Profwiz.exe" -ArgumentList "/REBOOTDELAY 30" -Wait -PassThru
$profwiz.WaitForExit();
# If Profwiz.exe does not return an error, write a flag file to prevent this script being run twice
if ($profwiz.ExitCode -eq 0) {
New-Item -Path $FlagFile -ItemType File| Out-Null
}
else{
Write-Warning "$_ exited with status code $($profwiz.ExitCode)"
}
# Cleanup
Remove-item $TargetFolder -Recurse