Guest User

RestoreFromVss

a guest
Nov 8th, 2017
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $restore_path = "C:\TempRestorePoint"
  2.  
  3. function get_volumes() {
  4.     $volumesText = vssadmin list volumes
  5.  
  6.     $volumes = @{}
  7.  
  8.     for ($count = 0; $count -lt $volumesText.Length; $count++) {
  9.         if ($volumesText[$count].StartsWith("Volume path")) {
  10.             $letter = $volumesText[$count].Split(" ")[2]
  11.             if ($letter.StartsWith("\\") -eq $false) {
  12.                 $name = $volumesText[$count+1].Split(" ")[6]
  13.                 $volumes.Add($letter,$name)
  14.                 $count++
  15.             }
  16.         }
  17.     }
  18.  
  19.     return $volumes
  20. }
  21.    
  22. function get_volume_name() {
  23.     $volumes = get_volumes
  24.     $index = 0
  25.     foreach ($key in $volumes.Keys) {
  26.         $index ++
  27.         Write-Host "Drive $key"
  28.     }
  29.     $volName = Read-Host -Prompt "Please enter a drive letter"
  30.  
  31.     $volName = $volName+":\"
  32.     While($true) {
  33.         if ($volumes.ContainsKey($volName)) {
  34.             return $volumes[$volName]
  35.         } else {
  36.             $volName = Read-Host -Prompt "Please enter an EXISTING drive letter"
  37.             $volName = $volName+":\"
  38.         }
  39.     }
  40. }
  41.  
  42. function get_vss_objects() {
  43.     $volumeName = get_volume_name
  44.     $vss = vssadmin list shadows
  45.     $vss_objects = @()
  46.     for ($count = 1; $count -lt $vss.Count; $count++)
  47.     {
  48.         if ($vss[$count] -like "*$volumeName*") {
  49.             $date = $vss[$count-2].Split(" ")[10]
  50.             $day = $date.Split(".")[0]
  51.             $month = $date.Split(".")[1]
  52.             $year = $date.Split(".")[2]
  53.             $time = $vss[$count-2].Split(" ")[11]
  54.             $hours = $time.Split(":")[0]
  55.             $minutes = $time.Split(":")[1]
  56.             $scvol = $vss[$count+1].Split(" ")[12]
  57.  
  58.             $vss_object = New-Object psobject
  59.             $vss_object | Add-Member NoteProperty day ([convert]::ToInt32($day,10))
  60.             $vss_object | Add-Member NoteProperty month ([convert]::ToInt32($month,10))
  61.             $vss_object | Add-Member NoteProperty year ([convert]::ToInt32($year,10))
  62.             $vss_object | Add-Member NoteProperty hours ([convert]::ToInt32($hours,10))
  63.             $vss_object | Add-Member NoteProperty minutes ([convert]::ToInt32($minutes,10))
  64.             $vss_object | Add-Member NoteProperty scvol $scvol
  65.  
  66.             $vss_objects += $vss_object
  67.         }
  68.     }
  69.     return $vss_objects
  70. }
  71.  
  72.  
  73. function get_vss_objects_for_day($vss_objects) {
  74.     # ASK FOR YEAR #
  75.     $first_year = $vss_objects[0].year
  76.     $this_year = (Get-Date).Year
  77.     if ($this_year -eq $first_year) {
  78.         # only this_year
  79.         $year = $this_year
  80.     } else {
  81.         # from first_year until this_year
  82.         $years = @()
  83.         for ($year = $first_year; $year -le $this_year; $year++) {
  84.             $years += $year
  85.         }
  86.         $answerOK = $false
  87.         do {
  88.             $year = [convert]::ToInt32((Read-Host -Prompt "Please choose a year"),10)
  89.             if ($years.contains($year)) {
  90.                 $answerOK = $true
  91.             } else {
  92.                 foreach ($wr_year in $years) {
  93.                     Write-Host $wr_year
  94.                 }
  95.             }
  96.         } while ($answerOK -eq $false)
  97.  
  98.     }
  99.     # ASK FOR YEAR END #
  100.     # ASK FOR MONTH #
  101.     $vss_objects = $vss_objects | Where {$_.year -eq $year}
  102.     $first_month = $vss_objects[0].month
  103.     $last_month = $vss_objects[$vss_objects.Length-1].month
  104.     $months = @()
  105.     for ($month = $first_month; $month -le $last_month; $month++) {
  106.         $months += $month
  107.     }
  108.     $answerOK = $false
  109.     do {
  110.         $month = [convert]::ToInt32((Read-Host -Prompt "Please choose a month"),10)
  111.         if ($months.contains($month)) {
  112.             $answerOK = $true
  113.         } else {
  114.             foreach ($wr_month in $months) {
  115.                 Write-Host $wr_month
  116.             }
  117.         }
  118.     } while ($answerOK -eq $false)
  119.     # ASK FOR MONTH END #
  120.     # ASK FOR DAY #
  121.     $vss_objects = $vss_objects | Where {$_.month -eq $month}
  122.     $first_day = $vss_objects[0].day
  123.     $last_day = $vss_objects[$vss_objects.Length-1].day
  124.     $days = @()
  125.     for ($day = $first_day; $day -le $last_day; $day++) {
  126.         $days += $day
  127.     }
  128.     $answerOK = $false
  129.     do {
  130.         $day = [convert]::ToInt32((Read-Host -Prompt "Please choose a date"),10)
  131.         if ($days.contains($day)) {
  132.             $answerOK = $true
  133.         } else {
  134.             foreach ($wr_day in $days) {
  135.                 Write-Host $wr_day
  136.             }
  137.         }
  138.     } while ($answerOK -eq $false)
  139.     # ASK FOR DAY END #
  140.     return $vss_objects | Where {$_.year -eq $year -and $_.month -eq $month -and $_.day -eq $day}
  141. }
  142.  
  143. $vss_objects = get_vss_objects
  144. do {
  145.     $vss_day_objects = get_vss_objects_for_day($vss_objects)
  146.     $vss_hashtable = @{}
  147.     if ($vss_day_objects.GetType().IsArray) {
  148.         $answerOK = $false
  149.         do {
  150.             for ($index = 0; $index -lt $vss_day_objects.Length; $index++) {
  151.                 $vss_hashtable.Add(($index+1),$vss_day_objects[$index].scvol)
  152.                 if ($vss_day_objects[$index].hours -lt 10) {
  153.                     $hour = "0" + $vss_day_objects[$index].hours
  154.                 } else {
  155.                     $hour = $vss_day_objects[$index].hours
  156.                 }
  157.                 if ($vss_day_objects[$index].minutes -lt 10) {
  158.                     $minute = "0" + $vss_day_objects[$index].minutes
  159.                 } else {
  160.                     $minute = $vss_day_objects[$index].minutes
  161.                 }
  162.                 Write-Host ($index+1) "-" $hour":"$minute "Uhr"
  163.             }
  164.             $promt = "Please enter number (1 - "+$vss_day_objects.Length+") or '0' for a different day"
  165.             $choose = [convert]::ToInt32((Read-Host -Prompt $promt),10)
  166.  
  167.             if ($choose -eq 0 -or $vss_hashtable.Keys -contains $choose) {
  168.                 $answerOK = $true
  169.             }
  170.         } while ($answerOK -eq $false)
  171.     } else {
  172.         $answerOK = $false
  173.         do {
  174.             $vss_hashtable.Add(1,$vss_day_objects.scvol)
  175.             if ($vss_day_objects.hours -lt 10) {
  176.                 $hour = "0" + $vss_day_objects.hours
  177.             } else {
  178.                 $hour = $vss_day_objects.hours
  179.             }
  180.             if ($vss_day_objects.minutes -lt 10) {
  181.                 $minute = "0" + $vss_day_objects.minutes
  182.             } else {
  183.                 $minute = $vss_day_objects.minutes
  184.             }
  185.             Write-Host "1 -" $hour":"$minute
  186.             $choose = [convert]::ToInt32((Read-Host -Prompt "Please confirm with '1' or enter '0' for a different day"),10)
  187.  
  188.             if ($choose -eq 0 -or $vss_hashtable.Keys -contains $choose) {
  189.                 $answerOK = $true
  190.             }
  191.         } while ($answerOK -eq $false)
  192.     }
  193. } while ($choose -eq 0)
  194.  
  195. $shadow_path = $vss_hashtable[$choose]+"\"
  196.  
  197. if (Test-Path($restore_path)) {
  198.     $message = "$restore_path already exists and will be over written. Continue?"
  199.     $answer = [System.Windows.Forms.MessageBox]::Show($message,"Warnung",4)
  200.     if ($answer -eq "No") {
  201.         exit
  202.     }
  203.     cmd /c rmdir $restore_path
  204. }
  205. cmd /c mklink /D $restore_path $shadow_path
Advertisement
Add Comment
Please, Sign In to add comment