Perform a Nightly Backup of Service Templates (Script)

 

Updated: May 13, 2016

Applies To: System Center 2012 R2 Virtual Machine Manager, System Center 2012 - Virtual Machine Manager

The script in this topic backs up service templates from the library on a nightly basis by exporting template packages. For an example script that demonstrates how to restore a service template to the library by using an exported package, see Import a Missing Service Template from a Previous Backup (Script).

Disclaimer

Scheduling a Task by using Task Scheduler

To run a script on a scheduled basis, you need to create a task in the Windows Task Scheduler service. The action in your task must start Powershell.exe and load the System Center 2012 – Virtual Machine Manager (VMM) module in order to run the VMM cmdlets that are used by the script. The following syntax starts Powershell.exe, loads the VMM module, and then runs the script named NightlyTemplateBackup.ps1 located in the C:\MyScripts folder:

PowerShell.exe -NoExit ipmo virtualmachinemanager; C:\MyScripts\NightlyTemplateBackup.ps1

You can set a trigger for this action that starts the process at the same time every day, such as 11:00 PM.

The following script gets all service templates, creates a folder in which to store the exported packages, and then exports the templates.

  
# Description:    This script backs up service templates from the library by  
#                 exporting the service templates to a file path.  
  
# Get all service templates.  
$ServiceTemplates = Get-SCServiceTemplate  
  
# Create a folder for today's backup.  
$Date = Get-Date -uformat "%Y_%m_%d"  
$Dir = New-Item -Type Directory -Path D:\TemplateExports\$Date  
$Path = $Dir.FullName  
  
# Export the service templates.  
Foreach ($Template in $ServiceTemplates)  
{  
   Export-SCTemplate -ServiceTemplate $Template -Path $Path -SettingsIncludePrivate  
}  
  

See Also

Get-SCServiceTemplate
Export-SCTemplate