'I am trying to write a script that will backup Azure Files for each storage account that starts with a certain letter

function Set-StorageAccounttoPolicy {

$storageaccounts= Get-AzStorageAccount | where {$_.StorageAccountName.StartsWith('p')}
Get-AzRecoveryServicesVault -Name xyztestvault | Set-AzRecoveryServicesVaultContext
$policy=Get-AzRecoveryServicesBackupProtectionPolicy  -Name testpolicy 

foreach($storage in $storageaccounts) {
    $storagename= $storage.StorageAccountName
    $resourcegroup= $storage.ResourceGroupName 

if($storage.PrimaryEndpoints.File -ne $null) 
{
    $fileshares= Get-AzRmStorageShare -ResourceGroupName $resourcegroup -StorageAccountName 
     $storagename
    foreach($file in $fileshares)
    {                 
          Enable-AzRecoveryServicesBackupProtection -StorageAccountName $storagename -Name 
         $file.Name -Policy $policy                 
     }
   }

  }
}

I keep getting an error "Enable-AzRecoveryServicesBackupProtection : 'containerName' cannot be null.", but this storage account has not been assigned to a recovery vault or policy yet. How can I fix this?



Solution 1:[1]

Have you double checked the $storagename variable to just make sure it isn't actually null? Also, double check to see if the current storage account/container is tied to a RSV. Because, if it is, it won't allow you to run the Enable-AzRecoveryServicesBackupProtection cmdlet.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kooleosis