Cleaned up Remove-VSC and New-VSC

- Changed Remove-VSC to have a single mandatory parameter (DevicePath)
- Updated New-VSC to check initial state of the VSS Service and return
VSS to its inital state after execution
This commit is contained in:
Jared Atkinson 2015-07-08 22:27:12 -04:00
parent 25934d4719
commit c29f9b4743
1 changed files with 12 additions and 34 deletions

View File

@ -77,6 +77,9 @@ function New-VolumeShadowCopy
Throw 'You must run Get-VolumeShadowCopy from an elevated command prompt.' Throw 'You must run Get-VolumeShadowCopy from an elevated command prompt.'
} }
# Save VSS Service initial state
$running = (Get-Service -Name VSS).Status
$class = [WMICLASS]"root\cimv2:win32_shadowcopy" $class = [WMICLASS]"root\cimv2:win32_shadowcopy"
$return = $class.create("$Volume", "$Context") $return = $class.create("$Volume", "$Context")
@ -98,6 +101,12 @@ function New-VolumeShadowCopy
13 {Write-Error "Unknown error."; break} 13 {Write-Error "Unknown error."; break}
default {break} default {break}
} }
# If VSS Service was Stopped at the start, return VSS to "Stopped" state
if($running -eq "Stopped")
{
Stop-Service -Name VSS
}
} }
function Remove-VolumeShadowCopy function Remove-VolumeShadowCopy
@ -134,14 +143,6 @@ function Remove-VolumeShadowCopy
----------- -----------
Removes all volume shadow copy Removes all volume shadow copy
.EXAMPLE
Get-WmiObject Win32_ShadowCopy | Remove-VolumeShadowCopy
Description
-----------
Removes all volume shadow copy
.EXAMPLE .EXAMPLE
Remove-VolumeShadowCopy -DevicePath '\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy4' Remove-VolumeShadowCopy -DevicePath '\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy4'
@ -150,13 +151,9 @@ function Remove-VolumeShadowCopy
----------- -----------
Removes the volume shadow copy at the 'DeviceObject' path \\?\GLOBALROOT\DeviceHarddiskVolumeShadowCopy4 Removes the volume shadow copy at the 'DeviceObject' path \\?\GLOBALROOT\DeviceHarddiskVolumeShadowCopy4
#> #>
[CmdletBinding(SupportsShouldProcess = $True)]
Param( Param(
[Parameter(Mandatory = $False, ValueFromPipeline = $True)] [Parameter(Mandatory = $True, ValueFromPipeline = $True)]
[ValidateNotNullOrEmpty()]
[Object]
$InputObject,
[Parameter(Mandatory = $False)]
[ValidatePattern('^\\\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy[0-9]{1,3}$')] [ValidatePattern('^\\\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy[0-9]{1,3}$')]
[String] [String]
$DevicePath $DevicePath
@ -164,29 +161,10 @@ function Remove-VolumeShadowCopy
PROCESS PROCESS
{ {
if($PSBoundParameters.ContainsKey("InputObject")) if($PSCmdlet.ShouldProcess("The VolumeShadowCopy at DevicePath $DevicePath will be removed"))
{
if($InputObject.GetType().Name -eq "String")
{
(Get-WmiObject -Namespace root\cimv2 -Class Win32_ShadowCopy | Where-Object {$_.DeviceObject -eq $InputObject}).Delete()
}
else
{
$InputObject.Delete()
}
}
elseif($PSBoundParameters.ContainsKey("DevicePath"))
{ {
(Get-WmiObject -Namespace root\cimv2 -Class Win32_ShadowCopy | Where-Object {$_.DeviceObject -eq $DevicePath}).Delete() (Get-WmiObject -Namespace root\cimv2 -Class Win32_ShadowCopy | Where-Object {$_.DeviceObject -eq $DevicePath}).Delete()
} }
else
{
$vsc = Get-WmiObject -Namespace root\cimv2 -Class Win32_ShadowCopy
foreach($copy in $vsc)
{
$copy.Delete()
}
}
} }
} }