Wednesday, August 23, 2017

Invoke-O365AzureSync.ps1

Today's script is for those who manage AzureSync and looking for a quick way to kick off Full or Delta syncs without having to RDP to the server and execute script.

Pretty straight forward script and lots of comments / verbose messages.  Unless requested will not be providing a code break out post.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<#
.SYNOPSIS
    Run Azure (DirSync) on dedicated remote server
.DESCRIPTION
    Run Azure (DirSync) on dedicated remote server
.PARAMETER Type
    Type of Sync to complete

    o Delta - Changes only
    o Full - Complete re-sync
.PARAMETER ADSyncPath
    Enter path for ADSync modeule if different then default
.PARAMETER Server
    Target server that has Azure Sync installed
.EXAMPLE
    Execute a full Azure Sync
    
    Invoke-O365AzureSync.ps1 -Server server -Type Full
.EXAMPLE
    Execute a change only Azure Sync
    
    Invoke-O365AzureSync.ps1 -Server server -Type Delta
.NOTES
    Created by Chris Lee
    Date September 6, 2016
.LINK  
    GitHub: https://github.com/clee1107/Public/blob/master/O365/Invoke-O365AzureSync.ps1
    Blogger:http://www.myitresourcebook.com/2017/08/invoke-o365azuresyncps1.html
 
#>

[Cmdletbinding()]
Param
(
    [string]
    [Parameter(Mandatory=$true)]
    [ValidateSet('Delta','Full')]
    $type,
    [string]
    $ADSyncPath= 'C:\Program Files\Microsoft Azure AD Sync\Bin\ADSync\ADSync.psd1',
    [string]
    $Server
)

#################################
## DO NOT EDIT BELOW THIS LINE ##
#################################

## region Functions
    Function Test-Verbose
        {
            [cmdletbinding()]
            Param()
            Write-Verbose "Verbose output"
            "Regular output"
        }
    Test-Verbose
## endregion

#################################
## DO NOT EDIT BELOW THIS LINE ##
#################################

## Connect to remote server
    Write-Verbose -Message "Opening PSSession to $Server"
    $Session = New-PSSession -ComputerName $Server
## Check that ADSync is present at supplied path
    Write-Verbose -Message "Checking for ADSync.psd1 at $ADSyncPath"
    If (Invoke-command -Session $Session -ArgumentList $ADSyncPath -scriptblock { param ($ADSyncPath) Test-Path $ADSyncPath})
        {
            Write-Verbose -Message "ADSync Module found"
            Invoke-Command -Session $Session -ArgumentList $ADSyncPath -scriptblock { param ($ADSyncPath) Import-Module $ADSyncPath}
            Write-Verbose -Message "ADSync Module Loaded"
            ## Check if request for Delta Sync otherwise (else) execute full
                If ($type -eq "Delta")
                    {
                        Write-Verbose -Message "Executing Change Only (Delta) Sync"
                        Invoke-Command -Session $Session {Start-ADSyncSyncCycle -PolicyType Delta}
                    }
                Else
                    {
                        Write-Verbose -Message "Executing Full Sync"
                        Invoke-Command -Session $Session {Start-ADSyncSyncCycle -PolicyType Initial}
                    }
        }
    Else
        {
            Verbose-Error "Unable to locate ADSync.psd1 at $ADSyncPath"
        }
## Close and Remove the remote session
    Write-Verbose -Message "Removing PSSession to $Server"
    Remove-PSSession $Session


Full script can be accessed from following link:
https://github.com/clee1107/Public/blob/master/O365/Invoke-O365AzureSync.ps1
Further reference links for PowerShell cmdlets used can be found on following post:

Code Snippets created via: http://hilite.me/


1 comment: