I'm building Azure runbook (powershell) which check if vm has backup enabled if it's not, it will enable it. I have problem with adding and building IF statement to make it better.
This is how I do it now and it works, but If vm has backup enabled, runbook will print a lot of red and thats not good. This is part of bigger runbook and those all are running inside on foreach.
$vault= Get-AzureRmRecoveryServicesVault -ResourceGroupName "RGName" -Name "VaultName"
Set-AzureRmRecoveryServicesVaultContext -Vault $vault
Write-Output "Configuring Azure backup to $($vm.Name)"
$policy = Get-AzureRmRecoveryServicesBackupProtectionPolicy -Name "PolicyName"
Enable-AzureRmRecoveryServicesBackupProtection `
-ResourceGroupName $vm.ResourceGroupName `
-Name $vm.Name `
-Policy $policy
Then I wanted to add IF statement there so if backup has been enable on vm, it would just skip it. The below command will print the results of backup ( true or false) but I don't know how to implement that into if statement so if results would be false, it would run the script block and if result would be true, it would just skip and print out $vm.Name has already configured to Azure backup.
Get-AzRecoveryServicesBackupStatus -Name 'VmName' -ResourceGroupName 'RgName' -Type AzureVM
if ()
{
$vault= Get-AzureRmRecoveryServicesVault -ResourceGroupName "RgName" -Name "VaultName"
Set-AzureRmRecoveryServicesVaultContext -Vault $vault
Write-Output "Configuring Azure backup to $($vm.Name)"
$policy = Get-AzureRmRecoveryServicesBackupProtectionPolicy -Name "PolicyName"
Enable-AzureRmRecoveryServicesBackupProtection `
-ResourceGroupName $vm.ResourceGroupName `
-Name $vm.Name `
-Policy $policy
}
else {
Write-Output "$vm.Name has already configured to Azure backup"
}
So any tips how to do it? Can I do it somehow like this: if (Get-AzRecoveryServicesBackupStatus -Name 'vmanme' -ResourceGroupName 'rgname' -Type AzureVM backedup -match false) ?