Powershell script to automate azure linux VM backup and restore from azure devops pipeline - azure

I tried multiple powershell script to do azure VM backup and restore but none of them are seems working or not so effective. Could any one please share the powershell script to create backup of VM and restore in azure via azure devops pipeline.
PowerShell Script:
<#
.SYNOPSIS
Written By John Lewis
email: jonos#live.com
Ver 1.1
v 1.2 updates - minor fixes
v 1.1 updates - Added Pre-Checks
v 1.0 updates - RTM
Deploys Azure Backup Vault and configures Azure VMs to leverage vault. Provides automated restore to new VM.
.PARAMETER Action
.PARAMETER backupvmname
.PARAMETER backupvmrg
.PARAMETER rg
.PARAMETER policyname
.PARAMETER containertype
.PARAMETER Location
.PARAMETER wrkloadtype
.PARAMETER vaultname
.PARAMETER createvmname
.PARAMETER createvmrg
.PARAMETER vaultrg
.PARAMETER vnet
.PARAMETER vnetrg
.PARAMETER createvmsubnet
.EXAMPLE
.\AZRM-VMBackup.ps1 -csvimport -csvfile C:\temp\backupservers.csv
.EXAMPLE
.\AZRM-VMBackup.ps1 -action createpolicy -vaultname myvault -vaultrg myres -policyname mypolicy
.EXAMPLE
.\AZRM-VMBackup.ps1 -action createvault -vaultname myvault -vaultrg myres
.EXAMPLE
.\AZRM-VMBackup.ps1 -action addvmcreatevault -backupvmname myvm -backupvmrg myres -vaultname myvault -vaultrg myres
.EXAMPLE
.\AZRM-VMBackup.ps1 -action restorevm -createvmname myvm -createvmrg myres -vaultrg backuprg -vaultname myvaultname -vnet vnet -vnetrg myvnetrg
#>
[CmdletBinding(DefaultParameterSetName = 'default')]
Param(
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateNotNullorEmpty()]
[ValidateSet("createpolicy","createvault","addvmcreatevault","addvmtovault","restorevm","executebackup","getstatus")]
[string]
$Action = 'createvault',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$vaultrg = "vault",
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$storeagerg = $createvmrg,
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateNotNullorEmpty()]
[string]
$location = "East US",
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$vaultname = "vault",
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$backupvmname = '',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$backupvmrg = '',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateNotNullorEmpty()]
[string]
$backupvmlocation = $location,
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$createvmname = '',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$createvmrg = '',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateNotNullorEmpty()]
[string]
$createvmlocation = $location,
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true,Position=4)]
[ValidateNotNullorEmpty()]
[Alias("createvmvnet")]
[string]
$VNetName = 'vnet',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateNotNullorEmpty()]
[string]
$vnetrg = $createvmrg,
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$policyname = 'policy',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$containertype = 'AzureVM',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$wrkloadtype = 'AzureVM',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$provider = "Microsoft.RecoveryServices",
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$Profile = "profile",
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$csvfile = -join $workfolder + "\azrm-vmbackup.csv",
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[Alias("csv")]
[switch]
$csvimport,
[Parameter(Mandatory=$False)]
[string]
$GenerateName = -join ((65..90) + (97..122) | Get-Random -Count 6 | % {[char]$_}) + "rmp",
[Parameter(Mandatory=$False)]
[string]
$StorageName = $createvmname + 'str',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateSet("Standard_LRS","Standard_GRS")]
[string]
$StorageType = 'Standard_GRS',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateNotNullorEmpty()]
[Alias("int1")]
[string]
$InterfaceName1 = $createvmname + '_nic1',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[Alias("int2")]
[string]
$InterfaceName2 = $createvmname + "_nic2",
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$SubscriptionID = '',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$TenantID = '',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateSet("InProgress","Completed")]
[string]
$status = 'InProgress',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateRange(0,8)]
[ValidateNotNullorEmpty()]
[Alias("createvmsubnet")]
[Int]
$Subnet1 = 4
)
$workfolder = Split-Path $script:MyInvocation.MyCommand.Path
$ProfileFile = $workfolder+'\'+$profile+'.json'
$restorejson = $workfolder+'\'+ 'config.json'
Function validate-profile {
$comparedate = (Get-Date).AddDays(-14)
$fileexist = Test-Path $ProfileFile -NewerThan $comparedate
if($fileexist)
{
$az = Import-AzureRmContext -Path $ProfileFile
$subid = $az.Context.Subscription.Id
Set-AzureRmContext -SubscriptionId $subid | Out-Null
Write-Host "Using $ProfileFile"
}
else
{
Write-Host "Please enter your credentials"
Add-AzureRmAccount
Save-AzureRmContext -Path $ProfileFile -Force
Write-Host "Saved Profile to $ProfileFile"
exit
}
}
Function csv-run {
param(
[string] $csvin = $csvfile
)
$GetPath = test-path -Path $csvin
if(!$csvin)
{exit}
else {
Write-Host $GetPath "File Exists"
import-csv -Path $csvin -Delimiter ',' -ErrorAction SilentlyContinue -InformationAction SilentlyContinue | ForEach-Object{.\AZRM-VMBackup.ps1 -VMName $_.VMName -rg $_.rg -Action $_.Action -vmrg $_.vmrg -servaultname $_.servaultname }
}
}
Function Check-StorageName
{
param(
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]$StorageName = $StorageName
)
$checkname = Get-AzureRmStorageAccountNameAvailability -Name $StorageName | Select-Object -ExpandProperty NameAvailable
if($checkname -ne 'False') {
Write-Host "Storage Account Found..."
$script:StorageNameVerified = $StorageName.ToLower()
}
else
{
$script:StorageNameVerified = $StorageName.ToLower()
Create-Storage
}
}
#region Create Storage
Function Create-Storage {
param(
[string]$StorageName = $script:StorageNameVerified,
[string]$StorageType = $StorageType,
[string]$containerName = 'vhds',
[string]$Location = $Location,
[string]$storeagerg = $storeagerg
)
Write-Host "Starting Storage Creation..."
$script:StorageAccount = New-AzureRmStorageAccount -ResourceGroupName $storeagerg -Name $StorageName.ToLower() -Type $StorageType -Location $Location -ErrorAction Stop -WarningAction SilentlyContinue
Write-Host "Completed Storage Creation" -ForegroundColor White
}
Function Check-Vnet {
$vnetexists = Get-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $vnetrg -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
if(!$vnetexists)
{
Write-Host "$VNetName does not exists!"
break
}
else
{ $existvnet = Get-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $vnetrg
$addspace = $existvnet.AddressSpace | Select-Object -ExpandProperty AddressPrefixes
$namespace = $existvnet.Name
$existvnet = Get-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $vnetrg
$addsubnet = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $existvnet
$sub = $addsubnet.AddressPrefix
$subname = $addsubnet.Name
$nsg = $addsubnet.NetworkSecurityGroup
$subnet = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $existvnet | ft Name,AddressPrefix -AutoSize -Wrap -HideTableHeaders
Write-Host " "
Write-Host "VNET CONFIGURATION - Existing VNET" -ForegroundColor Cyan
Write-Host " "
Write-Host "Active VNET: $VnetName in resource group $vnetrg"
Write-Host "Address Space: $addspace "
Write-Host "Subnet Ranges: $sub "
Write-Host "Subnet Names: $subname "
}
}
Function Check-VaultExists {
$vaultexists = Get-AzureRmRecoveryServicesVault -Name $vaultname -ResourceGroupName $vaultrg -ErrorAction Stop -WarningAction SilentlyContinue -InformationAction SilentlyContinue
if(!$vaultexists)
{
Write-Host "Specified vault $vaultname does not exist!" -ForegroundColor Red
break
}
}
function Check-VaultNullValues {
if(!$vaultrg) {
Write-Host "Please select -vaultrg" -ForegroundColor Red
exit
}
elseif(!$vaultname) {
Write-Host "Please Enter -vaultname" -ForegroundColor Red
exit
}
elseif(!$Location) {
Write-Host "Please Enter -Location" -ForegroundColor Red
exit
}
}
function Check-BackupVMNullValues {
if(!$backupvmrg) {
Write-Host "Please select -backupvmrg" -ForegroundColor Red
exit
}
elseif(!$backupvmname) {
Write-Host "Please Enter -backupvmname" -ForegroundColor Red
exit
}
elseif(!$Location) {
Write-Host "Please Enter -Location" -ForegroundColor Red
exit
}
}
function Check-CreateVMNullValues {
if(!$createvmrg) {
Write-Host "Please select -createvmrg" -ForegroundColor Red
exit
}
elseif(!$createvmname) {
Write-Host "Please Enter -createvmname" -ForegroundColor Red
exit
}
if(!$backupvmrg) {
Write-Host "Please select -backupvmrg" -ForegroundColor Red
exit
}
elseif(!$backupvmname) {
Write-Host "Please Enter -backupvmname" -ForegroundColor Red
exit
}
elseif(!$Location) {
Write-Host "Please Enter -Location" -ForegroundColor Red
exit
}
}
function Check-StorageNullValues {
if(!$storeagerg) {
Write-Host "Please select -storage" -ForegroundColor Red
exit
}
elseif(!$StorageName) {
Write-Host "Please Enter -createvmname" -ForegroundColor Red
exit
}
elseif(!$Location) {
Write-Host "Please Enter -Location" -ForegroundColor Red
exit
}
}
function Check-VNETNullValues {
if(!$vnetrg) {
Write-Host "Please select -vnetrg" -ForegroundColor Red
exit
}
elseif(!$VNetName) {
Write-Host "Please Enter -vnetname" -ForegroundColor Red
exit
}
elseif(!$Location) {
Write-Host "Please Enter -Location" -ForegroundColor Red
exit
}
}
function Reg-Provider {
param($provider = $provider)
Register-AzureRmResourceProvider -ProviderNamespace $provider
}
Function Provision-RG
{
Param(
[string]$rg = $vaultrg
)
New-AzureRmResourceGroup -Name $rg -Location $Location –Confirm:$false -WarningAction SilentlyContinue -Force | Out-Null
}
Function Check-VMExists {
param(
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$Location = $Location,
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$rg = $backupvmrg,
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$VMName = $backupvmname
)
$extvm = Get-AzureRmVm -Name $VMName -ResourceGroupName $rg -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
if(!$extvm)
{
Write-Host "$VMName does not exist, please verify the VM exists" -ForegroundColor Yellow
Break
}
else {Write-Host "Restoration VM found" -ForegroundColor Green}
} #
#endregion
Function Configure-Backup {
param(
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$action = $action
)
switch ($action)
{
"createvault" {
Check-VaultNullValues
Write-Host "Creating new Backup Vault $vaultname"
Write-Config
Provision-RG
Create-Vault
Get-CurrentPolicies
Create-Policy
Write-Completion
}
"createpolicy" {
Write-Host "Creating new Policy"
Write-Config
Check-VaultExists
Get-CurrentPolicies
Create-Policy
Write-Completion
}
"addvmtovault" {
Check-VMExists
Check-VaultExists
Write-Host "Adding VM $backupvmname to $vaultname"
Write-Config
Get-CurrentPolicies
AddVM-Vault
Write-Completion
}
"executebackup" {
Write-Host "Executing backup of $backupvmname to $vaultname"
Check-VMExists
Write-Config
Check-BackupVMNullValues
Get-CurrentPolicies
TriggerBackup-Vault
Write-Completion
}
"addvmcreatevault" {
Check-VaultNullValues
Write-Host "Creating new Backup Vault $vaultname"
Write-Config
Provision-RG
Create-Vault
Create-Policy
Check-BackupVMNullValues
Check-VaultExists
Check-VMExists
Write-Host "Adding VM $backupvmname to $vaultname"
Get-CurrentPolicies
AddVM-Vault
Write-Completion
}
"restorevm" {
Check-CreateVMNullValues
Check-VNETNullValues
Check-Vnet
Check-StorageNullValues
Check-VMExists
Check-StorageName
Write-Host "Restoring VM Backup from $backupvmname to $createvmname"
Write-Config
Restore-VMVHD
Create-VM
Write-Completion
}
"getstatus" {
Write-Host "Obtaining current job information"
Get-JobProgress
}
default{"An unsupported backup command was used"}
}
exit
}
Function Get-JobProgress {
param(
$status = $status
)
Get-AzureRmRecoveryservicesBackupJob –Status $status
}
function Create-Vault {
New-AzureRmResourceGroup -Name $vaultrg -Location $Location –Confirm:$false -WarningAction SilentlyContinue -Force | Out-Null
New-AzureRmRecoveryServicesVault -Name $vaultname -ResourceGroupName $vaultrg -Location $location
$vault1 = Get-AzureRmRecoveryServicesVault –Name $vaultname
Set-AzureRmRecoveryServicesBackupProperties -Vault $vault1 -BackupStorageRedundancy GeoRedundant
}
function Get-Context {
param($vaultname = $vaultname)
Get-AzureRmRecoveryServicesVault -Name $vaultname | Set-AzureRmRecoveryServicesVaultContext
}
function Get-CurrentPolicies {
param(
$vaultname = $vaultname,
$wrkloadtype = $wrkloadtype
)
Get-AzureRmRecoveryServicesVault -Name $vaultname | Set-AzureRmRecoveryServicesVaultContext
$schPol = Get-AzureRmRecoveryServicesBackupSchedulePolicyObject -WorkloadType $wrkloadtype
$retPol = Get-AzureRmRecoveryServicesBackupRetentionPolicyObject -WorkloadType $wrkloadtype
}
function Create-Policy {
param(
$vaultname = $vaultname,
$policyname = $policyname,
$wrkloadtype = $wrkloadtype
)
Get-AzureRmRecoveryServicesVault -Name $vaultname | Set-AzureRmRecoveryServicesVaultContext
$schPol = Get-AzureRmRecoveryServicesBackupSchedulePolicyObject -WorkloadType $wrkloadtype
$retPol = Get-AzureRmRecoveryServicesBackupRetentionPolicyObject -WorkloadType $wrkloadtype
New-AzureRmRecoveryServicesBackupProtectionPolicy -Name $policyname -WorkloadType $wrkloadtype -RetentionPolicy $retPol -SchedulePolicy $schPol -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue
$script:pol = Get-AzureRmRecoveryServicesBackupProtectionPolicy -Name $policyname
}
function Modify-Policy {
param(
$vaultname = $vaultname,
$policyname = $policyname,
$wrkloadtype = $wrkloadtype
)
$retPol = Get-AzureRmRecoveryServicesBackupRetentionPolicyObject -WorkloadType "AzureVM"
$retPol.DailySchedule.DurationCountInDays = 365
$pol= Get-AzureRmRecoveryServicesBackupProtectionPolicy -Name $policyname
Set-AzureRmRecoveryServicesBackupProtectionPolicy -Policy $pol -RetentionPolicy $RetPol
}
function TriggerBackup-Vault {
param(
$vaultname = $vaultname,
$backupvmname = $backupvmname,
$containertype = "AzureVM"
)
Get-AzureRmRecoveryServicesVault -Name $vaultname | Set-AzureRmRecoveryServicesVaultContext
$namedContainer = Get-AzureRmRecoveryServicesBackupContainer -ContainerType $containertype -Status "Registered" -FriendlyName $backupvmname
$item = Get-AzureRmRecoveryServicesBackupItem -Container $namedContainer -WorkloadType "AzureVM"
$job = Backup-AzureRmRecoveryServicesBackupItem -Item $item
}
function AddVM-Vault {
param(
$vaultname = $vaultname,
$backupvmname = $backupvmname,
$containertype = "AzureVM",
$vaultrg = $backupvmrg,
$policyname = $policyname
)
Get-AzureRmRecoveryServicesVault -Name $vaultname | Set-AzureRmRecoveryServicesVaultContext
$script:pol = Get-AzureRmRecoveryServicesBackupProtectionPolicy -Name $policyname
Enable-AzureRmRecoveryServicesBackupProtection -Policy $script:pol -Name $backupvmname -ResourceGroupName $vaultrg
$namedContainer = Get-AzureRmRecoveryServicesBackupContainer -ContainerType $containertype -Status "Registered" -FriendlyName $backupvmname
$item = Get-AzureRmRecoveryServicesBackupItem -Container $namedContainer -WorkloadType "AzureVM"
$job = Backup-AzureRmRecoveryServicesBackupItem -Item $item
$job
}
Function Get-RestorePoint {
Get-AzureRmRecoveryServicesVault -Name $vaultname | Set-AzureRmRecoveryServicesVaultContext
$startDate = (Get-Date).AddDays(-7)
$endDate = Get-Date
$rp = Get-AzureRmRecoveryServicesBackupRecoveryPoint -Item $backupitem -StartDate $startdate.ToUniversalTime() -EndDate $enddate.ToUniversalTime()
$rp[0]
}
Function Write-Config {
param(
)
Write-Host " "
$time = " Start Time " + (Get-Date -UFormat "%d-%m-%Y %H:%M:%S")
Write-Host BACKUP/RESTORE CONFIGURATION - $time -ForegroundColor Cyan
Write-Host " "
Write-Host "Operation: $Action " -ForegroundColor White
Write-Host " "
}
Function Write-Completion {
param(
)
Write-Host " "
$time = " End Time " + (Get-Date -UFormat "%d-%m-%Y %H:%M:%S")
Write-Host BACKUP/RESTORE CONFIGURATION - $time -ForegroundColor Cyan
Write-Host " "
Write-Host "Completed operation: $Action " -ForegroundColor White
Write-Host " "
}
function Restore-VMVHD {
param(
$vaultname = $vaultname,
$storeagerg = $storeagerg,
$StorageName = $script:StorageNameVerified,
$backupvmname = $backupvmname
)
Get-AzureRmRecoveryServicesVault -Name $vaultname | Set-AzureRmRecoveryServicesVaultContext
$namedContainer = Get-AzureRmRecoveryServicesBackupContainer -ContainerType "AzureVM" –Status "Registered" -FriendlyName $backupvmname -WarningAction SilentlyContinue -InformationAction SilentlyContinue -ErrorAction Stop
$backupitem = Get-AzureRmRecoveryServicesBackupItem –Container $namedContainer –WorkloadType "AzureVM" -InformationAction SilentlyContinue -WarningAction SilentlyContinue -ErrorAction Stop
$startDate = (Get-Date).AddDays(-7)
$endDate = Get-Date
$rp = Get-AzureRmRecoveryServicesBackupRecoveryPoint -Item $backupitem -StartDate $startdate.ToUniversalTime() -EndDate $enddate.ToUniversalTime() -WarningAction SilentlyContinue -ErrorAction Stop -InformationAction SilentlyContinue
$rp[0]
Write-Host "Preparing Restore Job"
$restorejob = Restore-AzureRmRecoveryServicesBackupItem -RecoveryPoint $rp[0] -StorageAccountName $StorageName -StorageAccountResourceGroupName $storeagerg -WarningAction SilentlyContinue -InformationAction SilentlyContinue -ErrorAction Stop
Wait-AzureRmRecoveryServicesBackupJob -Job $restorejob -Timeout 43200 -WarningAction SilentlyContinue -InformationAction SilentlyContinue -ErrorAction Stop
Write-Host "Restore Job Running"
$script:restorejob = $restorejob
$restorejob = Get-AzureRmRecoveryServicesBackupJob -Job $script:restorejob -InformationAction SilentlyContinue -WarningAction SilentlyContinue -ErrorAction Stop
$JobDetails = Get-AzureRmRecoveryServicesBackupJobDetails -Job $restorejob -InformationAction SilentlyContinue -WarningAction SilentlyContinue -ErrorAction Stop
$properties = $JobDetails.properties
$properties
$storageAccountName = $properties["Target Storage Account Name"]
$containerName = $properties["Config Blob Container Name"]
$blobName = $properties["Config Blob Name"]
Write-Host $storageAccountName
Write-Host $blobName
Write-Host $containerName
Write-Host "Completed restore job"
Set-AzureRmCurrentStorageAccount -Name $storageaccountname -ResourceGroupName $storeagerg
$destination_path = $restorejson
Get-AzureStorageBlobContent -Container $containerName -Blob $blobName -Destination $destination_path -ErrorAction Stop -WarningAction SilentlyContinue -InformationAction SilentlyContinue -Force -Confirm:$false
$obj = ((Get-Content -Path $destination_path -Raw -Encoding Unicode)).TrimEnd([char]0x00) | ConvertFrom-Json
Write-Host "Exported json configuration file to $destination_path"
$vm = New-AzureRmVMConfig -VMSize $obj.'properties.hardwareProfile'.vmSize -VMName $createvmname -ErrorAction Stop -WarningAction SilentlyContinue -InformationAction SilentlyContinue
Set-AzureRmVMOSDisk -VM $vm -Name "osdisk" -VhdUri $obj.'properties.StorageProfile'.osDisk.vhd.Uri -CreateOption "Attach" -WarningAction SilentlyContinue -ErrorAction Stop -InformationAction SilentlyContinue
$vm.StorageProfile.OsDisk.OsType = $obj.'properties.StorageProfile'.OsDisk.OsType
foreach($dd in $obj.'properties.StorageProfile'.DataDisks)
{
$vm = Add-AzureRmVMDataDisk -VM $vm -Name "datadisk1" -VhdUri $dd.vhd.Uri -DiskSizeInGB 128 -Lun $dd.Lun -CreateOption "Attach"
}
Write-Host "Completed data disk configuration"
$pip = New-AzureRmPublicIpAddress -Name $InterfaceName1 -ResourceGroupName $createvmrg -Location $createvmlocation -AllocationMethod "Dynamic" –Confirm:$false -WarningAction SilentlyContinue -ErrorAction Stop -Force -InformationAction SilentlyContinue
Write-Host "Completed public ip creation"
$script:VNet = Get-AzureRMVirtualNetwork -Name $VNetName -ResourceGroupName $vnetrg | Set-AzureRmVirtualNetwork
$script:Interface1 = New-AzureRmNetworkInterface -Name $InterfaceName1 -ResourceGroupName $createvmrg -Location $createvmlocation -SubnetId $VNet.Subnets[$Subnet1].Id -PublicIpAddressId $pip.Id –Confirm:$false -WarningAction SilentlyContinue -ErrorAction Stop -EnableIPForwarding -Force
$script:VirtualMachine = Add-AzureRmVMNetworkInterface -VM $vm -Id $script:Interface1.Id -Primary -WarningAction SilentlyContinue -ErrorAction Stop -InformationAction SilentlyContinue
Write-Host "Completed vm prep"
}
Function Login-AddAzureRmProfile
{
Add-AzureRmAccount -WarningAction SilentlyContinue
}
Function Create-VM
{
Write-Host "Creating VM"
New-AzureRmVM -ResourceGroupName $createvmrg -Location $createvmlocation -VM $script:VirtualMachine
}
try {
Get-AzureRmResourceGroup -Location $Location -ErrorAction Stop | Out-Null
}
catch {
Write-Host -foregroundcolor Yellow `
"User has not authenticated, use Add-AzureRmAccount or $($_.Exception.Message)"; `
Login-AddAzureRmProfile
}
Reg-Provider
if($csvimport) {
try {
csv-run
}
catch {
Write-Host -foregroundcolor Yellow `
"$($_.Exception.Message)"; `
break
}
}
try {
Configure-Backup
}
catch {
Write-Host -foregroundcolor Yellow `
"$($_.Exception.Message)"; `
break
}
Error Message:
The below error is coming while I run from Run books automation account. provided runtime parameters to run the above script and left the value for CSVIMPORT parameter.
System.Management.Automation.ParameterBindingValidationException: Cannot bind argument to parameter 'Path' because it is null.
at System.Management.Automation.ParameterBinderController.BindPositionalParametersInSet(UInt32 validParameterSets, Dictionary`2 nextPositionalParameters, CommandParameterInternal argument, ParameterBindingFlags flags, ParameterBindingException& bindingException)
at System.Management.Automation.ParameterBinderController.BindPositionalParameters(Collection`1 unboundArguments, UInt32 validParameterSets, UInt32 defaultParameterSet, ParameterBindingException& outgoingBindingException)
at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParametersNoValidation(Collection`1 arguments)
at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParameters(Collection`1 arguments)
at System.Management.Automation.CommandProcessor.BindCommandLineParameters()
at System.Management.Automation.CommandProcessorBase.DoPrepare(IDictionary psDefaultParameterValues)
at System.Management.Automation.Internal.PipelineProcessor.Start(Boolean incomingStream)
at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
Environments Context
------------ -------
{[AzureCloud, AzureCloud], [AzureChinaCloud, AzureChinaCloud], [AzureUSGovernment, AzureUSGovernment]} Microsoft.Azur...
A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: Are you sure you want to register the provider 'Microsoft.RecoveryServices'

