@@ -77,6 +77,9 @@ function New-VolumeShadowCopy
77
77
Throw ' You must run Get-VolumeShadowCopy from an elevated command prompt.'
78
78
}
79
79
80
+ # Save VSS Service initial state
81
+ $running = (Get-Service - Name VSS).Status
82
+
80
83
$class = [WMICLASS ]" root\cimv2:win32_shadowcopy"
81
84
82
85
$return = $class.create (" $Volume " , " $Context " )
@@ -98,6 +101,12 @@ function New-VolumeShadowCopy
98
101
13 {Write-Error " Unknown error." ; break }
99
102
default {break }
100
103
}
104
+
105
+ # If VSS Service was Stopped at the start, return VSS to "Stopped" state
106
+ if ($running -eq " Stopped" )
107
+ {
108
+ Stop-Service - Name VSS
109
+ }
101
110
}
102
111
103
112
function Remove-VolumeShadowCopy
@@ -134,14 +143,6 @@ function Remove-VolumeShadowCopy
134
143
-----------
135
144
Removes all volume shadow copy
136
145
137
- . EXAMPLE
138
-
139
- Get-WmiObject Win32_ShadowCopy | Remove-VolumeShadowCopy
140
-
141
- Description
142
- -----------
143
- Removes all volume shadow copy
144
-
145
146
. EXAMPLE
146
147
147
148
Remove-VolumeShadowCopy -DevicePath '\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy4'
@@ -150,43 +151,20 @@ function Remove-VolumeShadowCopy
150
151
-----------
151
152
Removes the volume shadow copy at the 'DeviceObject' path \\?\GLOBALROOT\DeviceHarddiskVolumeShadowCopy4
152
153
#>
154
+ [CmdletBinding (SupportsShouldProcess = $True )]
153
155
Param (
154
- [Parameter (Mandatory = $False , ValueFromPipeline = $True )]
155
- [ValidateNotNullOrEmpty ()]
156
- [Object ]
157
- $InputObject ,
158
-
159
- [Parameter (Mandatory = $False )]
156
+ [Parameter (Mandatory = $True , ValueFromPipeline = $True )]
160
157
[ValidatePattern (' ^\\\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy[0-9]{1,3}$' )]
161
158
[String ]
162
159
$DevicePath
163
160
)
164
161
165
162
PROCESS
166
163
{
167
- if ($PSBoundParameters.ContainsKey (" InputObject" ))
168
- {
169
- if ($InputObject.GetType ().Name -eq " String" )
170
- {
171
- (Get-WmiObject - Namespace root\cimv2 - Class Win32_ShadowCopy | Where-Object {$_.DeviceObject -eq $InputObject }).Delete()
172
- }
173
- else
174
- {
175
- $InputObject.Delete ()
176
- }
177
- }
178
- elseif ($PSBoundParameters.ContainsKey (" DevicePath" ))
164
+ if ($PSCmdlet.ShouldProcess (" The VolumeShadowCopy at DevicePath $DevicePath will be removed" ))
179
165
{
180
166
(Get-WmiObject - Namespace root\cimv2 - Class Win32_ShadowCopy | Where-Object {$_.DeviceObject -eq $DevicePath }).Delete()
181
167
}
182
- else
183
- {
184
- $vsc = Get-WmiObject - Namespace root\cimv2 - Class Win32_ShadowCopy
185
- foreach ($copy in $vsc )
186
- {
187
- $copy.Delete ()
188
- }
189
- }
190
168
}
191
169
}
192
170
0 commit comments