Search code examples
azure-resource-managerazure-rm-templateazure-storage-account

How do I deploy file share within storage account using ARM template


I can successfully create a storage account using an ARM template and I realize the ARM template does not directly support creating a file share on the storage account via any of the existing providers. I thought I would write a PowerShell script and use the custom script extension in the arm template but it seems like that can only run on a VM (typically used for post setup stuff on VM).

Is there a way to create the file share and child directory structure in PowerShell and have this executed after my ARM template is deployed?


Solution

  • you can use the following powershell:

    Create share

    $storageAcct = Get-AzStorageAccount -ResourceGroupName xxx -Name yyy
    New-AzStorageShare `
       -Name myshare `
       -Context $storageAcct.Context
    

    Create folder.

    New-AzStorageDirectory `
       -Context $storageAcct.Context `
       -ShareName "myshare" `
       -Path "myDirectory"
    

    Upload file.

    # this expression will put the current date and time into a new file on your scratch drive
    Get-Date | Out-File -FilePath "C:\Users\ContainerAdministrator\CloudDrive\SampleUpload.txt" -Force
    
    # this expression will upload that newly created file to your Azure file share
    Set-AzStorageFileContent `
       -Context $storageAcct.Context `
       -ShareName "myshare" `
       -Source "C:\Users\ContainerAdministrator\CloudDrive\SampleUpload.txt" `
       -Path "myDirectory\SampleUpload.txt"
    

    Source: https://learn.microsoft.com/en-us/azure/storage/files/storage-how-to-use-files-powershell