-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathRobocopyProfile.ps1
More file actions
141 lines (123 loc) · 5.29 KB
/
RobocopyProfile.ps1
File metadata and controls
141 lines (123 loc) · 5.29 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
<#
.SYNOPSIS
Robocopy profile to designated UNC path
.DESCRIPTION
This script will robocopy a user profile from a remote machine to a specified UNC path. It is intended to be used when a profile is either corrupt or a new operating system is being deployed that is a different architecture.
.PARAMETER DirectoryExclusionsFile
File containing a list of directories to exclude
.PARAMETER FileExclusionsFile
File containing a list of files to exclude
.PARAMETER RobocopySwitches
Switches for controlling the robocopy process
.PARAMETER DestinationUNC
UNC path where the profile backup will be written to
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.145
Created on: 9/6/2018 9:05 AM
Created by: Mick Pletcher
Filename: RobocopyProfile.ps1
===========================================================================
#>
[CmdletBinding()]
param
(
[string]$DirectoryExclusionsFile = 'DirectoryExclusions.txt',
[string]$FileExclusionsFile = 'FileExclusions.txt',
[string]$RobocopySwitches = '/e /eta /r:1 /w:0 /TEE /MIR',
[ValidateNotNullOrEmpty()][string]$DestinationUNC = '\\profiles\userprofiles'
)
function Get-RelativePath {
<#
.SYNOPSIS
Get the relative path
.DESCRIPTION
Returns the location of the currently running PowerShell script
.NOTES
Additional information about the function.
#>
[CmdletBinding()][OutputType([string])]
param ()
$Path = (split-path $SCRIPT:MyInvocation.MyCommand.Path -parent) + "\"
Return $Path
}
function Invoke-RoboCopy {
<#
.SYNOPSIS
Backup User profile
.DESCRIPTION
This function will initiate robocopy to backup the defined contents of the user profile to the designated UNC path.
.PARAMETER ComputerName
A description of the ComputerName parameter.
.PARAMETER UserName
A description of the UserName parameter.
.NOTES
Additional information about the function.
#>
[CmdletBinding()][OutputType([string])]
param
(
[ValidateNotNullOrEmpty()][string]$ComputerName,
[ValidateNotNullOrEmpty()][string]$UserName
)
#Get Relative Path this script is being executed from
$RelativePath = Get-RelativePath
$Executable = $Env:windir + "\system32\robocopy.exe"
#PSEXEC remote execution parameters
$DirectoryExclusions = Get-Content ($RelativePath + $DirectoryExclusionsFile)
IF ($DirectoryExclusions -ne $null) {
$ExcludeDir = "/xd"
#Add each exclusion from the file to the /xd command line
foreach ($Exclusion in $DirectoryExclusions) {
$ExcludeDir += [char]32 + $Exclusion
}
}
#Read the contents in of all file exclusions
$FileExclusions = Get-Content ($RelativePath + $FileExclusionsFile)
IF ($FileExclusions -ne $null) {
$ExcludeFiles = "/xf"
#Add each exclusion from the file to the /xd command line
foreach ($Exclusion in $FileExclusions) {
$ExcludeFiles += [char]32 + $Exclusion
}
}
#Include all subdirectories, give an estimated time for each file, retry once, and do not wait if file transfer fails
If ($DestinationUNC.Substring($DestinationUNC.Length - 1) -ne '\') {
$Arguments = [char]34 + '\\' + $ComputerName + '\c$\users\' + $UserName + [char]34 + [char]32 + [char]34 + $DestinationUNC + '\' + $ComputerName + [char]34 + [char]32 + $RobocopySwitches + [char]32 + $ExcludeDir + [char]32 + $ExcludeFiles + [char]32 + '/LOG:' + [char]34 + $DestinationUNC + '\0RobocopyLogs\' + $ComputerName + '.log' + [char]34
} else {
$Arguments = [char]34 + '\\' + $ComputerName + '\c$\users\' + $UserName + [char]34 + [char]32 + [char]34 + $DestinationUNC + $ComputerName + [char]34 + [char]32 + $RobocopySwitches + [char]32 + $ExcludeDir + [char]32 + $ExcludeFiles + [char]32 + '/LOG:' + [char]34 + $DestinationUNC + '0RobocopyLogs\' + $ComputerName + '.log' + [char]34
}
$ErrCode = (Start-Process -FilePath $Executable -ArgumentList $Arguments -WindowStyle Minimized -Wait -Passthru).ExitCode
Return $ErrCode
}
$ComputerName = Read-Host -Prompt 'Input computer name'
$UserName = Read-Host -Prompt 'Input user name'
#Check if Computername and username are correct
If ((Get-Item -Path ('\\' + $ComputerName + '\c$') -ErrorAction SilentlyContinue) -eq $null) {
Write-Host "Computer not reachable or incorrect computer name"
Exit 1
}
If ((Get-Item -Path ('\\' + $Computername + '\c$\users\' + $Username) -ErrorAction SilentlyContinue) -eq $null) {
Write-Host "Username is incorrect"
Exit 1
}
$ErrCode = Invoke-RoboCopy -ComputerName $ComputerName -UserName $UserName
switch ($ErrCode) {
0 { Write-Host '0 - No Changes' }
1 { Write-Host '1 - OK Copy' }
2 { Write-Host '2 - Extra content deleted' }
3 { Write-Host '3 - OK Copy & Extra content deleted' }
4 { Write-Host '4 - Mismatches' }
5 { Write-Host '5 - OK Copy & mismatches'}
6 { Write-Host '6 - Mismatches & Extra content deleted'}
7 { Write-Host '7 - OK Copy & Mismatches & Extra content deleted'}
8 { Write-Host '8 - Failed'}
9 { Write-Host '9 - OK Copy & Failed'}
10 { Write-Host '10 - Failed & Extra content deleted'}
11 { Write-Host '11 - OK Copy & Failed & Extra content deleted'}
12 { Write-Host '12 - Failed & Mismatches'}
13 { Write-Host '13 - OK Copy & Failed & Mismatches'}
14 { Write-Host '14 - Failed & Mismatches & Extra content deleted'}
15 { Write-Host '15 - OK Copy & Failed & Mismatches & Extra content deleted'}
16 { Write-Host '16 - Fatal Error'}
}