-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFSR-FileRestore.psm1
More file actions
123 lines (116 loc) · 5.4 KB
/
DFSR-FileRestore.psm1
File metadata and controls
123 lines (116 loc) · 5.4 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
function Get-ConflictFiles {
Param(
[Parameter(Mandatory=$true)][string]$Share,
[Parameter(Mandatory=$true)][string]$Filename,
[Parameter(Mandatory=$true)][string]$Namespace,
[string]$Domain = (Get-WmiObject Win32_ComputerSystem).Domain
)
$Manifest = "\\$Domain\$Namespace\$Share\DfsrPrivate\ConflictAndDeletedManifest.xml"
$Files = Get-DfsrPreservedFiles -Path $Manifest | Where-Object Path -like "*$Filename*"
Return $Files
}
function Restore-ConflictFiles {
Param(
[Parameter(Mandatory=$true)][string]$Share,
[Parameter(Mandatory=$true)][Object[]]$Files,
[Parameter(Mandatory=$true)][string]$Namespace,
[string]$Domain = (Get-WmiObject Win32_ComputerSystem).Domain
)
$Manifest = "\\$Domain\$Namespace\$Share\DfsrPrivate\ConflictAndDeletedManifest.xml"
foreach ($File in $Files) {
$OriginalFile = $File.Path -replace "^.*\\$Namespace\\", "\\$Domain\$Namespace\"
$ConflictFile = $File.PreservedName
$error.clear()
try {
Copy-Item "\\$Domain\$Namespace\$Share\DfsrPrivate\ConflictAndDeleted\$ConflictFile" $OriginalFile -Force
}
catch {}
if (!$error) {
Write-Host "**RESTORED**" $OriginalFile -ForegroundColor Green
(Get-Content $Manifest) -notmatch "^.*$ConflictFile.*$" | Out-File $Manifest -Force -Encoding ascii
}
}
}
function Restore-ConflictFile {
Param(
[Parameter(Mandatory=$true)][string]$Share,
[Parameter(Mandatory=$true)][Object]$File,
[Parameter(Mandatory=$true)][string]$Namespace,
[string]$Domain = (Get-WmiObject Win32_ComputerSystem).Domain
)
$Manifest = "\\$Domain\$Namespace\$Share\DfsrPrivate\ConflictAndDeletedManifest.xml"
$OriginalFile = $File.Path -replace "^.*\\$Namespace\\", "\\$Domain\$Namespace\"
$ConflictFile = $File.PreservedName
$error.clear()
try {
Copy-Item "\\$Domain\$Namespace\$Share\DfsrPrivate\ConflictAndDeleted\$ConflictFile" $OriginalFile -Force
}
catch {}
if (!$error) {
Write-Host "**RESTORED**" $OriginalFile -ForegroundColor Green
(Get-Content $Manifest) -notmatch "^.*$ConflictFile.*$" | Out-File $Manifest -Force -Encoding ascii
}
}
function Find-MissingFiles {
Param(
[Parameter(Mandatory=$true)][string]$Share,
[Parameter(Mandatory=$true)][string]$Filename,
[string]$Namespace = 'Shares',
[string]$Domain = (Get-WmiObject Win32_ComputerSystem).Domain
)
$Files = Get-ConflictFiles -Namespace $Namespace -Share $Share -Filename $Filename
if ( $Files ) {
$NewSearch = $true
while ( $NewSearch ) {
$NewSearch = $false
$FileNumber = 0
foreach ( $File in $Files ) {
$FileNumber++
$OriginalFile = $File.Path -replace "^.*\\$Namespace\\", "\\$Domain\$Namespace\"
Write-Host "("$FileNumber" )" $OriginalFile -ForegroundColor Green
Write-Host " Moved on" $File.PreservedTime " Reason : " $File.PreservedReason -ForegroundColor Yellow
}
Write-Host "Found" $Files.count "files!" -ForegroundColor Yellow
$title = ""
$message = "Restore all files, some files, modify the query, or exit?"
$a = New-Object System.Management.Automation.Host.ChoiceDescription "Restore &all", `
"Restore all files (newest files overwrite old)."
$s = New-Object System.Management.Automation.Host.ChoiceDescription "&Select restore", `
"Restore select files from the list using the indicator numbers."
$f = New-Object System.Management.Automation.Host.ChoiceDescription "Refine &filename", `
"Modify filename query."
$e = New-Object System.Management.Automation.Host.ChoiceDescription "&Exit", `
"Exit"
$options = [System.Management.Automation.Host.ChoiceDescription[]]( $a, $s, $f, $e )
$result = $host.ui.PromptForChoice( $title, $message, $options, 3 )
switch ( $result ) {
0 {
Restore-ConflictFiles -Namespace $Namespace -Share $Share -Files $Files
$Files = Get-ConflictFiles -Namespace $Namespace -Share $Share -Filename $Filename
}
1 {
$FilesToRestore = Read-Host -Prompt 'Enter the file numbers to restore, separate with comma (2,5,8,4)'
$FilesToRestore = $FilesToRestore.Split(',')
$FileNumber = 0
foreach ( $File in $Files ) {
$FileNumber++
foreach ( $Number in $FilesToRestore ) {
if ( $Number -as [int] -eq $FileNumber ) {
Restore-ConflictFile -Namespace $Namespace -Share $Share -File $File
}
}
}
}
2 {
$Filename = Read-Host -Prompt 'Enter the new filename to search'
$Files = Get-ConflictFiles -Namespace $Namespace -Share $Share -Filename $Filename
$NewSearch = $true
}
3 { Return }
}
}
Write-Host "Done!" -ForegroundColor Green
} else {
Write-Host "No Files Found" -ForegroundColor Green
}
}