param (
[Parameter(Mandatory=$false)][String]$ResourceGroupName,
[Parameter(Mandatory=$false)][String]$ServicePrincipalName,
[Parameter(Mandatory=$false)][String]$ServicePrincipalPass,
[Parameter(Mandatory=$false)][String]$SubscriptionId,
[Parameter(Mandatory=$false)][String]$TenantId,
[Parameter(Mandatory=$false)][String]$VMname,
[Parameter(Mandatory=$false)][String]$vaultname
)
$targetVault = Get-AzRecoveryServicesVault -ResourceGroupName $ResourceGroupName -Name $vaultname
$targetVault.ID
Get-AzRecoveryServicesBackupProtectionPolicy -WorkloadType "AzureVM" -VaultId $targetVault.ID
Get-AzRecoveryServicesVault -Name $vaultname | Set-AzRecoveryServicesVaultContext
$policy = Get-AzRecoveryServicesBackupProtectionPolicy -Name $policyname
Enable-AzRecoveryServicesBackupProtection -ResourceGroupName $ResourceGroupName -Name $VMname -Policy $policy
$backupcontainer = Get-AzRecoveryServicesBackupContainer -ContainerType "AzureVM" -FriendlyName $VMname
$item = Get-AzRecoveryServicesBackupItem -Container $backupcontainer -WorkloadType "AzureVM"
$backupjob=Backup-AzRecoveryServicesBackupItem -Item $item
echo $backupjob
#$Backupstatus=Get-AzRecoveryservicesBackupJob
#Wait-AzRecoveryServicesBackupJob -Job $joblist[0] -Timeout 43200 -VaultId $targetVault.ID
Get-AzRecoveryservicesBackupJob
$namedContainer = Get-AzRecoveryServicesBackupContainer -ContainerType "AzureVM" -Status "Registered" -FriendlyName $VMname -VaultId $targetVault.ID
$backupitem = Get-AzRecoveryServicesBackupItem -Container $namedContainer -WorkloadType "AzureVM" -VaultId $targetVault.ID
echo "Please wait for backup to complete - Backup is in progress"
start-sleep -s 30
$joblist = Get-AzRecoveryservicesBackupJob –Status "InProgress" -VaultId $targetVault.ID
$joblist[0]
while (!$rp.ContainerName)
{
$startDate = (Get-Date).AddDays(-7)
$endDate = Get-Date
$rp = Get-AzRecoveryServicesBackupRecoveryPoint -Item $backupitem -StartDate $startdate.ToUniversalTime() -EndDate $enddate.ToUniversalTime() -VaultId $targetVault.ID
start-sleep -s 30
$rp[0]
Write-Host -NoNewline "Waiting 30 seconds for" $VMName "VM to backup"
}
Get-AzRecoveryservicesBackupJob
echo "Backup is completed successfully"

