14

How to enable volume shadow copy on specific drive (D:, E:,...) and set up schedule using Powershell ?

I just need some hints how to start.

Primoz
  • 397
  • 3
  • 7
  • 16

10 Answers10

7

You can create a Shadow copy on a specific drive using WMI object like it's explained in Microsoft Article.

Enable volume shadow copy on specific drive (D:, E:,...) is two things

  1. Creating a shadow copy as shown in the preceding article
  2. Creating a scheduled task to create shadow copy
JPBlanc
  • 215
  • 2
  • 7
  • Nice. This answer is exactly what I needed. I didn't know that I must create sceduled task. – Primoz Apr 13 '11 at 21:19
7

It took me a little while to get this to worked (after finding the simple error causing it to fail), and the function is incomplete. I want to add the ability to enable it on a remote computer, which will probably have to be done via a task that is ran once. If anybody modifies this and adds that, let me know!

function Enable-ShadowCopies {
param(
    [String]$ComputerName = $Env:ComputerName,

    [Parameter(Mandatory=$true)]
    [String]$Drive
)
$volumeWMI = Get-WmiObject -ComputerName $ComputerName -Class Win32_Volume -Filter "DriveLetter = '$Drive'";
$volumeID = ($volumeWMI.DeviceID.SubString(10)).SubString(0,($volumeWMI.DeviceID.SubString(10)).Length-1);

$scheduler = New-Object -ComObject Schedule.Service
$scheduler.Connect($ComputerName)
$tskDef = $scheduler.NewTask(0);
$tskRegInfo = $tskDef.RegistrationInfo;
$tskSettings = $tskDef.Settings;
$tskTriggers = $tskDef.Triggers;
$tskActions = $tskDef.Actions;
$tskPrincipals = $tskDef.Principal;

# Registration Info
$tskRegInfo.Author = "PowerShell Script";

# Settings
$tskSettings.DisallowStartIfOnBatteries = $false;
$tskSettings.StopIfGoingOnBatteries = $false
$tskSettings.AllowHardTerminate = $false;
$tskSettings.IdleSettings.IdleDuration = "PT600S";
$tskSettings.IdleSettings.WaitTimeout = "PT3600S";
$tskSettings.IdleSettings.StopOnIdleEnd = $false;
$tskSettings.IdleSettings.RestartOnIdle = $false;
$tskSettings.Enabled = $true;
$tskSettings.Hidden = $false;
$tskSettings.RunOnlyIfIdle = $false;
$tskSettings.WakeToRun = $false;
$tskSettings.ExecutionTimeLimit = "PT259200S";
$tskSettings.Priority = "5";
$tskSettings.StartWhenAvailable = $false;
$tskSettings.RunOnlyIfNetworkAvailable = $false;

# Triggers
$tskTrigger1 = $tskTriggers.Create(3);
$tskTrigger2 = $tskTriggers.Create(3);

## Trigger 1
$tskTrigger1.Id = "Trigger1"
$tskTrigger1.StartBoundary = (Get-Date -format "yyyy-MM-dd")+"T07:00:00";
$tskTrigger1.DaysOfWeek = 0x3E; # Monday - Friday - http://msdn.microsoft.com/en-us/library/windows/desktop/aa384024(v=vs.85).aspx
$tskTrigger1.Enabled = $true;

## Trigger 2
$tskTrigger2.Id = "Trigger2";
$tskTrigger2.StartBoundary = (Get-Date -format "yyyy-MM-dd")+"T12:00:00";
$tskTrigger2.DaysOfWeek = 0x3E; # Monday - Friday - http://msdn.microsoft.com/en-us/library/windows/desktop/aa384024(v=vs.85).aspx
$tskTrigger2.Enabled = $true;

# Principals (RunAs User)
$tskPrincipals.Id = "Author";
$tskPrincipals.UserID = "SYSTEM";
$tskPrincipals.RunLevel = 1;

 # Actions
$tskActions.Context = "Author"
$tskAction1 = $tskActions.Create(0);

# Action 1
$tskAction1.Path = "C:\Windows\system32\vssadmin.exe";
$tskAction1.Arguments = "Create Shadow /AutoRetry=15 /For="+$volumeWMI.DeviceID;
$tskAction1.WorkingDirectory = "%systemroot%\system32";

# Configure VSS, Add scheduled task
vssadmin Add ShadowStorage /For=$Drive /On=$Drive /MaxSize=10%;
$tskFolder = $scheduler.GetFolder("\")
$tskFolder.RegisterTaskDefinition("ShadowCopyVolume$volumeID", $tskDef, 6, "SYSTEM", $null,5);
}

Even though this works, and it mimics what happens when you do it via the GUI, it still shows as disabled for that drive. But if you enable it, nothing changes! (LOL) I'm guessing there's something that needs to also be modified in the registry.

djet
  • 3
  • 2
Verron Knowles
  • 185
  • 1
  • 2
2
$diskname = "C:\"
$VolumeWmi = gwmi Win32_Volume -Namespace root/cimv2 | ?{ $_.Name -eq $diskname }
$DeviceID = $VolumeWmi.DeviceID.ToUpper().Replace("\\?\VOLUME", "").Replace("\","")
$TaskName = "ShadowCopyVolume" + $DeviceID
$TaskFor = "\\?\Volume" + $DeviceID + "\"
$Task = "C:\Windows\system32\vssadmin.exe"
$Argument = "Create Shadow /AutoRetry=15 /For=$TaskFor"
$WorkingDir = "%systemroot%\system32"

$ScheduledAction = New-ScheduledTaskAction –Execute $Task -WorkingDirectory $WorkingDir -Argument $Argument
$ScheduledTrigger = @()
$ScheduledTrigger += New-ScheduledTaskTrigger -Daily -At 10:00
$ScheduledTrigger += New-ScheduledTaskTrigger -Daily -At 15:00
$ScheduledSettings = New-ScheduledTaskSettingsSet -Compatibility V1 -DontStopOnIdleEnd -ExecutionTimeLimit (New-TimeSpan -Days 3) -Priority 5
$ScheduledTask = New-ScheduledTask -Action $ScheduledAction -Trigger $ScheduledTrigger -Settings $ScheduledSettings
Register-ScheduledTask $TaskName -InputObject $ScheduledTask -User "NT AUTHORITY\SYSTEM"

After a whole bunch of messing with it, got it to work slightly differently (it also seems to show up properly via the GUI).

Shoutouts to this page for a bit of help: https://social.technet.microsoft.com/forums/windowsserver/en-US/fb69840d-5f52-4711-8168-2faa23088233/shadow-copy-schedule-per-script

The downside to using schtasks (what that page uses) is that you can't have multiple triggers as far as I can see.

Also because of the way I troubleshot the solution (Used a bindiff of working/not working xml), I'm not entirely convinced that the flags I use are optimal.

user261949
  • 21
  • 2
2

Simpler means using schtasks that shows in the UI, compatible in PowerShell 2. Designed for a standard build, may need to play about with $volumeinfo[x] when creating $taskrun to find the appropriate volume.

$volumeinfo = GWMI -namespace root\cimv2 -class win32_volume
$volumeid = $volumeinfo[1].deviceid
$taskname = "ShadowCopyVolume" + $volumeid.replace("\","").replace("?Volume","")
$taskrun = "C:\Windows\system32\vssadmin.exe Create Shadow /AutoRetry=15 /For=$volumeid"
schtasks /create /RU SYSTEM /SC DAILY /ST 07:00 /RI 60 /DU 12:00 /K /V1 /TN $TaskName /TR "$taskrun "

Can configure the following arguments appropriately:

  • /SC - frequency task is triggered
  • /ST - time task is triggered
  • /RI - repetition of task every x minutes after trigger
  • /DU - duration of task to be repeated

NOTE: The /TR switch REQUIRES the space at the end, if it is not there it replaces the final backslash with a double quote causing the VSS UI to not recognise the task.

Deadly-Bagel
  • 161
  • 4
2

Shadow Copy is actually enabled by creating tasks that call vssadmin.exe.

PowerShell 3.0 has cmdlets that enable you to create tasks, but these depend upon syscalls that weren't implemented until Windows 8 / Windows Server 2012. You need the following three of these cmdlets to do this the PowerShell way:

register-scheduledTask
new-scheduledtaskaction
new-scheduledtasktrigger

In earlier versions of Windows, you will be restricted to using schtasks.exe.

However -- and this is where this answer differs from the others -- creating a Shadow Copy task in these ways is not the right way to do things, as any changes you make will not show up in the GUI. As such, an unobservant user may be unaware that Shadow Copy has been enabled, thus resulting in them enabling a second instance of it, or worse silently overwriting your task (if you followed Windows's naming conventions (VolumeShadowCopy{$GUID})).

What you need to do, is create one via the GUI (preferably on Win7/Win2008, as newer versions of Windows use taskxml 1.2 by default instead of the more compatible taskxml 1.1), export the task as XML via schtasks, programmatically edit that XML, save that XML as UTF16, and import it. Thankfully, once you get past the GUI at the beginning, this can all be done on the commandline.

I would like to post code, but what I wrote is proprietary, and I do not have permission from my employer. I hope that the XML tip will, at the very least, save you countless hours. I just deployed the XML solution across hundreds of managed servers, to great success.

1

This is what I use in PowerShell. Its a link to my site, but it looks a lot better than it does pasted here.

#Enable Volume Shadow copy
clear
$Continue = Read-Host "Enable Volume Shadowcopy (Y/N)?"
while("Y","N" -notcontains $Continue) {
    $Continue = Read-Host "Enable Volume Shadowcopy (Y/N)?"
}
if ($Continue -eq "Y") {

    #Enable Shadows
    vssadmin add shadowstorage /for=C: /on=C:  /maxsize=8128MB
    vssadmin add shadowstorage /for=D: /on=D:  /maxsize=8128MB

    #Create Shadows
    vssadmin create shadow /for=C:
    vssadmin create shadow /for=D:

    #Set Shadow Copy Scheduled Task for C: AM
    $Action=new-scheduledtaskaction -execute "c:\windows\system32\vssadmin.exe" -Argument "create shadow /for=C:"
    $Trigger=new-scheduledtasktrigger -daily -at 6:00AM
    Register-ScheduledTask -TaskName ShadowCopyC_AM -Trigger $Trigger -Action $Action -Description "ShadowCopyC_AM"

    #Set Shadow Copy Scheduled Task for C: PM
    $Action=new-scheduledtaskaction -execute "c:\windows\system32\vssadmin.exe" -Argument "create shadow /for=C:"
    $Trigger=new-scheduledtasktrigger -daily -at 6:00PM
    Register-ScheduledTask -TaskName ShadowCopyC_PM -Trigger $Trigger -Action $Action -Description "ShadowCopyC_PM"

    #Set Shadow Copy Scheduled Task for D: AM
    $Action=new-scheduledtaskaction -execute "c:\windows\system32\vssadmin.exe" -Argument "create shadow /for=D:"
    $Trigger=new-scheduledtasktrigger -daily -at 7:00AM
    Register-ScheduledTask -TaskName ShadowCopyD_AM -Trigger $Trigger -Action $Action -Description "ShadowCopyD_AM"

    #Set Shadow Copy Scheduled Task for D: PM
    $Action=new-scheduledtaskaction -execute "c:\windows\system32\vssadmin.exe" -Argument "create shadow /for=D:"
    $Trigger=new-scheduledtasktrigger -daily -at 7:00PM
    Register-ScheduledTask -TaskName ShadowCopyD_PM -Trigger $Trigger -Action $Action -Description "ShadowCopyD_PM"
}
Dan Rhodes
  • 21
  • 1
1

Thanks to everyone, that was contributing to this thread. It was helpful and saved me a lot of work. The following PowerShell Script was tested on Windows Server 2008 R2.

The following example creates a Snapshot every 2h between Mo.-Fr. from 08:00h-18:00h on Volume C:

If you want to modify the schedule just edit or replace the inline XML-Data.

Attention: No warranty. No responsibiltiy. Please test in your own lab before use in production enviroment.

# German Keyboard-Layout for Console Output. Can be skipped in english enviroments
#chcp 1252 >$null 2>&1

function ConfigureVolumeShadowCopies([string] $diskname, $vssMaxSizeInPercent) {

$xmlDocument = [xml] "<?xml version='1.0' encoding='UTF-16'?>
<Task version='1.1' xmlns='http://schemas.microsoft.com/windows/2004/02/mit/task'>
  <RegistrationInfo>
    <Author>Administrator</Author>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2018-01-01T08:00:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByWeek>
        <DaysOfWeek>
          <Monday />
          <Tuesday />
          <Wednesday />
          <Thursday />
          <Friday />
        </DaysOfWeek>
        <WeeksInterval>1</WeeksInterval>
      </ScheduleByWeek>
    </CalendarTrigger>
    <CalendarTrigger>
      <StartBoundary>2018-01-01T10:00:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByWeek>
        <DaysOfWeek>
          <Monday />
          <Tuesday />
          <Wednesday />
          <Thursday />
          <Friday />
        </DaysOfWeek>
        <WeeksInterval>1</WeeksInterval>
      </ScheduleByWeek>
    </CalendarTrigger>
    <CalendarTrigger>
      <StartBoundary>2018-01-01T12:00:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByWeek>
        <DaysOfWeek>
          <Monday />
          <Tuesday />
          <Wednesday />
          <Thursday />
          <Friday />
        </DaysOfWeek>
        <WeeksInterval>1</WeeksInterval>
      </ScheduleByWeek>
    </CalendarTrigger>
    <CalendarTrigger>
      <StartBoundary>2018-01-01T14:00:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByWeek>
        <DaysOfWeek>
          <Monday />
          <Tuesday />
          <Wednesday />
          <Thursday />
          <Friday />
        </DaysOfWeek>
        <WeeksInterval>1</WeeksInterval>
      </ScheduleByWeek>
    </CalendarTrigger>
    <CalendarTrigger>
      <StartBoundary>2018-01-01T16:00:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByWeek>
        <DaysOfWeek>
          <Monday />
          <Tuesday />
          <Wednesday />
          <Thursday />
          <Friday />
        </DaysOfWeek>
        <WeeksInterval>1</WeeksInterval>
      </ScheduleByWeek>
    </CalendarTrigger>
    <CalendarTrigger>
      <StartBoundary>2018-01-01T18:00:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByWeek>
        <DaysOfWeek>
          <Monday />
          <Tuesday />
          <Wednesday />
          <Thursday />
          <Friday />
        </DaysOfWeek>
        <WeeksInterval>1</WeeksInterval>
      </ScheduleByWeek>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id='Author'>
      <UserId>S-1-5-18</UserId>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
    <IdleSettings>
      <Duration>PT600S</Duration>
      <WaitTimeout>PT3600S</WaitTimeout>
      <StopOnIdleEnd>false</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT259200S</ExecutionTimeLimit>
    <Priority>5</Priority>
  </Settings>
  <Actions Context='Author'>
    <Exec>
      <Command>C:\Windows\system32\vssadmin.exe</Command>
      <Arguments>Create Shadow /AutoRetry=15 /For=\\?\Volume{REPLACEME}\</Arguments>
      <WorkingDirectory>%systemroot%\system32</WorkingDirectory>
    </Exec>
  </Actions>
</Task>"

# Query Volume GUID of the given Volume
$VolumeWmi = gwmi Win32_Volume -Namespace root/cimv2 | ?{ $_.Name -eq ($diskname +"\") }

# Build Variables
$DeviceID = $VolumeWmi.DeviceID.ToUpper().Replace("\\?\VOLUME", "").Replace("\","")
$TaskName = "ShadowCopyVolume" + $DeviceID
$TaskFor = "\\?\Volume" + $DeviceID + "\"

# Replace Volume GUID in XML-Data
$xmlDocument.Task.Actions.Exec.Arguments = "Create Shadow /AutoRetry=15 /For=$TaskFor"

# Write temporary XML-File for Import of scheduled Task with schtask
$xmlDocumentFileName = $PSScriptRoot + "\" + $TaskName + ".xml"
$xmlDocument.Save($xmlDocumentFileName)

# Try to Delete existing Task for VSS
try {
schtasks /delete /TN $Taskname /f
}
catch {
    #If Task can not be found: do nothing
}

# Create Scheduled Task
schtasks /Create /XML $xmlDocumentFileName /TN $TaskName

# Create (First) Snapshot
vssadmin Create Shadow /For=$diskname /AutoRetry=15

# Define Max Size of VSS Shadow Storage
vssadmin resize shadowstorage /For=$diskname /On=$diskname /MaxSize=$vssMaxSizeInPercent

# Delete temporary XML Document
Remove-Item $xmlDocumentFileName -Force

}

# Start Configuration - for Example on Volume C: with 10% MaxSize
ConfigureVolumeShadowCopies "C:" "10%"
SomeAdmin
  • 11
  • 1
0

This is the easiest way to enable Shadow copies via powershell, it works in Windows 10

Enable-ComputerRestore -Drive "C:\"
vssadmin resize shadowstorage /for=C: /on=C: /maxsize=10%

#Set Shadow Copy Scheduled Task for C: 6AM
$Action=New-ScheduledTaskAction powershell.exe (Get-WmiObject -List win32_shadowcopy).create("C:\","ClientAccessible")
$Principal=New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
$Trigger=New-ScheduledTaskTrigger -Daily -At 6:00AM
Register-ScheduledTask -TaskName ShadowCopyC_6AM -Trigger $Trigger -Action $Action -Principal $Principal -Description "ShadowCopyC_6AM"

#Set Shadow Copy Scheduled Task for C: 6PM
$Action=New-ScheduledTaskAction powershell.exe (Get-WmiObject -List win32_shadowcopy).create("C:\","ClientAccessible")
$Principal=New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
$Trigger=New-ScheduledTaskTrigger -Daily -At 6:00PM
Register-ScheduledTask -TaskName ShadowCopyC_6PM -Trigger $Trigger -Action $Action -Principal $Principal -Description "ShadowCopyC_6PM"

0

The best I can do is point you here, and specifically point out this one and this one.

Neither address exactly what you're looking for, but I will point out you can create shadow copies at any point as VSS is a background service that runs until called.

Holocryptic
  • 5,665
  • 2
  • 28
  • 37
0

I got exactly what i needed with the following code, my requirement was to configure VSS but also it needed to be visible in the GUI. The key part to make it visible in the GUI is to add the volume ID in the scheduled task name as pointed by user261949 (that's a bit strange, honestly i was searching for a registry key but i found absolutely nothing..). I ended up with the following code that suits my needs:

#Enable Volume Shadow copy

#Enable Shadows
vssadmin add shadowstorage /for=C: /on=C:  /maxsize=8128MB

#Create Shadows
vssadmin create shadow /for=C:

#create scheduled tasks
$diskname = "C:\"
$VolumeWmi = gwmi Win32_Volume -Namespace root/cimv2 | ?{ $_.Name -eq $diskname }
$DeviceID = $VolumeWmi.DeviceID.ToUpper().Replace("\\?\VOLUME", "").Replace("\","")
$TaskName = "ShadowCopyVolume" + $DeviceID
$TaskFor = "\\?\Volume" + $DeviceID + "\"
$Task = "C:\Windows\system32\vssadmin.exe"
$Argument = "Create Shadow /AutoRetry=15 /For=$TaskFor"
$WorkingDir = "%systemroot%\system32"

$ScheduledAction = New-ScheduledTaskAction -Execute $Task -WorkingDirectory 
$WorkingDir -Argument $Argument
$ScheduledTrigger = @()
$ScheduledTrigger += New-ScheduledTaskTrigger -Daily -At 07:00
$ScheduledTrigger += New-ScheduledTaskTrigger -Daily -At 12:00
$ScheduledSettings = New-ScheduledTaskSettingsSet -Compatibility V1 -DontStopOnIdleEnd -ExecutionTimeLimit (New-TimeSpan -Days 3) -Priority 5
$ScheduledTask = New-ScheduledTask -Action $ScheduledAction -Trigger 
$ScheduledTrigger -Settings $ScheduledSettings
Register-ScheduledTask $TaskName -InputObject $ScheduledTask -User 'NT AUTHORITY\SYSTEM'

Thanks all for your useful informations !