PowerShell Scripting Guide: Enabling Volume Shadow Copy with Scheduled Backups on Specific Drives


3 views


Volume Shadow Copy Service is a Windows feature that creates point-in-time snapshots of volumes. These snapshots persist even when files are in use, making them ideal for backup operations. PowerShell provides native cmdlets to manage VSS through the Windows.ServerBackup module.

First, verify if VSS is already enabled on your target drive:

Get-WBPolicy | Select-Object -ExpandProperty VolumeShadowCopyComponents

To enable VSS on drive D: with default settings:

Enable-WBVolumeShadowCopy -Volume (Get-WBVolume -VolumePath "D:") Set-WBVolumeShadowCopy -Volume (Get-WBVolume -VolumePath "D:") -Enable $true

For production environments, you'll want scheduled snapshots. This example creates daily snapshots at 2 AM:

$policy = New-WBPolicy $schedule = New-WBSchedule -Time 02:00 -Daily Add-WBSchedule -Policy $policy -Schedule $schedule $vol = Get-WBVolume -VolumePath "D:" Add-WBVolume -Policy $policy -Volume $vol Set-WBPolicy -Policy $policy -Force

For handling multiple drives (D: and E: in this example):

$policy = New-WBPolicy $volumes = @("D:", "E:") | ForEach-Object { Get-WBVolume -VolumePath $_ } $volumes | Add-WBVolume -Policy $policy Enable-WBVolumeShadowCopy -Volume $volumes[0] Enable-WBVolumeShadowCopy -Volume $volumes[1] Set-WBPolicy -Policy $policy

Check your VSS configuration status:

vssadmin list shadows Get-WBSummary | Format-List

Here's a robust implementation with error handling:

try { $policy = New-WBPolicy -ErrorAction Stop $vol = Get-WBVolume -VolumePath "D:" -ErrorAction Stop Add-WBVolume -Policy $policy -Volume $vol -ErrorAction Stop $schedule = New-WBSchedule -Time 02:00 -Daily -ErrorAction Stop Add-WBSchedule -Policy $policy -Schedule $schedule -ErrorAction Stop Set-WBPolicy -Policy $policy -Force -ErrorAction Stop Write-Host "VSS configuration completed successfully" -ForegroundColor Green } catch { Write-Host "Error configuring VSS: $_" -ForegroundColor Red exit 1 }

Volume Shadow Copy Service (VSS) is a Windows feature that creates point-in-time copies (snapshots) of volumes. These snapshots enable backup operations without requiring downtime. PowerShell provides comprehensive cmdlets to manage VSS programmatically.

Before enabling shadow copies, verify the current status:

Get-WmiObject -Class Win32_ShadowCopy | ForEach-Object {
    $volume = Get-WmiObject -Class Win32_Volume | Where-Object { $_.DeviceID -eq $_.VolumeName }
    [PSCustomObject]@{
        Drive = $volume.DriveLetter
        ShadowCopies = $_.Count
        MaxSize = (Get-WmiObject -Class Win32_ShadowStorage).MaxSpace/1MB
    }
}

To enable VSS on drive D: with default settings:

$drive = 'D:'
vssadmin add shadowstorage /for=$drive /on=$drive /maxsize=10%

The /maxsize parameter specifies the maximum storage space (10% of volume size in this example).

For automated snapshots, we'll use the Task Scheduler cmdlets:

$action = New-ScheduledTaskAction -Execute 'powershell.exe' 
    -Argument "-Command "vssadmin create shadow /for=$drive""
$trigger = New-ScheduledTaskTrigger -Daily -At '02:00'
Register-ScheduledTask -TaskName "Daily VSS $drive" 
    -Action $action -Trigger $trigger -RunLevel Highest

For enterprise environments, consider these enhancements:

# Set custom maximum storage size (5GB example)
vssadmin resize shadowstorage /for=$drive /on=$drive /maxsize=5GB

# Create differential backup schedule
$scriptBlock = {
    $drive = 'D:'
    vssadmin create shadow /for=$drive /autoretry=15 /quiet
    # Additional post-snapshot commands can go here
}
$action = New-ScheduledTaskAction -Execute 'powershell.exe' 
    -Argument "-Command $scriptBlock"
Register-ScheduledTask -TaskName "Custom VSS Task" 
    -Action $action -Trigger $trigger

Regularly check shadow copy health:

vssadmin list shadowstorage
vssadmin list shadows
Get-WinEvent -LogName 'Application' | 
    Where-Object { $_.ProviderName -match 'VSS' } | 
    Select-Object TimeCreated, Message -First 10

If snapshots fail, check these potential solutions:

# Reset VSS writers
net stop vss
net start vss
vssadmin list writers

# Increase storage allocation if snapshots fail
vssadmin resize shadowstorage /for=$drive /on=$drive /maxsize=15%