-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMount-VSC.ps1
123 lines (108 loc) · 3.36 KB
/
Mount-VSC.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
Function Mount-VSC {
<#
.SYNOPSIS
Mount a volume shadow copy.
Original Source for this script found at https://p0w3rsh3ll.wordpress.com/2014/06/21/mount-and-dismount-volume-shadow-copies/
Posted by Emin Atac. Modified for rapid use.
.DESCRIPTION
Mount a volume shadow copy. Verbose enabled by default.
.PARAMETER Destination
Target folder that will contain mounted volume shadow copies
.EXAMPLE
Mount-VSC -Destination C:\VSS
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[String]$Destination
)
Begin {
Try {
$null = [mklink.symlink]
} Catch {
Add-Type @"
using System;
using System.Runtime.InteropServices;
namespace mklink
{
public class symlink
{
[DllImport("kernel32.dll")]
public static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int dwFlags);
}
}
"@
}
}
Process {
$oldVerbose = $VerbosePreference
$VerbosePreference = "Continue"
$ShadowPath = Get-CimInstance -ClassName Win32_ShadowCopy
$ShadowPath.DeviceObject | ForEach-Object -Process {
if ($($_).EndsWith("\")) {
$sPath = $_
} else {
$sPath = "$($_)\"
}
$tPath = Join-Path -Path $Destination -ChildPath (
'{0}-{1}' -f (Split-Path -Path $sPath -Leaf),[GUID]::NewGuid().Guid
)
try {
if (
[mklink.symlink]::CreateSymbolicLink($tPath,$sPath,1)
) {
Write-Verbose -Message "Successfully mounted $sPath to $tPath"
} else {
Write-Warning -Message "Failed to mount $sPath"
}
} catch {
Write-Warning -Message "Failed to mount $sPath because $($_.Exception.Message)"
}
}
$VerbosePreference = $oldVerbose
}
End {}
}
Function Unmount-VSC {
<#
.SYNOPSIS
Unmount a volume shadow copy.
.DESCRIPTION
Unmount a volume shadow copy.
.PARAMETER Path
Path of volume shadow copies mount points
.EXAMPLE
Unmount-VSC -Path C:\vss
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[Alias("FullName")]
[string[]]$Path
)
Begin {
}
Process {
$oldVerbose = $VerbosePreference
$VerbosePreference = "Continue"
$Path = (Get-ChildItem $Path).FullName | ForEach-Object -Process {
$sPath = $_
if (Test-Path -Path $sPath -PathType Container) {
if ((Get-Item -Path $sPath).Attributes -band [System.IO.FileAttributes]::ReparsePoint) {
try {
[System.IO.Directory]::Delete($sPath,$false) | Out-Null
Write-Verbose -Message "Successfully dismounted $sPath"
} catch {
Write-Warning -Message "Failed to dismount $sPath because $($_.Exception.Message)"
}
} else {
Write-Warning -Message "The path $sPath isn't a reparsepoint"
}
} else {
Write-Warning -Message "The path $sPath isn't a directory"
}
}
$VerbosePreference = $oldVerbose
}
End {}
}