Related

Azure: Get resource for azure automation

I have runbook in Azure that i want to use in different RG,
My code
$vms = Get-AzVM -ResourceGroupName RG-TEST
foreach($vm in $vms)
{
$statuscheck = Get-AzVM -ResourceGroupName RG-TEST -Name $vm.Name -Status
if($statuscheck.Statuses.DisplayStatus[1] -eq "VM running")
{
Write-Output "Stopping virtual machine...$($vm.Name)"
Stop-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Force
}
else
{
Write-output "Virtual machine $($vm.Name) is already in stopped state"
}
}
How can I update the code that the script will get the name of the RG where it's located,
So that the RG is not hard coded
Dont know how to do it
Do something like this
$rgs = $(Get-AzVM)
# Get Distinct RG Names
$rgs = $rgs | Select-Object -Property ResourceGroupName | Sort-Object -Unique
foreach($rg in $rgs){
$vms = Get-AzVM -ResourceGroupName $rg.ResourceGroupName
foreach($vm in $vms)
{
$statuscheck = Get-AzVM -ResourceGroupName $rg.ResourceGroupName -Name $vm.Name -Status
if($statuscheck.Statuses.DisplayStatus[1] -eq "VM running")
{
Write-Output "Stopping virtual machine...$($vm.Name)"
Stop-AzVM -ResourceGroupName $rg.ResourceGroupName -Name $vm.Name -Force
}
else
{
Write-output "Virtual machine $($vm.Name) is already in stopped state"
}
}
}

