HyperCloud PoSH
Have you ever wanted backup your Hyper-V virtual machines directly to a cloud provider with minimal effort? HyperCloud PoSH does just that. By “installing” this script with the addition of rClone, you can set up task scheduler to automatically export each one of your virtual machines to your cloud provider on any time schedule you may desire. In my use case HyperCloud PoSH exports all of my virtual machines to a folder nightly and have rClone move them to my Google Drive account where they are archived. Please note that certain cloud providers have a file size limit, for example Amazon Cloud being a maximum of 25GB per file. I suggest using GSuite where Google Drive has a maximum file size of 5TB per file. This is very useful for exporting large virtual machines. HyperCloud PoSH works with all cloud providers that rClone supports.
HyperCloud PoSH Script:
Import-Module Hyper-V # Imports Hyper-V management module for PoSH $date = Get-Date -Format yyyy-MM-dd # Gets current date for filename and path purposes $path = "C:\Exports\$date" # Sets the export location to include the date Start-Transcript -Path "C:\Scripts\Logs\ScheduledHyperClone-$date.txt" #starts transcription of the console $allVMs = Get-VM # Retrieves all VM's to be exported Write-Output "Testing if export path exists..." if (!(Test-Path $path)) # Determines whether the export location exists { New-Item $path -Type Directory # Creates path if it doesnt exist Write-Output "Export path did not exist, it has been created at $path" } else { Write-Output "Beginning rclone" $CMD = 'rclone.exe' $arg1 = 'move' $arg2 = '--transfers=7' $arg3 = '--stats=10m' $arg4 = $path $arg5 = 'gdrive:HyperAuto' $arg6 = $date $arg7 = '-q' &$CMD $arg1 $arg2 $arg3 $arg4 $arg5'\'$arg6 $arg7 #Write-Output $($arg1 + ' ' + $arg2 + ' ' + $arg3 + ' ' + $arg4 + ' ' + $arg5 + '' + '\' + $arg6) Write-Output "rclone completed, rcloned the following files:" $arg1 = "lsd" $arg2 = "gdrive:HyperAuto" $arg3 = "$date" &$CMD $arg1 $arg2'\'$arg3 Write-Output "Backup complete." } Write-Output "Export path exists, moving on to exports." # foreach ($VM in $allVMs) { Write-Output "Attempting to export the VM $($VM.VMName)" Export-VM -Path $path -Name $VM.VMName -ErrorAction "SilentlyContinue" # exports the job with the paremeters defined above } Write-Output "Waiting for all exports to finish.." $vmstatus = Get-VM | select secondaryoperationalstatus while ($vmstatus -like "*export*") { Start-Sleep 30 Write-Output 'Waiting' $vmstatus = Get-VM | select secondaryoperationalstatus } $successfulExports = gci $path # Gets a list of all items in the export folder and lists them Write-Output "Successfully exported the following VMs;" Write-Output "$successfulExports" Write-Output "Beginning rclone" $CMD = 'rclone.exe' $arg1 = 'move' $arg2 = '--transfers=7' $arg3 = '--stats=10m' $arg4 = $path $arg5 = 'gdrive:HyperAuto' $arg6 = $date $arg7 = '-q' #& $CMD $($arg1 + ' ' + $arg2 + ' ' + $arg3 + ' ' + $arg4 + ' ' + $arg5 + '' + '\' + $arg6) &$CMD $arg1 $arg2 $arg3 $arg4 $arg5'\'$arg6 $arg7 Write-Output "rclone completed, rcloned the following files:" $arg8 = "lsd" $arg9 = "gdrive:HyperAuto" $arg10 = $date &$CMD $arg8 $arg9'/'$arg10 Write-Output "Backup complete." Stop-Transcript #stops the transcripting of the logs break
The defaults for this script are as followed:
- Hyper-V exports to the C:\Exports\(date) folder.
- rClone moves data from C:\Exports\(date) folder to the “HyperAuto” folder in the root directory of gdrive.
- All logging goes into the C:\Scripts\Logs\ folder.
If you are a season Windows Server Pro, all you have to do is put HyperCloud PoSH into your C:\Scripts
folder and add the script location to the Windows Task Scheduler.
If you are not a seasoned user of Windows, no problem. I’ll show you the basics of getting this setup and working.
This script also assumes that you have rClone already installed onto your server with a cloud destination setup and rClone added to PATH.
Step 1: Adding rClone to global PATH variable
In order for HyperCloud PoSH to function properly, we need to setup rClone to be in the system environment variables. This is needed to execute “rclone” in any folder on PoSH.
To do this, open Explorer and right click on “This PC” selecting “Properties”.
Once you have done this the system properties box will open. Click “Environment Variables…” then under “System variables” edit the “Path” variable and add the location of your rClone folder to the end. For my server rClone.exe is located in the C:\Users\root\rclone
directory.
Step 2: Creating folders and adding HyperCloud PoSH
We need a place to put this script. Lets create the directory C:\Scripts
where we will copy and paste the above script into a PowerShell format. Lets call it rClone.ps1. Also, while we are in the folder, we also need to create the sub-folder Logs. This is where our logs of the script will go.
Step 3: Setting up Task Scheduler
Fist open Task Scheduler. You can find it by opening run and typing “taskschd.msc” Once you are in the main screen, click on “Task Scheduler Library” under “Task Scheduler (Local)”. Create a new task with the following properties, while modifying the configuration to match your system users.
Depending on how you want the backups done you can change the frequency and time that the script runs. I prefer to run it after midnight at 1:00 AM every day.
On the next tab “Actions” we now need to point task scheduler to run PowerShell and execute our script.
It is also good practice to set it up so that if the script runs longer than a day to stop the task. Lets do that in the “Settings” tab.
Testing
Before setting and forgetting this script, lets verify that everything is in working order, it is easiest to do this through PowerShell and manually running the script to see if there are any errors in real time, but if you would like you can just run the script through task scheduler by right clicking on the new task and clicking “Run”
Conclusion
I hope this script worked for you as it has made my life so much easier, utilizing the light weight rclone program along with hyper-v’s native exporting function.
Please don’t hesitate to leave a comment for any assistance you may need.
Check out this other handy tutorial for fixing an import error for Hyper-V!
How To Fix: Virtual machine cannot start – The security ID structure is invalid (0x80070539)