@@ -77,6 +77,9 @@ function New-VolumeShadowCopy
7777 Throw ' You must run Get-VolumeShadowCopy from an elevated command prompt.'
7878 }
7979
80+ # Save VSS Service initial state
81+ $running = (Get-Service - Name VSS).Status
82+
8083 $class = [WMICLASS ]" root\cimv2:win32_shadowcopy"
8184
8285 $return = $class.create (" $Volume " , " $Context " )
@@ -98,6 +101,12 @@ function New-VolumeShadowCopy
98101 13 {Write-Error " Unknown error." ; break }
99102 default {break }
100103 }
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+ }
101110}
102111
103112function Remove-VolumeShadowCopy
@@ -134,14 +143,6 @@ function Remove-VolumeShadowCopy
134143 -----------
135144 Removes all volume shadow copy
136145
137- . EXAMPLE
138-
139- Get-WmiObject Win32_ShadowCopy | Remove-VolumeShadowCopy
140-
141- Description
142- -----------
143- Removes all volume shadow copy
144-
145146. EXAMPLE
146147
147148 Remove-VolumeShadowCopy -DevicePath '\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy4'
@@ -150,43 +151,20 @@ function Remove-VolumeShadowCopy
150151 -----------
151152 Removes the volume shadow copy at the 'DeviceObject' path \\?\GLOBALROOT\DeviceHarddiskVolumeShadowCopy4
152153#>
154+ [CmdletBinding (SupportsShouldProcess = $True )]
153155 Param (
154- [Parameter (Mandatory = $False , ValueFromPipeline = $True )]
155- [ValidateNotNullOrEmpty ()]
156- [Object ]
157- $InputObject ,
158-
159- [Parameter (Mandatory = $False )]
156+ [Parameter (Mandatory = $True , ValueFromPipeline = $True )]
160157 [ValidatePattern (' ^\\\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy[0-9]{1,3}$' )]
161158 [String ]
162159 $DevicePath
163160 )
164161
165162 PROCESS
166163 {
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" ))
179165 {
180166 (Get-WmiObject - Namespace root\cimv2 - Class Win32_ShadowCopy | Where-Object {$_.DeviceObject -eq $DevicePath }).Delete()
181167 }
182- else
183- {
184- $vsc = Get-WmiObject - Namespace root\cimv2 - Class Win32_ShadowCopy
185- foreach ($copy in $vsc )
186- {
187- $copy.Delete ()
188- }
189- }
190168 }
191169}
192170
0 commit comments