-
Notifications
You must be signed in to change notification settings - Fork 128
/
CopyProfile.ps1
151 lines (136 loc) · 6.61 KB
/
CopyProfile.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
<#
Author: Mick Pletcher
Date: 04 March 2014
Synopsis: This script will robocopy specific user data from one profile to another on
the same machine, or to a new machine. It will require user input in the following
format:
1) Have the user logout
2) Rename the user profile to <profile>.old
3) Have the user log back in
1) Define Global Memory
2) Get the relative path
3) User input
a) User profile to copy
b) Is the profile to be copied to a new machine
c) If yes, what is the computer name of the new machine
4) Get the OS version of the source machine
5) If copying to a new machine, get the OS version of destination machine
6)
#>
#Define Global Variables
Set-Variable -Name AdminPassword -Scope Global -Force
Set-Variable -Name AdminUsername -Scope Global -Force
Set-Variable -Name DestinationComputer -Scope Global -Force
Set-Variable -Name DestinationProfile -Scope Global -Force
Set-Variable -Name RelativePath -Scope Global -Force
Set-Variable -Name SourceComputer -Scope Global -Force
Set-Variable -Name SourceProfile -Scope Global -Force
Function GetRelativePath {
$Global:RelativePath = (split-path $SCRIPT:MyInvocation.MyCommand.Path -parent)+"\"
}
Function GetUserInput {
#Define Local Memory
Set-Variable -Name Message -Scope Local -Force
Set-Variable -Name No -Scope Local -Force
Set-Variable -Name Options -Scope Local -Force
Set-Variable -Name Result -Scope Local -Force
Set-Variable -Name Title -Scope Local -Force
Set-Variable -Name Username -Scope Local -Force
Set-Variable -Name Yes -Scope Local -Force
$Username = Read-Host "Enter the username"
$Global:SourceComputer = Read-Host "Enter the computer name of the source system"
$Title = ""
$message = "Is the profile to be copied to a different machine?"
$Yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Copy files to profile on different machine"
$No = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Copy files to new profile on same machine"
$Options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$Result = $host.ui.PromptForChoice($title, $message, $options, 0)
If ($Result -eq 1) {
$Global:DestinationComputer = $Global:SourceComputer
$Global:SourceProfile = Read-Host "Enter renamed profile name"
$Global:SourceProfile = "\\"+$Global:SourceComputer+"\c$\users\"+$Global:SourceProfile
$Global:DestinationProfile = "\\"+$Global:DestinationComputer+"\c$\users\"+$Username
} else {
$Global:DestinationComputer = Read-Host "Enter the computer name of the new machine"
#$Global:SourceProfile = $Env:systemdrive+"\users\"+$Username
$Global:SourceProfile = $Username
$Global:DestinationProfile = "\\"+$Global:DestinationComputer+"\c$\users\"+$Username
}
$Global:AdminUsername = Read-Host "Enter administrator username"
$Global:AdminPassword = Read-Host -AsSecureString "Enter administrator account password"
$Global:AdminPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Global:AdminPassword))
$Global:AdminUsername = "[Domain]\"+$Global:AdminUsername
#Cleanup Local Memory
Remove-Variable -Name Message -Scope Local -Force
Remove-Variable -Name No -Scope Local -Force
Remove-Variable -Name Options -Scope Local -Force
Remove-Variable -Name Result -Scope Local -Force
Remove-Variable -Name Title -Scope Local -Force
Remove-Variable -Name Username -Scope Local -Force
Remove-Variable -Name Yes -Scope Local -Force
}
Function RoboCopyFiles {
#Define Local Memory
Set-Variable -Name ErrCode -Scope Local -Force
Set-Variable -Name ExcludeDir -Scope Local -Force
Set-Variable -Name ExcludeFiles -Scope Local -Force
Set-Variable -Name EXE -Scope Local -Force
Set-Variable -Name Logs -Scope Local -Force
Set-Variable -Name Parameters -Scope Local -Force
Set-Variable -Name RemoteExec -Scope Local -Force
Set-Variable -Name Robocopy -Scope Local -Force
Set-Variable -Name Switches -Scope Local -Force
$EXE = "\\BNASANIS01\SupportServices\Tools\PSTools\PsExec.exe"
$RemoteExec = "\\"+$Global:SourceComputer+[char]32+"-accepteula -u $Global:AdminUsername -p $Global:AdminPassword"+[char]32
$Switches = [char]32+"/e /eta /r:1 /w:0"
$ExcludeDir = [char]32+"/xd AppData Application* Downloads LocalService *Games* NetworkService *Links* *temp *TEMPOR~1 *cache Local*"
$ExcludeFiles = [char]32+"/xf ntuser.* *.exd *.nk2 *.srs extend.dat *cache* *.oab index.* {* *.ost UsrClass.* SharePoint*.pst history* *tmp*"
$Logs = [char]32+"/log:"+$Env:windir+"\waller\Logs\ApplicationLogs\ProfileCopy.log"
$Parameters = $Switches+$ExcludeDir+$ExcludeFiles+$Logs
$Arguments = $RemoteExec+$Env:windir+"\system32\robocopy.exe"+[char]32+$Env:systemdrive+"\users\"+$Global:SourceProfile+[char]32+$Global:DestinationProfile+$Parameters
$ErrCode = (Start-Process -FilePath $EXE -ArgumentList $Arguments -Wait -Passthru).ExitCode
#Cleanup Local Memory
Remove-Variable -Name ErrCode -Scope Local -Force
Remove-Variable -Name ExcludeDir -Scope Local -Force
Remove-Variable -Name ExcludeFiles -Scope Local -Force
Remove-Variable -Name EXE -Scope Local -Force
Remove-Variable -Name Logs -Scope Local -Force
Remove-Variable -Name Parameters -Scope Local -Force
Remove-Variable -Name RemoteExec -Scope Local -Force
Remove-Variable -Name Robocopy -Scope Local -Force
Remove-Variable -Name Switches -Scope Local -Force
}
Function CopyFiles ($FileSource,$FileDest,$FileFilter) {
$Dest = $FileDest
$Files = Get-ChildItem $FileSource -Filter $FileFilter
If ($Files.Count -eq $null) {
Write-Host "Copy "$Files.Name"....." -NoNewline
Copy-Item $Files.FullName -Destination $Dest -Force
$Test = $Dest + "\"+$Files.Name
If (Test-Path $Test) {
Write-Host "Success" -ForegroundColor Yellow
} else {
Write-Host "Failed" -ForegroundColor Red
}
} else {
For ($i = 0; $i -lt $Files.Count; $i++) {
$File = $Files[$i].FullName
Write-Host "Copy"$Files[$i].Name"....." -NoNewline
Copy-Item $File -Destination $Dest -Force
$Test = $Dest + "\"+$Files[$i].Name
If (Test-Path $Test) {
Write-Host "Success" -ForegroundColor Yellow
} else {
Write-Host "Failed" -ForegroundColor Red
}
}
}
}
cls
GetRelativePath
GetUserInput
RoboCopyFiles
#Copy Outlook Signatures
$TempSource = "\\"+$SourceComputer+"\c$\users\"+$SourceProfile+"\AppData\Roaming\Microsoft\Signatures"
$TempDestination = "\\"+$DestinationComputer+"\c$\users\"+$DestinationProfile+"\AppData\Roaming\Microsoft\Signatures"
CopyFiles $TempSource $TempDestination "*.*"