Sync-AAD.psm1
function Sync-AAD { <# .SYNOPSIS Initiates an Azure Active Directory Connect sync cycle on a remote server. .DESCRIPTION This function connects to a remote server and triggers an Azure Active Directory Connect sync cycle using the Start-ADSyncSyncCycle cmdlet. .PARAMETER ComputerName The name of the remote computer where the sync cycle should be initiated. .NOTES Author: Eric Meinders Version: 1.0 #> [cmdletbinding()] param ( [parameter(Mandatory, Position=0, ValueFromPipeline)] [string]$ComputerName, [parameter(Position=1)] [System.Management.Automation.PSCredential] [System.Management.Automation.Credential()] $Credential ) BEGIN { # Check if the remote computer is reachable if (!(Test-Connection -ComputerName $ComputerName -Count 1 -ErrorAction SilentlyContinue)) { throw "Can't connect to $ComputerName" } # Define parameters for the Invoke-Command cmdlet $Parameters = @{ ComputerName = $ComputerName Credential = if ($Credential) { $Credential } else { (Get-Credential -Message "Please provide your ADM account credentials") } ScriptBlock = { Import-Module adsync Start-ADSyncSyncCycle -PolicyType Delta } } } PROCESS { $success = $true # Invoke the command on the remote computer try { Invoke-Command @Parameters -Verbose -ErrorAction Stop } catch { $success = $false $Error[0].Exception.Message } } END { if ($success) { Write-Host "Sync complete" -ForegroundColor Green } } } |