Script a backup of selected datasources Microsoft DPM 2010

Can be hard to work out this one, here is what worked for us.  Our requirement was to back up a selection of datasources from varying protection groups on a daily cycle.  The script below works the way we want, it is modified from a snipet we found ont there on google.

#####################################################################
# Any script which includes the next two lines can run DPM Scripts
# probably need to be running elevated though ...
add-pssnapin -name Microsoft.DataProtectionManager.PowerShell -ErrorAction SilentlyContinue
$Error.Clear()
 
# For try catch logic to work the way I expect
$errorActionPreference = 'Stop'
 
#####################################################################
 
#
# Variables you need to review
#
#####################################################################
# 
# The Dpm server we want to work with
#
$DpmServer="DPM2010.DOMAIN.COM" # enter here the name of your DPM server
$storage="Disk" # choose from Disk or Tape
$protectionType="LongTerm" # choose from LongTerm or ShortTerm
#
#
#
#####################################################################
 
function DoTheBackups([string] $dpmServer, [string] $pg, [string] $computerName,[string] $ds)
{
   $mypg = get-protectiongroup -dpmservername $dpmServer | where {$_.FriendlyName -ieq $pg}
 
   $myds = get-datasource -protectiongroup $mypg |
where {$_.Name -ieq $ds -and $_.ProductionServerName -ieq $computerName }
 
   if($myds) {
 
     write-host "`n`n" "Create recovery point for '$ds' on '$computername' to '$storage'"
 
     if ($storage -ieq "Tape") {
       try {
         $myjob = new-recoverypoint -datasource $myds -Tape -ProtectionType $protectionType
       }
       catch {
         write-host "`t" $_.Exception.Message
       }
   }
   else {
     try {
       $myjob = new-recoverypoint -datasource $myds -Disk -DiskRecoveryPointOption WithSynchronize
     }
     catch {
       write-host "`t" $_.Exception.Message
     }
   }
 
   if ($myJob) {
     $jobtype = $myjob.jobtype 
     write-host "`t`t" "Starting the process of $jobtype for tape backup"
 
     while (! $myjob.hascompleted ) 
     {
       write-host "`t`t`t" $myjob.Status
       start-sleep 5
     }
 
     if($myjob.Status -ne "Succeeded") 
     { 
       write-error "`t`t" "Job Failed" $pg $ds 
     }
     Else {
       Write-host "`t`t" "Job Succeeded" $pg $ds 
     }
  }
  else {
   write-host "Error creating new recovery point for '$ds' on '$computerName'"
  }
}
else {
   write-host "Data source '$ds' on protectiongroup '$pg' not found or not appropriate"
}
 
}
 
#
# Define what we want this script to do
#
#####################################################################
function main()
{
connect-dpmserver $DpmServer | out-null
#
# Add lines here for each datasource you want to backup
#
DoTheBackups -dpmserver $DpmServer -pg "PG-Desktops" -computerName "Parsley.domain.com" -ds "User data"
DoTheBackups -dpmserver $DpmServer -pg "Protection Group 2 - XPTESTPC1" -computerName "XPTESTPC1.domain.com" -ds "C:\"
 
disconnect-dpmserver $DpmServer | out-null
}
#####################################################################
 
#
# Run the main script
#
#####################################################################
main
#####################################################################