Azure Automation runbook to manage snapshot file share

Andrea 256 Reputation points
2024-09-19T08:46:15.0733333+00:00

Hello guys,
I created a runbook in powershell that can create daily snapshots for File Share on Storage Account, setting a retention of 3 days. The list of StorageAccounts is written in a JSON file placed in a container accessible directly via REST API with a URL with related SAS (which may change over time).

Inside the JSON file there is this:

[

{

"resourceGroupName": "DefaultResourceGroup",

"storageAccountName": "sastandard",

"fileShareName": "fsnew"

}

]

Then the script should read the JSON file to determine which fileshares to create and manage snapshot retention.

After several attempts I continue to have several errors.

The script is this:

# Set variable with URL and SAS token
$saListUrl = "https://....."

# Get JSON file with Invoke-RestMethod
try {
    $storageAccountsList = Invoke-RestMethod -Uri $saListUrl
} catch {
    Write-Error "Is not possible download the file JSON: $_"
    exit
}

$retentionDays = 7

# Loop file JSON
foreach ($storageAccount in $storageAccountsList) {
    $resourceGroupName = $storageAccount.resourceGroupName
    $storageAccountName = $storageAccount.storageAccountName
    $fileShareName = $storageAccount.fileShareName

    # Create snapshot
    try {
        Write-Host "Creating snapshot File Share: ${fileShareName} for the Storage Account: ${storageAccountName}"
#$snapshot = New-AzStorageShareSnapshot -ResourceGroupName $resourceGroupName -StorageAccountName $storageAccountName -ShareName $fileShareName
    $snapshot = New-AzRmStorageShare -ResourceGroupName $resourceGroupName -StorageAccountName $storageAccountName -ShareName $fileShareName
        Write-Host "Snapshot created with success!"
    } catch {
        Write-Error "Error creating ${fileShareName}: $_"
        continue
    }

    # Get snapshot and manage retention
    try {
        Write-Host "Manage retention for the File Share: ${fileShareName}"
        # Get file share and their all snapshot
        $fileShare = Get-AzRmStorageShare -ResourceGroupName $resourceGroupName -StorageAccountName    $storageAccountName -ShareName $fileShareName -IncludeSnapshot
        $snapshots = $fileShare.Snapshots

        foreach ($snapshot in $snapshots) {
            $snapshotTime = Get-Date $snapshot.SnapshotTime
            if ((Get-Date) - $snapshotTime -gt (New-TimeSpan -Days $retentionDays)) {
                Write-Host "Delete snapshot $snapshotTime for the File Share ${fileShareName}"
                # Delete snapshot
                Remove-AzStorageShare -ResourceGroupName $resourceGroupName -StorageAccountName $storageAccountName -ShareName $fileShareName -SnapshotTime $snapshotTime -Force
                Write-Host "Snapshot deleted!"
            }
        }
    } catch {
        Write-Error "Error creating retention ${fileShareName}: $_"
    }
}


the output error is:

31 | … $snapshot = New-AzRmStorageShare -ResourceGroupName $resourceGroupNam …

 |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 | No subscription found in the context.  Please ensure that the

 | credentials you provided are authorized to access an Azure subscription,

 | then run Connect-AzAccount to login.

Write-Error: A parameter cannot be found that matches parameter name 'ResourceGroupName'.

Someone can help me to find a method to manage retention of snapshot filehare ?

thanks

Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,224 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,257 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Nehruji R 8,146 Reputation points Microsoft Vendor
    2024-09-19T13:34:22.72+00:00

    Hello Andrea,

    Greetings! Welcome to Microsoft Q&A Forum.

    I understand that you are encountering an issue with your Azure Automation runbook in PowerShell. As per the error message no subscription was found in the current session of your PowerShell function.

    "No subscription found in the context. Please ensure that the credentials you provided are authorized to access an Azure subscription, then run Connect-AzAccount to login"

    Ensure you have met the Prerequisites:

    · Install the Azure PowerShell module in your Azure Automation account.

    · Assign necessary roles (e.g., Storage Blob Data Contributor) to the managed identity or service principal.

    Reference Links:

    Most likely the credentials that you are using to login to Azure and use Get-AzureRmVm have expired, no longer valid or no longer available. If you are using the default credentials when Azure Automation account is created in the Automation account check Run as accounts tabs.

    Please do let us know if you have any further queries. I’m happy to assist you further.

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.