DPM Restore to remote server with folder rename and delete

    General discussion

  • Hi,

    I have some trouble to automate this task.

    I have 2 servers lets say Srv-Dpm and Srv-HyperV, what I want to do is to rename older backup with a script than currently is on the HyperV server, next restore DPM backups on a Drive on HyperV server and after that delete old folders. All this with the scheduler if possible...

    Restore Script on DPM Server:

    #### Main ####
    param([string] $restorePath = "F:\DPM", [string] $targetServerName = "Srv-HyperV"#0. Initialize variables
    $dpmName = $env:computername
        $restorePath = Read-Host "Enter the path to where the VMs should be restored"
    if (!$targetServerName)
        $targetServerName = Read-AHost "Enter netbios name of the target server"
    #1. Get all Protection Groups
    $pgList = Get-ProtectionGroup -dpmServerName $dpmName
    #2. Get all Hyper-V Datasources
    $hypervDSList = @()
    ForEach ($pg in $pgList)
     $hypervDSList += @(Get-Datasource -protectionGroup $pg | where {$_.Type.Name -eq "Hyper-V" -and $_.Name -notcontains "Initial Store"})
    #3. Do Inquiry on target server for DPM to discover the available volumes
    Write-Host "Getting available volumes on Target Server"
    $targetServer = Get-ProductionServer -dpmServerName $dpmName | where {$_.NetBiosName -eq $targetServerName}
    Get-Datasource -ProductionServer $targetServer -Inquire | Out-Null
    #4. Recover datasources one by one.
    ForEach ($hypervDS in $hypervDSList)
     #4.1 Get the latest recovery point.
     $recoveryPoints = @(Get-RecoveryPoint -datasource $hypervDS | sort -property RepresentedPointInTime -Descending)
        if ($recoveryPoints.Length -eq 0)
            Write-Host "No recovery point available for" $hypervDS.Name -ForeGroundColor Yellow
        $latestRecoveryPoint = $recoveryPoints[0]
     #4.2 Recover it to target server as files
     $rop = New-RecoveryOption -HyperVDatasource -targetServer $targetServerName -recoveryLocation CopyToFolder -RecoveryType Restore -TargetLocation $restorePath
        Write-Host "Recovering" $hypervDS.Name -NoNewLine
     $recoveryJob = Recover-RecoverableItem -RecoverableItem $latestRecoveryPoint -recoveryoption $rop -JobStateChangedEventHandler $jobStateChangeHandler
            Write-Host "Failure on invoking Recover-RecoverableItem" -ForeGroundColor Red
        #4.3 Wait till the recovery job completes
        while (! $recoveryJob.hasCompleted )
            # Show a progress bar
            Write-Host "." -NoNewLine
            Start-Sleep 5
     if($recoveryJob.Status -ne "Succeeded")
            Write-Host "Recovery failed" -ForeGroundColor Red
        Write-Host "Recovery successful" -ForeGroundColor Green


    Bat File Called from scheduler:


    C:\Windows\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft DPM\DPM\bin\dpmshell.psc1" -noexit -command ".'C:\scripts\restore.ps1'"


    Rename Script on HyperV



    $FolderToRename = Get-ChildItem -Path F:\DPM
    foreach ($str in $FolderToRename){
     $Renomme = [string]$str.Name + ".old"
     Rename-Item $str.FullName -NewName $Renomme


    Delete Script on HyperV



    $FolderToDel = Get-ChildItem F:\DPM | Where-Object {$ -like "*.old"}
    foreach ($str in $FolderToDel){
     Remove-Item F:\DPM\$str -recurse 


    Both Called from a Bat File.

    The problem I have; first If I schedule the Bat file to launch for the DPM restore task nothing happens? Work great manualy

    Second thing is, can I execute the Rename script before backup launch(all in one) for the remote HyperV server (tried Invoke-Command, but can't get it to work), and the Delete just after it finished of curse.


    All this must go with scheduler, and nothing should be ask (example: "-credential from invoke-command with the password popup").

    Really need some help on this! Thanks to read.


    Sunday, December 04, 2011 3:16 PM

All replies

  • Are you setting up the scheduled task to run as a domain user that has permission on the remote server?
    Thanks, Wilson Souza - MSFT This posting is provided "AS IS" with no warranties, and confers no rights
    Saturday, December 17, 2011 7:01 AM
  • Hi Wilson,

    Yes the scheduled task is running with a domain admin user (both are in the same domain of course), I don't think there is a permission issue.

    Thanks for helping! Still searching... 

    Tuesday, December 20, 2011 10:02 AM
  • Still searching, if someone could gave me something to advance.

    Would be greatly appreciated.


    Monday, January 02, 2012 2:06 PM
  • From these three scripts,

    Which one is not working when launched via Scheduled Task?


    Thanks, Wilson Souza - MSFT This posting is provided "AS IS" with no warranties, and confers no rights
    Monday, January 02, 2012 9:04 PM