PowerShell for Azure File Sync

While deploying Azure File Sync for a customer I’ve come across some useful PowerShell commands. Some are directly from Microsoft while others have been created by me to handle scenarios that may not be customer specific.

View file sync error report

When Azure File Sync is installed it includes tools to investigate the reason for tiles not syncing. If the portal states that there are files not being synced connect to the affected server endpoint and run the following PowerShell-cmdlets.

Set-Location $env:ProgramFiles\Azure\StorageSyncAgent
.\FileSyncErrorsReport.ps1

This will run the built in sync error report for all sync groups on the server. If you’d prepare to export the errors to a CSV-file just add a switch as below.

Set-Location $env:ProgramFiles\Azure\StorageSyncAgent
.\FileSyncErrorsReport.ps1 -CsvPath C:\temp\SyncErrors.csv

Resolving files with invalid characters

Since writing this article Microsoft have published a script to handle invalid characters in folder and file names, check it out on GitHub.

The major issue I encountered during the migration was files not syncing due to invalid characters in the file names returned in the report above as error code 0x8007007b. While the root cause is unknown this script will quickly remove the illegal characters from the file names.

$BasePath = "D:\Data" #Path where files with illegal characters are located

#Find all files in the path containing the illegal characted
$Items = Get-ChildItem $BasePath -Name *�*  -Recurse

#Rename the files replacing the illegal character
$Items | foreach {

$Path = $BasePath + $_

$Filename = $Path -split '\',-1,'SimpleMatch'
$Filename = $Filename[$Filename.Count-1]
$Filename = $Filename -replace "�",""

Rename-Item -Path $Path -NewName $Filename

}

If the files still appear in the sync error report a few minutes later I found that restarting the “Storage Sync Agent”-service helped.

Download all tiered files

This is primarily useful if you’re disabling tiering on a server or removing Azure File Sync for a specific share or all together.

Using the Azure Portal, disable tiering for the server endpoint. Connect to the server and run the following PowerShell command to download all files to the local server.

$Path = "D:\Data" #Local path of the files to download

Set-Location $env:ProgramFiles\Azure\StorageSyncAgent
Import-Module ".\StorageSync.Management.ServerCmdlets.dll"
Invoke-StorageSyncFileRecall -Path $Path

Force tiering of a specific file or folder

A tiered file is not actually available on the server endpoint. It’s content will automatically be made available if requested. If you have files that take up space on your server and want to free up that space you can use the following code. When applied to a directory the script will recursively tier all files in that folder and sub-folders.

$Path = "D:\Data" #Local path of the files you want to tier

Set-Location $env:ProgramFiles\Azure\StorageSyncAgent
Import-Module ".\StorageSync.Management.ServerCmdlets.dll"
Invoke-StorageSyncCloudTiering -Path $Path

Check what application is tiering files on your server

If you start to see sync activity that is not expected it may be worth investigating further. Monitoring, backup or anti-virus may be tiering files in a way that will negatively impact performance and cause additional cost. The following command will return the 500 latest files that were tiered.

The primary application causing a file to be tiered on a general purpose file server should be “Access over SMB/System process”, by un-commenting the section in the script these events can be excluded from the output.

Set-Location $env:ProgramFiles\Azure\StorageSyncAgent
Import-Module ".\StorageSync.Management.ServerCmdlets.dll"

$RC = Get-StorageSyncFileRecallResult
$RC <# | where {$_.ProcessName -ne "Access over SMB/System process"} #> | ft EventTime, ProcessName, FileName 

Leave a Reply

Your email address will not be published. Required fields are marked *