Starting azure VM in parallel and validate after in PowerShell

Context : I am automating starting of 3 Azure VM through a PowerShell script, and each VM is taking 4-5min to start. I want to run the start command parallel and after 5-6min verify them whether they are started.
function restartLoadAgents ($AzuresecretValue,$AzureApplicationID,$AzureObjectID,$AzureDirectoryID,$AzureUserName,$AzureSubscriptionID) {
$password = ConvertTo-SecureString $AzuresecretValue -AsPlainText -Force;
$LoadAgentResourceGroup = "test-performance-rg01";
#Connecting to the Azure VM using the Service Principle
$pscredential = New-Object -TypeName System.Management.Automation.PSCredential($AzureApplicationID, $password);
Connect-AzAccount -ServicePrincipal -Tenant $AzureDirectoryID -Credential $pscredential | Out-null;
#List all the Load Agents
$VMList = Get-AzVm -ResourceGroupName $LoadAgentResourceGroup -Status;
ForEach($VM in $VMList) {
#Skipping the Master Machine and DB machine
if ($VM.Name -eq "test-load-appmachine01" -or $VM.Name -eq "test-load-appmachine02") {
continue;
}
$VMLoadAgentStatus = (Get-AzVm -ResourceGroupName $LoadAgentResourceGroup -Name $VM.Name -status).Statuses
$CurrentLoadAgentRunningStatus = $VMLoadAgentStatus[1].DisplayStatus;
if($CurrentLoadAgentRunningStatus -match "deallocated" -or $CurrentLoadAgentRunningStatus -match "VM deallocated"){
Start-AzVM -ResourceGroupName $LoadAgentResourceGroup -Name $VM.Name | Out-null
commandVerifier;
checkVMStatus($VM.Name);
}
else {
Write-Host $VM.Name " Current State is "$CurrentLoadAgentRunningStatus;
}
}
}
function commandVerifier() {
if ($?){
Write-Host "Successfully Started "$VM.Name;
}
else {
Write-Host "Start Unsuccessful "$VM.Name;
}
}
function checkVMStatus($VM_NAME) {
$VMLoadAgentStatus = (Get-AzVm -ResourceGroupName $LoadAgentResourceGroup -Name $$VM_NAME -status).Statuses
$VMRunningStatusAfterTriggered = $VMLoadAgentStatus[1].DisplayStatus;
if($VMRunningStatusAfterTriggered -eq "running" -or $VMRunningStatusAfterTriggered -eq "VM running"){
Write-Host "Successfully Started VM"
}
else{
Write-Host "Something went with starting VM and current status is"$VMRunningStatusAfterTriggered
}
}
function getServicePrincipleDetails () {
$AzuresecretValue = "<secretValue>";
$AzureApplicationID = "<app_id>";
$AzureObjectID = "<obj_id>";
$AzureDirectoryID = "<dir_id>";
$AzureUserName = "SVCUSER";
$AzureSubscriptionID = "<sub_id>";
restartLoadAgents $AzuresecretValue $AzureApplicationID $AzureObjectID $AzureDirectoryID $AzureUserName $AzureSubscriptionID
}
getServicePrincipleDetails
There are 5 VM in total and first two need not to be stopped or started. test-load-appmachine03,test-load-appmachine04 & test-load-appmachine05 are the target VM and I want to start them parallelly and check after.

