Azure File Share Snapshot

Richard Loong 0 Reputation points
2023-03-28T09:44:19.56+00:00

I am new to Azure, we have an Azure file share in the China region, but unfortunately the China region does not provide the function of Azure file backup. So I can only find some Powershell code from the web to produce snapshots. Below is the code, but when test run it keeps saying "does not contain a method named 'Snapshot'."

System.Management.Automation.RuntimeException: Method invocation failed because [Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageFileShare] does not contain a method named 'Snapshot'.

Can someone help me fix this? I wish you a happy day!

Param(
 [string]$KeyVaultName,
 [string]$SecretName,
 [string]$StorageAccountName,
 [string]$FileShareName
)

# Authenticate with your Automation Account
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Add-AzAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationID $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint -Environment AzureChinaCloud

#Create Snapshot
$keyvaultsecret = (Get-AzKeyVaultSecret -vaultName $KeyVaultName -name $SecretName -AsPlainText)
$context = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $keyvaultsecret 
$share = Get-AzStorageShare -Context $context -Name $FileShareName
$share.Snapshot()

# Generate Snapshot List
$SnapshotList = Get-AzStorageShare -Context $context | Where-Object {$_.Name -eq $ShareName -and $_.IsSnapshot -eq $true -and $_.SnapshotTime -lt ([datetime]::UtcNow.AddDays(-4))}

# Delete Snapshots older than 4 days
foreach ($Snapshot in $SnapshotList)
    {
        Remove-AzStorageShare -Share $Snapshot -Verbose -Force
    }
Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,274 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sedat SALMAN 13,740 Reputation points
    2023-03-28T10:04:05.7966667+00:00

    can you check the following

    
    Param(
     [string]$KeyVaultName,
     [string]$SecretName,
     [string]$StorageAccountName,
     [string]$FileShareName
    )
    
    # Authenticate with your Automation Account
    $Conn = Get-AutomationConnection -Name AzureRunAsConnection
    Add-AzAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationID $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint -Environment AzureChinaCloud
    
    #Create Snapshot
    $keyvaultsecret = (Get-AzKeyVaultSecret -vaultName $KeyVaultName -name $SecretName -AsPlainText)
    $context = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $keyvaultsecret 
    $share = Get-AzStorageShare -Context $context -Name $FileShareName
    $shareObj = [Microsoft.Azure.Commands.Management.Storage.Models.PSFileShare]::new($share.ICloudFileShare)
    $shareObj.Snapshot()
    
    # Generate Snapshot List
    $SnapshotList = Get-AzStorageShare -Context $context | Where-Object {$_.Name -eq $FileShareName -and $_.IsSnapshot -eq $true -and $_.SnapshotTime -lt ([datetime]::UtcNow.AddDays(-4))}
    
    # Delete Snapshots older than 4 days
    foreach ($Snapshot in $SnapshotList)
        {
            Remove-AzStorageShare -Share $Snapshot -Verbose -Force
        }
    
    

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.