Azure File Sync is a fairly new service that allows users to sync on-premises files to Azure Storage. Think of this service as DropBox for business. Now, we can simply drop files into a folder on an on-prem server and automatically upload those files to Azure!
But first, let’s make sure you’ve got all of the prerequisites. You will need the following:
- An Azure Storage account
- A supported operating system. We’ll be using Windows Server 2016.
- PowerShell 5.1
- The AzureRM PowerShell module installed on the server
With that out of the way, let’s go through the steps to get Azure File Sync set up for the first time.
Step 1. Disable IE Enhanced Security Configuration
The first step is to disable IE Enhanced security. This is required for the initial registration and can be enabled once it’s set up. We could do this via the GUI but what fun is that? Let’s do it with a simple PowerShell script!
$AdminKey = 'HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}'
$UserKey = 'HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}'
Set-ItemProperty -Path $AdminKey -Name 'IsInstalled' -Value 0
Set-ItemProperty -Path $UserKey -Name 'IsInstalled' -Value 0
Stop-Process -Name Explorer
Step 2. Create an Azure File Share
Again, we could do this via the Azure Portal, but it’s a lot easier just to run a few lines of code instead of clicking through a webpage. For this code to run, you’ll need to know the storage account name you’ll be syncing files to and the resource group name it is in. My storage account is called techsnips, and my resource group is called TechSnipsBackEnd.
Once I have the storage account information in a variable, I’ll then create the file share and call it ipswitchfileshare as shown below ensuring the name is all lowercase.
$resourceGroup = 'TechSnipsBackEnd'
$storageAccountName = 'techsnips'
$storageAccount = Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
$storageKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $storageAccount.ResourceGroupName -Name $storageAccount.StorageAccountName | select -first 1).Value
$storageContext = New-AzureStorageContext -StorageAccountName $storageAccount.StorageAccountName -StorageAccountKey $storageKey
$share = New-AzureStorageShare -Name ipswitchfileshare -Context $storageContext
Step 3. Install the agent
Next up, we need to install the agent on the server. Again, let’s take a shortcut by downloading and installing it while we’re in the PowerShell console. Below you can see that I’m downloading the installer from Microsoft and then calling it to bring up the installation wizard.
$downloadUri = 'https://download.microsoft.com/download/1/8/D/18DC8184-E7E2-45EF-823F-F8A36B9FF240/StorageSyncAgent_V3_WS2016.EXE'
Invoke-WebRequest -Uri $downloadUri -OutFile 'C:\filesyncagent.exe'
Invoke-Item 'C:\filesyncagent.exe'
The code above will bring up the installer wizard. Go ahead and walk through all of the steps and take the defaults. At the end, the wizard will ask you to close any PowerShell consoles open. Once you do that, click Install and it will begin installing.
Step 4. Deploy the Storage Sync Service
Next up, we need to deploy a storage sync service. We can again use PowerShell to make this happen. To do this, however, you’ll need to know the name of your Azure subscription if you have more than one. This can be found in the Azure portal.
You will also need to know the region that you’d like to deploy the service in and the resource group. For our purposes, I’ll be using the ‘East US’ region and the SharedLab resource group.
$agentPath = 'C:\Program Files\Azure\StorageSyncAgent'
$region = 'East US 2' ## This needs to be in the same region as the storage account
$resourceGroup = 'SharedLab'
$storageSyncName = 'TechSnipsStorageSync'
Import-Module "$agentPath\StorageSync.Management.PowerShell.Cmdlets.dll"
$subscription = Get-AzureRmSubscription -SubscriptionName 'TechSnips'
Login-AzureRmStorageSync –SubscriptionId $subscription.Id -ResourceGroupName $resourceGroup -TenantId $subscription.TenantID -Location $region
New-AzureRmStorageSyncService -StorageSyncServiceName $storageSyncName
Step 5. Register Windows Server with the Storage Sync Service
Once we’ve got the storage sync service created, we now have to register the server with the storage sync server. We can do this using the Register-AzureRmStorageSyncServer command below.
$registeredServer = Register-AzureRmStorageSyncServer -StorageSyncServiceName $storageSyncName
Step 6. Create a sync group
The next step is to create a sync group. A sync group defines the sync topology for a set of files. Endpoints within a sync group are kept in sync with each other. We create this sync group using the New-AzureRmStorageSyncGroup command as shown below.
$syncGroupName = 'TechSnipsSyncGroup'
New-AzureRmStorageSyncGroup -SyncGroupName $syncGroupName -StorageSyncService $storageSyncName
Step 7. Creating a Cloud Endpoint
All sync groups require a cloud endpoint so we must get this created next. We can again use PowerShell to do this by using the New-AzureRmStorageSyncCloudEndpoint command. To make this command work, we’ll need re-use the names of the resources we’ve created earlier.
$parameters = @{
StorageSyncServiceName = $storageSyncName
SyncGroupName = $syncGroupName
StorageAccountResourceId = $storageAccount.Id
StorageAccountShareName = 'ipswitchfileshare'
}
New-AzureRmStorageSyncCloudEndpoint @parameters
Step 8. Create the Server Endpoint
Now that we have the cloud endpoint created, we then need to create the server endpoint. This is the resource that specifies the location on the registered server where you’d like to sync files from. Below I’m using all of the same variables that we’ve been created to create the server endpoint, but now we’re specifying the folder path that we’d like to sync the files from as the ServerLocalPath parameter.
New-AzureRmStorageSyncServerEndpoint -StorageSyncServiceName $storageSyncName -SyncGroupName $syncGroupName -ServerId $registeredServer.Id -ServerLocalPath 'C:\FileSyncDemo'
At this point, if you place any files into the ‘C:’ folder, they will automatically be synced to your Azure storage account!
Adam Bertram
Adam Bertram is a 25+ year IT veteran and an experienced online business professional. He’s a successful blogger, consultant, 6x Microsoft MVP, trainer, published author and freelance writer for dozens of publications. For how-to tech tutorials, catch up with Adam at adamtheautomator.com, connect on LinkedIn or follow him on X at @adbertram.