Skip to content

Instantly share code, notes, and snippets.

@EitanBlumin
Last active September 2, 2020 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save EitanBlumin/71e406de0cd65415b9ed34cc673ec56a to your computer and use it in GitHub Desktop.
Save EitanBlumin/71e406de0cd65415b9ed34cc673ec56a to your computer and use it in GitHub Desktop.
Generate a File Recovery script for a specified VM in an Azure Recovery Services Vault
# Copyright 2020 Eitan Blumin <@EitanBlumin, https://www.eitanblumin.com>
# while at Madeira Data Solutions <https://www.madeiradata.com>
#
# Licensed under the MIT License (the "License");
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
<#
.SYNOPSIS
This Powershell script connects to your Azure Portal account, and generates a File Recovery script for a specified VM in a Recovery Services Vault.
.EXAMPLE
C:\PS> .\Generate-FileRecoveryFromVault -VaultSubscriptionName "Microsoft Azure Sponsorship" `
-VaultName "VMBackupsVault" -VaultResourceGroupName "Contoso-RG" `
-SourceVMName "ProdVM01" -DaysBack 7
.LINK
https://docs.microsoft.com/en-in/azure/backup/backup-azure-vms-automation#restore-files-from-an-azure-vm-backup
.LINK
https://madeiradata.com
.LINK
https://eitanblumin.com
#>
Param(
[string]$VaultSubscriptionName,
[string]$VaultName,
[string]$VaultResourceGroupName,
[string]$SourceVMName,
[int]$DaysBack
)
if ($PSVersionTable.PSEdition -eq 'Desktop' -and (Get-Module -Name AzureRM -ListAvailable)) {
Write-Warning -Message ('Az module not installed. Having both the AzureRM and ' +
'Az modules installed at the same time is not supported.')
} elseif (-not (Get-InstalledModule Az -ErrorAction SilentlyContinue)) {
Install-Module -Name Az -AllowClobber -Scope CurrentUser -Force -Confirm -SkipPublisherCheck
}
Connect-AzAccount
Select-AzSubscription -SubscriptionName $VaultSubscriptionName | Out-Null
$targetVault = Get-AzRecoveryServicesVault -Name $VaultName -ResourceGroupName $VaultResourceGroupName
$namedContainer = Get-AzRecoveryServicesBackupContainer -ContainerType "AzureVM" -Status "Registered" -FriendlyName $SourceVMName -VaultId $targetVault.ID
$backupitem = Get-AzRecoveryServicesBackupItem -Container $namedContainer -WorkloadType "AzureVM" -VaultId $targetVault.ID
$startDate = (Get-Date).AddDays(-$DaysBack)
$endDate = Get-Date
$rp = Get-AzRecoveryServicesBackupRecoveryPoint -Item $backupitem -StartDate $startdate.ToUniversalTime() -EndDate $enddate.ToUniversalTime() -VaultId $targetVault.ID
Write-Verbose $rp[0]
$mountScript = Get-AzRecoveryServicesBackupRPMountScript -RecoveryPoint $rp[0] -VaultId $targetVault.ID
Write-Host "== File Saved to: =="
Write-Host $mountScript.FilePath
Write-Host "== Password: =="
Write-Host $mountScript.Password
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment