< Blog

Using a PowerShell Script with AfterBackup.bat in Configuration Manager 2012 and 2007

By Garth Jones

In 2011, I created a batch file to keep 7 days’ worth of a Configuration Manager backup directory on another server (http://smsug.ca/blogs/garth_jones/archive/2011/01/13/afterbackup-bat.aspx).

This batch file makes one directory for each day of the week using the first 3 characters of the date format. It still works great, but only if your date format is the same as the one below (mm-dd-yy). This happens to be the default date format in the US.

AfterBackup and PowerShell-US Date Format

However, it won’t work if your date format is different, such as the one below (yy-mm-dd). This is the default date format in Canada.

AfterBackup and PowerShell-Canada Date Format

While working with a colleague, Damien Redhead, he suggested that the easiest solution to having my batch file work with any date format was to convert it to a PowerShell script; enabling it to work with any date format.

I’m not a big fan of PowerShell, so I asked Damien to write the script for me. Damien doesn’t have a blog, so I agreed to post the PowerShell script for him.

You’ll see below that Backup.ps1 performs the same tasks as my old batch file. It copies the ConfigMgr 2012 or ConfigMgr 2007 backup directory to another network share and keeps 7 days’ worth of backups. You then launch the PowerShell script from the afterbackup.bat; the command line is listed below.

AfterBackup.bat

@ECHO OFF

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -command “. ‘E:\Program Files\Microsoft Configuration Manager\inboxes\smsbkup.box\backup.ps1′”

Backup.ps1

##############################################################

## Author:   Damien Redhead                               ##

## Date:     December 02, 2014                            ##

## Purpose:  Transfer local backup of SCCM to remote server##

##############################################################

$currentDay = (Get-Date).dayofweek

$localPath = “E:\CMBak\*”

## Send to FSP

$remotePath = \\ServerName\CMBak\$currentDay\Site Code\

if (Test-Path -Path $remotePath)

{

      Remove-Item -Recurse -Force $remotePath

}

copy-item $localPath $remotePath -force -recurse -verbose