-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathCopy-UserFolder.ps1
340 lines (289 loc) · 9.26 KB
/
Copy-UserFolder.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
function Main
{
[CmdletBinding()]
param
(
[parameter(Mandatory)]
[string]
$SourceUserName,
[parameter(Mandatory)]
[string]
$TargetUserName
)
Show-Caution
$SourceUserName, $TargetUserName `
| %{
Write-Host ("Starting backup SourceUserName '{0}'" -f $_) -ForegroundColor Cyan
Backup-UserDirectory -userName $_
}
Write-Host ("Starting Copy SourceUser '{0}' to TargetUser '{1}'" -f $SourceUserNamem, $TargetUserName) -ForegroundColor Cyan
Copy-TargetDirectory -SourceUserName $SourceUserName -DestinationUserName $TargetUserName
}
function Get-UserFolders
{
[CmdletBinding()]
param
(
[parameter(
mandatory = 0,
position = 0)]
[ValidateNotNullOrEmpty()]
[string]
$usersPath = (Split-Path $env:USERPROFILE -Parent)
)
Get-ChildItem -Path $usersPath
}
function Get-BackupDirectory
{
[CmdletBinding()]
param
(
[parameter(
mandatory = 0,
position = 0)]
[ValidateNotNullOrEmpty()]
[string]
$userPath = $env:USERPROFILE,
[parameter(
mandatory = 0,
position = 0)]
[ValidateNotNullOrEmpty()]
[string]
$backupName = ('backup_{0}' -f (Get-Date).ToString('yyyyMMdd_HHmmss'))
)
$backupDir = Join-Path $userPath 'Backup'
return Join-Path $backupDir $backupName
}
function Test-BackupDirectory
{
[CmdletBinding()]
param
(
[parameter(
mandatory = 0,
position = 0)]
[ValidateNotNullOrEmpty()]
[string]
$backupPath = (Get-BackupDirectory)
)
Test-Path $backupPath
}
function New-BackupDirectory
{
[CmdletBinding()]
param
(
[parameter(
mandatory = 0,
position = 0)]
[ValidateNotNullOrEmpty()]
[string]
$backupPath = (Get-BackupDirectory)
)
New-Item -Path $backupPath -ItemType Directory -Force
}
function Backup-UserDirectory
{
[CmdletBinding()]
param
(
[parameter(
mandatory = 0,
position = 0)]
[ValidateNotNullOrEmpty()]
[validateScript({$_ -in (Get-UserFolders).Name})]
[string]
$userName
)
begin
{
$userPath = (Get-UserFolders | where Name -eq $userName).FullName
$backupPath = Get-BackupDirectory -userPath $userPath
if (-not (Test-BackupDirectory -backupPath $backupPath))
{
New-BackupDirectory -backupPath $backupPath
}
}
process
{
Get-ChildItem -Path $userPath `
| where Name -notin "backup", "SharePoint","SkyDrive","DropBox", "SkyDrive Pro" `
| %{Copy-Item -Path $_.FullName -Destination $backupPath -Recurse -ErrorAction SilentlyContinue}
}
}
function Copy-TargetDirectory
{
[CmdletBinding()]
param
(
[parameter(
mandatory = 1,
position = 0)]
[ValidateNotNullOrEmpty()]
[validateScript({$_ -in (Get-UserFolders).Name})]
[string]
$SourceUserName,
[parameter(
mandatory = 1,
position = 1)]
[ValidateNotNullOrEmpty()]
[validateScript({$_ -in (Get-UserFolders).Name})]
[string]
$DestinationUserName
)
begin
{
$SourceUserPath = (Get-UserFolders | where Name -eq $SourceUserName).FullName
$DestinationUserPath = (Get-UserFolders | where Name -eq $DestinationUserName).FullName
if ($null -eq $SourceUserPath)
{
Throw "'{0}' not found exception!" -f $SourceUserPath
}
if ($null -eq $DestinationUserName)
{
Throw "'{0}' not found exception!" -f $DestinationUserName
}
}
process
{
Get-ChildItem -Path $SourceUserPath `
| where Name -notin "backup", "SharePoint","SkyDrive","DropBox", "SkyDrive Pro" `
| %{
$item = $_
switch ($item.PSIsContainer)
{
$true {
Write-Verbose "Target is 'Directory'"
$param = @{
Path = $item.FullName
Destination = $item.FullName.Replace($item.BaseName,"").Replace($SourceUserPath,$DestinationUserPath)}
Copy-Item @param -Force -ErrorAction SilentlyContinue -Recurse
}
$false {
Write-Verbose "Target is 'File'"
$param = @{
Path = $item.FullName
Destination = $item.FullName.Replace($SourceUserPath,$DestinationUserPath)}
Copy-Item @param -Force -ErrorAction SilentlyContinue -Recurse
}
}
}
}
}
function Show-ValentiaPromptForChoice
{
[CmdletBinding()]
param
(
# input prompt items with array. second index is for help message.
[parameter(
mandatory = 0,
position = 0)]
[string[]]
$questions = $valentia.promptForChoice.questions,
# input title message showing when prompt.
[parameter(
mandatory = 0,
position = 1)]
[string[]]
$title = $valentia.promptForChoice.title,
# input message showing when prompt.
[parameter(
mandatory = 0,
position = 2)]
[string]
$message = $valentia.promptForChoice.message,
# input additional message showing under message.
[parameter(
mandatory = 0,
position = 3)]
[string]
$additionalMessage = $valentia.promptForChoice.additionalMessage,
# input Index default selected when prompt.
[parameter(
mandatory = 0,
position = 4)]
[int]
$defaultIndex = $valentia.promptForChoice.defaultIndex
)
$ErrorActionPreference = $valentia.errorPreference
try
{
# create caption Messages
if(-not [string]::IsNullOrEmpty($additionalMessage))
{
$message += ([System.Environment]::NewLine + $additionalMessage)
}
# create dictionary include dictionary <int, KV<string, string>> : accessing KV <string, string> with int key return from prompt
$script:dictionary = New-Object 'System.Collections.Generic.Dictionary[int, System.Collections.Generic.KeyValuePair[string, string]]'
foreach ($question in $questions)
{
if ($private:count -eq 1)
{
# create key to access value
$private:key = "n"
}
else
{
# create key to access value
$private:key = "y"
}
# create KeyValuePair<string, string> for prompt item : accessing value with 1 letter Alphabet by converting char
$script:keyValuePair = New-Object 'System.Collections.Generic.KeyValuePair[string, string]'($key, $question)
# add to Dictionary
$dictionary.Add($count, $keyValuePair)
# increment to next char
$count++
# prompt limit to max 26 items as using single Alphabet charactors.
if ($count -gt 26)
{
throw ("Not allowed to pass more then '{0}' items for prompt" -f ($dictionary.Keys).count)
}
}
# create choices Collection
$script:collectionType = [System.Management.Automation.Host.ChoiceDescription]
$script:choices = New-Object "System.Collections.ObjectModel.Collection[$CollectionType]"
# create choice description from dictionary<int, KV<string, string>>
foreach ($dict in $dictionary.GetEnumerator())
{
foreach ($kv in $dict)
{
# create prompt choice item. Currently you could not use help message.
$private:choice = (("{0} (&{1}){2}-" -f $kv.Value.Value, "$($kv.Value.Key)".ToUpper(), [Environment]::NewLine), ($valentia.promptForChoice.helpMessage -f $kv.Value.Key, $kv.Value.Value))
# add to choices
$choices.Add((New-Object $CollectionType $choice))
}
}
# show choices on host
$script:answer = $host.UI.PromptForChoice($title, $message, $choices, $defaultIndex)
# return value from key
return ($dictionary.GetEnumerator() | where {$_.Key -eq $answer} | %{$_.Value.Value})
}
catch
{
throw $_
}
}
function Show-Caution
{
$answer = Show-ValentiaPromptForChoice -questions ("はい","いいえ") -title "ユーザーフォルダをコピーします。影響しそうなプログラムは閉じておきましょう。。" -message "未保存のファイルはすべて保存しましたか?実行しますよ。"
if ($answer -eq "はい")
{
return
}
else
{
Write-Host "処理は実施前にキャンセルされました。"
if ($PSVersionTable.PSVersion -gt "2.0")
{
pause
}
else
{
Read-Key "Press any Key to End console."
}
exit
}
}
#Sample Execution
#Main -SourceUserName hogehoge.ActiveDirectory -TargetUserName fugafuga