lrakai / automate-azure-file-sync

Demonstrate how to automate Azure File Sync deployments using PowerShell

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

automate-azure-file-sync

Demonstrate how to automate Azure File Sync deployments using PowerShell. This Lab automates the tasks performed manually in Azure Portal and over RDP on the Windows Server in this Lab.

Final Environment

Getting Started

An Azure RM template is included in infrastructure/ to create the environment:

Using Azure PowerShell, do the following to provision the resources:

.\New-Lab.ps1

Alternatively, you can perform a one-click deploy with the following button:

Following Along

  1. Start an Azure Cloud Shell PowerShell.

  2. Create a file share:

    $fileShareName = "sync"
    $resourceGroupName = Get-AzResourceGroup | select -ExpandProperty ResourceGroupName
    $storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName | `
                  where StorageAccountName -like calabsync*
    New-AzureStorageShare -Context $storageAccount.Context -Name $fileShareName
  3. Open the Cloud Shell editor:

    code .
  4. Paste in the src/Install-AfsAgent.ps1 script for installing the Azure File Sync Agent on the VM.

  5. Save the script as Install-AfsAgent.ps1.

  6. Use the VM Custom Script Extension to install the agent on the VM:

    # Create a blog storage container that permits anonymous access to invidividual blobs
    $containerName = "deploy-afs"
    New-AzureStorageContainer -Name $containerName -Context $storageAccount.Context -Permission blob
    
    # Upload the Install-Afs.ps1 script to the blob container
    Set-AzureStorageBlobContent -File "Install-AfsAgent.ps1" `
        -Container $containerName `
        -Context $storageAccount.Context 
    
    $fileUri = "$($storageAccount.Context.BlobEndPoint)$containerName/Install-AfsAgent.ps1"
    $Settings = @{
        "fileUris" = @($fileUri)
    }
    $ProtectedSettings = @{
        "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File Install-AfsAgent.ps1"
    }
    Set-AzVMExtension -ResourceGroupName $resourceGroupName `
        -VMName $vm.Name `
        -Location $vm.Location `
        -Publisher "Microsoft.Compute" `
        -ExtensionType "CustomScriptExtension" `
        -TypeHandlerVersion "1.9" `
        -Settings $Settings `
        -ProtectedSettings $ProtectedSettings `
        -Name provision
  7. Create a sync group by running the src/Deploy-Afs.ps1 script in the same fashion.

Tearing Down

When finished, first delete the server endpoint, cloud endpoint, sync group, and registered server in Storage Sync Service (These resources cannot be deleted by deleting the resource group) and finally remove the remaining Azure resources with:

.\Remove-Lab.ps1

About

Demonstrate how to automate Azure File Sync deployments using PowerShell

License:MIT License


Languages

Language:PowerShell 100.0%