<# .DESCRIPTION A Runbook example which continuously check for files and directories changes in recursive mode For a specific Azure File Share in a specific Sync Group / Cloud Endpoint Using the Run As Account (Service Principal in Azure AD) .NOTES Filename : Enable-ImmediateFileSync Author : Charbel Nemnom Version : 1.0 Date : 24-August-2019 Updated : 26-August-2019 .LINK To provide feedback or for further assistance please visit: https://charbelnemnom.com #> Param ( [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()] [String] $AzureSubscriptionId, [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()] [String] $ResourceGroupName, [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()] [String] $StorageSyncServiceName, [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()] [String] $SyncGroupName, [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()] [String] $Path ) $connectionName = "AzureRunAsConnection" Try { #! Get the connection "AzureRunAsConnection " $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName Write-Output "Logging in to Azure..." Connect-AzAccount -ServicePrincipal ` -TenantId $servicePrincipalConnection.TenantId ` -ApplicationId $servicePrincipalConnection.ApplicationId ` -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint } Catch { If (!$servicePrincipalConnection) { $ErrorMessage = "Connection $connectionName not found..." throw $ErrorMessage } Else { Write-Error -Message $_.Exception throw $_.Exception } } Select-AzSubscription -SubscriptionId $AzureSubscriptionId #! Get Cloud Endpoint Name $azsync = Get-AzStorageSyncCloudEndpoint -ResourceGroupName "$ResourceGroupName" -StorageSyncServiceName $StorageSyncServiceName ` -SyncGroupName "$SyncGroupName" Write-Output "Get Azure StorageSync Cloud Endpoint Name: $($azsync.CloudEndpointName)" #! Invoke-AzStorageSyncChangeDetection Write-Output "Check for files and directories changes for $StorageSyncServiceName in $SyncGroupName" Invoke-AzStorageSyncChangeDetection -ResourceGroupName $ResourceGroupName -StorageSyncServiceName $StorageSyncServiceName ` -SyncGroupName $SyncGroupName -CloudEndpointName $azsync.CloudEndpointName -DirectoryPath $Path -Recursive Write-Output ("")