Azure-DevOps Run powershell Getting Error while running

I have below command which is used for change vm size on azure:
Install-PackageProvider -name nuget -MinimumVersion 2.8.5.201 -Force
IF(!(Get-InstalledModule PSExcel -ErrorAction SilentlyContinue)){
Install-Module -Name PSExcel -ErrorAction SilentlyContinue -Force
Import-Module PSExcel -Force
}
IF(!(Get-InstalledModule ImportExcel -ErrorAction SilentlyContinue)){
Install-Module -Name ImportExcel -ErrorAction SilentlyContinue -Force
Import-Module ImportExcel -Force
}
IF(!(Get-InstalledModule az -ErrorAction SilentlyContinue)){
Install-Module -Name az -ErrorAction SilentlyContinue -Force
Import-Module az -Force
}
Import-Module PSExcel -Force
Import-Module ImportExcel -Force
$ExcelFile = "D:\test-data (3).xlsx"
$objExcel = New-Excel -Path $ExcelFile
$WorkBook = $objExcel | Get-Workbook
$NewExcelFile ="D:\test-data1.xlsx"
copy-item $ExcelFile -Destination $NewExcelFile
$sheetName = "Sheet2"
$newExcel = New-Object -ComObject Excel.Application
$newwb = $newExcel.Workbooks.Open($NewExcelFile)
$WorkSheet = $newwb.sheets.item($sheetName)
$WorksheetRange = $workSheet.UsedRange
$RowCount = $WorksheetRange.Rows.Count
$sheet=$newwb.Worksheets.Item(1)
$q=$RowCount
write-host 260
$q1="R"+$q
$r1 = $sheet.Range("R2:$q1")
$r1.cut()
$i1="I"+$q
$r2 = $sheet.Range("I2:$i1")
$sheet.Paste($r2)
$newwb.Close($true)
$newExcel.Quit()
#Import-Module az -Force
#Connect-AzAccount
Write-Host 273
#Loop through all items in the excel
ForEach($Worksheet in #($Workbook.Worksheets))
{
$totalNoOfRecords = $Worksheet.Dimension.Rows
$totalNoOfItems = $totalNoOfRecords - 1
# Declaring starting positions first row and column names
$rowNo, $Instance_Type_Current = 1, 9
$rowNo, $Instance_Type_Recommended = 1, 18
$rowNo, $Resource_Group= 1,42
$rowNo, $Resource_Name = 1,2
$rowNo, $osname = 1,8
if ($totalNoOfRecords -gt 1)
{
#Loop to get values from excel file
for ($i = 1; $i -le $totalNoOfRecords - 1; $i++)
{
$newvmsize = $WorkSheet.Cells.Item($rowNo + $i, $Instance_Type_Recommended).text
$vmName = $WorkSheet.Cells.Item($rowNo + $i, $Resource_Name).text
$resourceGroup = $WorkSheet.Cells.Item($rowNo + $i, $Resource_Group).text
$currentvmsize = $WorkSheet.Cells.Item($rowNo + $i, $Instance_Type_Current).text
$Checkwindowsos = $WorkSheet.Cells.Item($rowNo + $i, $osname).text
$currentsizefromazure = (Get-AzVM -ResourceGroupName $resourceGroup -VMName $vmName -ErrorAction SilentlyContinue).HardwareProfile.VmSize
#If only windows OS then VM size will update on azure
IF($Checkwindowsos -match 'windows'){
IF($currentvmsize -eq $currentsizefromazure) {
Stop-AzVM -ResourceGroupName $resourceGroup -Name $vmName -Force
$vm = Get-AzVM -ResourceGroupName $resourceGroup -VMName $vmName -ErrorAction SilentlyContinue
$vm.HardwareProfile.VmSize = $newvmsize
Write-Host 304
$vmstatus = $null
while($vmstatus -notmatch 'deallocated'){
$vmstatus=((get-azvm -Name $VmName -ResourceGroupName $resourceGroup -Status).Statuses[1]).code
}
#Start VM Snapshot(Backup)
$vm1 = get-azvm -Name $VmName -ResourceGroupName $resourceGroup
write-host $VmName
$snapshotdisk = $vm1.StorageProfile
$vmlocation =$vm1.location
#backup OS DISK
$OSDiskSnapshotConfig = New-AzSnapshotConfig -SourceUri $snapshotdisk.OsDisk.ManagedDisk.id -CreateOption Copy -Location $vmlocation -OsType Windows
$snapshotNameOS = "$($snapshotdisk.OsDisk.Name)_snapshot_$(Get-Date -Format ddMMyy)"
New-AzSnapshot -ResourceGroupName $resourceGroup -SnapshotName $snapshotNameOS -Snapshot $OSDiskSnapshotConfig
Write-Output "VM-Name $($vm1.name) OS Disk Snapshot End & Snapshot name is $snapshotNameOS"
Write-Host 319
#backup Data DISK
$dataDisks = ($snapshotdisk.DataDisks).name
IF($dataDisks -ne $null){
foreach ($datadisk in $datadisks) {
$dataDisk = Get-AzDisk -ResourceGroupName $vm1.ResourceGroupName -DiskName $datadisk
Write-Output "VM $($vm1.name) data Disk Name $($datadisk.Name) Snapshot Begin"
$DataDiskSnapshotConfig = New-AzSnapshotConfig -SourceUri $dataDisk.Id -CreateOption Copy -Location $vmlocation
$snapshotNameData = "$($datadisk.name)_snapshot_$(Get-Date -Format ddMMyy)"
New-AzSnapshot -ResourceGroupName $resourceGroup -SnapshotName $snapshotNameData -Snapshot $DataDiskSnapshotConfig
Write-Output "VM $($vm1.name) data Disk $($datadisk.Name) Snapshot End"
}}
Update-AzVM -VM $vm -ResourceGroupName $resourceGroup -ErrorAction SilentlyContinue
Start-AzVM -ResourceGroupName $resourceGroup -Name $vmName -ErrorAction SilentlyContinue
$vmstatus = $null
while($vmstatus -notmatch 'running'){
$vmstatus=((get-azvm -Name $VmName -ResourceGroupName $resourceGroup -Status).Statuses[1]).code
}
Write-Host Your VM name $vmName is started and changed size from $currentvmsize changed to $newvmsize
}
elseif($currentsizefromazure -eq $newvmsize) { Write-host Your VM $vmName is already Upgraded}
else{Write-host VM current size is mismatched from azure size}
}
}
}
}
Giving below error while running this script on azure devops:
Please help to resolve error.
Unable to get the Open property of the Workbooks class
At C:\Users\azureadmin\Downloads\vsts-agent-win-x64-2.195.2\_work\1\s\Automatic VM backup.ps1:28 char:1
+ $newwb = $newExcel.Workbooks.Open($NewExcelFile)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
Note: Script is running on self hosted agent and this script is running when normal ISE also azconnection through devops azure
p.s. This script is change vm size and get backup

Backup to azure Blob showing completed while backup still in progress

I am using azure automation to automate the process to backup my database to azure blob storage, its working all fine but the issue i am trying to resolve is that the status shows completed before the actual backup is done, and if i try to rerun the automation it tells me this :
"ErrorActionPreference" or common parameter is set to Stop: 45183: There is an import or export operation in progress on the database 'database'.
Here is my powershell script:
param(
[parameter(Mandatory=$true)]
[String] $ResourceGroupName,
[parameter(Mandatory=$true)]
[String] $DatabaseServerName,
[parameter(Mandatory=$true)]
[String]$DatabaseAdminUsername,
[parameter(Mandatory=$true)]
[String]$DatabaseAdminPassword,
[parameter(Mandatory=$true)]
[String]$DatabaseNames,
[parameter(Mandatory=$true)]
[String]$StorageAccountName,
[parameter(Mandatory=$true)]
[String]$BlobStorageEndpoint,
[parameter(Mandatory=$true)]
[String]$StorageKey,
[parameter(Mandatory=$true)]
[string]$BlobContainerName
# [parameter(Mandatory=$true)]
# [Int32]$RetentionDays
)
$ErrorActionPreference = 'stop'
function Login() {
$connectionName = "AzureRunAsConnection"
try
{
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
Write-Verbose "Logging in to Azure..." -Verbose
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint | Out-Null
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
}
function Create-Blob-Container([string]$blobContainerName, $storageContext) {
Write-Verbose "Checking if blob container '$blobContainerName' already exists" -Verbose
if (Get-AzureStorageContainer -ErrorAction "Stop" -Context $storageContext | Where-Object { $_.Name -eq $blobContainerName }) {
Write-Verbose "Container '$blobContainerName' already exists" -Verbose
} else {
New-AzureStorageContainer -ErrorAction "Stop" -Name $blobContainerName -Permission Off -Context $storageContext
Write-Verbose "Container '$blobContainerName' created" -Verbose
}
}
function Export-To-Blob-Storage([string]$resourceGroupName, [string]$databaseServerName, [string]$databaseAdminUsername, [string]$databaseAdminPassword, [string[]]$databaseNames, [string]$storageKey, [string]$blobStorageEndpoint, [string]$blobContainerName) {
Write-Verbose "Starting database export to databases '$databaseNames'" -Verbose
$securePassword = ConvertTo-SecureString –String $databaseAdminPassword –AsPlainText -Force
$creds = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $databaseAdminUsername, $securePassword
foreach ($databaseName in $databaseNames.Split(",").Trim()) {
Write-Output "Creating request to backup database '$databaseName'"
$bacpacFilename =$databaseName + "LiveBak_anon" + ".bacpac"
$bacpacUri = $blobStorageEndpoint + "/" + $blobContainerName + "/" + $bacpacFilename
$exportRequest = New-AzureRmSqlDatabaseExport -ResourceGroupName $resourceGroupName –ServerName $databaseServerName `
–DatabaseName $databaseName –StorageKeytype "StorageAccessKey" –storageKey $storageKey -StorageUri $BacpacUri `
–AdministratorLogin $creds.UserName –AdministratorLoginPassword $creds.Password -ErrorAction "Stop"
# Print status of the export
# Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $exportRequest.OperationStatusLink -ErrorAction "Stop"
}
}
# function Delete-Old-Backups([int]$retentionDays, [string]$blobContainerName, $storageContext) {
# Write-Output "Removing backups older than '$retentionDays' days from blob: '$blobContainerName'"
# $isOldDate = [DateTime]::UtcNow.AddDays(-$retentionDays)
# $blobs = Get-AzureStorageBlob -Container $blobContainerName -Context $storageContext
# foreach ($blob in ($blobs | Where-Object { $_.LastModified.UtcDateTime -lt $isOldDate -and $_.BlobType -eq "BlockBlob" })) {
# Write-Verbose ("Removing blob: " + $blob.Name) -Verbose
# Remove-AzureStorageBlob -Blob $blob.Name -Container $blobContainerName -Context $storageContext
# }
# }
Write-Verbose "Starting database backup" -Verbose
$StorageContext = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageKey
Login
Create-Blob-Container `
-blobContainerName $blobContainerName `
-storageContext $storageContext
Export-To-Blob-Storage `
-resourceGroupName $ResourceGroupName `
-databaseServerName $DatabaseServerName `
-databaseAdminUsername $DatabaseAdminUsername `
-databaseAdminPassword $DatabaseAdminPassword `
-databaseNames $DatabaseNames `
-storageKey $StorageKey `
-blobStorageEndpoint $BlobStorageEndpoint `
-blobContainerName $BlobContainerName
# Delete-Old-Backups `
# -retentionDays $RetentionDays `
# -storageContext $StorageContext `
# -blobContainerName $BlobContainerName
Write-Verbose "Database backup script finished" -Verbose
All i basically need is for this process to show running even while the backup operation is in progress because i am using this automation in a logic app.
You could use Get-AzureRmSqlDatabaseImportExportStatus
Sample :
PS C:\>Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink "https://management.contoso.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resource01/providers/Microsoft.Sql/servers/server01/databases/database01/importExportOperationResults/00000000-000-0000-0000-000000000000?api-version=2014-04-01"
OperationStatusLink :
ErrorMessage :
LastModifiedTime : 4/15/2016 10:16:14 PM
QueuedTime : 4/15/2016 10:16:13 PM
StatusMessage : Running, Progress = 5.00 %
Status : InProgress
When you run New-AzureRmSqlDatabaseExport, A job is submitted however it is never waited for the completion of the job. The subsequent lines gets executed.
In your code, you have mentioned the below line
Write-Verbose "Database backup script finished" -Verbose
Once the export job is submitted, the above line is executed.
To overcome this, you could use the Get-AzureRmSqlDatabaseImportExportStatus and poll the status, proceed on the completion status.

Azure PowerShell Runbook Get Set Az-Disk dynamically

The team is trying to automate a snapshot restore, which was achieved successfully. However I am not able to figure out how to dynamically get the previous disk within the resource group; As well as set the next disk with a new name.
In the code below "AZR-001_OsDisk_7" has to set dynamically to "AZR-001_OsDisk_8" the next time it runs:
$diskConfig = New-AzDiskConfig -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy
$disk = New-AzDisk -Disk $diskConfig -ResourceGroupName "ETD-RFT" -DiskName "AZR-001_OsDisk_7"
$disk1 = Get-AzDisk -ResourceGroupName "ETD-RFT" -Name "AZR-001_OsDisk_7"
Not a final solution, but I have a quick idea. You may use Get-AzDisk -ResourceGroupName 'ResourceGroupName ' to get all the disks. And then you can get the disk name.
As you named the disk with appropriate rule, you may split the name string by _, then you will get a string array which contains all the parts. In this way, you will be able to get the version.
A sample:
$disks = Get-AzDisk -ResourceGroupName JackVS2019
foreach($disk in $disks){
$arr = $disk.Name.Split('_')
Write-Host $arr[2]
}
The output:
1
Then you can generate the new disk name.
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
$account = Connect-AzAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
Select-AzSubscription -SubscriptionName 101-Prototyping
$vm = Get-AzVM -ResourceGroupName "ETD-RFT" -Name "AZR-101-007-001"
$snapshot = Get-AzSnapshot -ResourceGroupName "ETD-RFT" -SnapshotName "SNAPSHOT-DEC-2019"
$diskConfig = New-AzDiskConfig -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy
$disks = Get-AzDisk -ResourceGroupName "ETD-RFT"
$attached = $disks | Where-Object ManagedBy -ne $null
foreach($disk in $attached)
{
$arr = $disk.Name.Split('_')
$arr[2]
}
$a = $arr[2] -as [int]
$a=$a+1
Write-Host $a
$newname = $arr[0] + "_" + $arr[1] + "_" + $a
$disknew = New-AzDisk -Disk $diskConfig -ResourceGroupName "ETD-RFT" -DiskName $newname
$disk1 = Get-AzDisk -ResourceGroupName "ETD-RFT" -Name $newname
Set-AzVMOSDisk -VM $vm -ManagedDiskId $disk1.Id -Name $disk1.Name
Update-AzVM -ResourceGroupName "ETD-RFT" -VM $vm

Resources