<commit_before>﻿
$namespace = @{dsml="http://www.dsml.org/DSML"; 'ms-dsml'="http://www.microsoft.com/MMS/DSML"}

function Install-SharePointSyncConfiguration
{
<#
.Synopsis
   Configures the Synchronization Service for SharePoint User Profile Synchronization
.DESCRIPTION
   Long description
.EXAMPLE
   Install-SharePointSyncConfiguration -Path C:\SharePointSync -ForestDnsName litware.ca -ForestCredental (Get-Credential LITWARE\Administrator) -OrganizationalUnit 'ou=Litwarians,dc=Litware,dc=ca' -SharePointUrl http://SharePointServer:5555 -SharePointCredential (Get-Credential LITWARE\Administrator)
.EXAMPLE
    $spProps = @{
        Path                 = 'C:\Temp\SharePointSync'
        ForestDnsName        = 'litware.ca'
        ForestCredential     = New-Object PSCredential ("LITWARE\administrator", (ConvertTo-SecureString 'J$p1ter' -AsPlainText -Force))
        OrganizationalUnit   = 'ou=Litwarians,dc=Litware,dc=ca'
        SharePointUrl        = 'http://cmvm38386:9140'
        SharePointCredential = New-Object PSCredential ("LITWARE\administrator", (ConvertTo-SecureString 'J$p1ter' -AsPlainText -Force))
    }
    Install-SharePointSyncConfiguration @spProps -Verbose

#>
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Path to the configuration XML files
        [Parameter(Mandatory=$true, Position=0)]
        $Path,

        # DNS name of the Active Directory forest to synchronize (ie - litware.ca)
        [Parameter(Mandatory=$true, Position=1)]
        $ForestDnsName,

        # Credential for connecting to Active Directory
        [Parameter(Mandatory=$true, Position=2)]
        [PSCredential]
        $ForestCredential,

        # OU to synchronize to SharePoint
        [Parameter(Mandatory=$true, Position=3)]
        $OrganizationalUnit,

        # URL for SharePoint
        [Parameter(Mandatory=$true, Position=4)]
        [Uri]    
        $SharePointUrl,

        # Credential for connecting to SharePoint
        [Parameter(Mandatory=$true, Position=5)]
        [PSCredential]
        $SharePointCredential,

        # Flow Direction for Profile Pictures
        [Parameter(Mandatory=$false, Position=6)]
        [ValidateSet('Export only (NEVER from SharePoint)', 'Import only (ALWAYS from SharePoint)')]
        [String]
        $PictureFlowDirection = 'Export only (NEVER from SharePoint)'
    )

    #region Pre-requisites
    if (-not (Get-SynchronizationServiceRegistryKey))
    {
        throw "The Synchronization Service is not installed on this computer.  Please install the MIM Synchronization Service on this computer, or run this script on a computer where the MIM Synchronization Service is installed." 
    }

    if (-not (Get-Service -Name FimSynchronizationService))
    {
        throw "The Synchronization Service is installed but not running.  Please start the MIM Synchronization Service before running this script (Start-Service -Name FimSynchronizationService).  If the service fails to start please see the event log for details." 
    }

    if ((Test-SynchronizationServicePermission) -eq $false)
    {
        throw "The current user must be a member of the Synchronization Service Admins group before this command can be run.  You may need to logoff/logon before the group membership takes effect."
    }
    #endregion

    ### Load the Synchronization PowerShell snap-in
    #Add-PSSnapin miis.ma.config
    Import-Module -Name (Join-Path (Get-SynchronizationServicePath) UIShell\Microsoft.DirectoryServices.MetadirectoryServices.Config.dll) 



    Write-Verbose "Contacting AD to get the partition details"
    $RootDSE                = [ADSI]"LDAP://$ForestDnsName/RootDSE"
    $DefaultNamingContext   = [ADSI]"LDAP://$($RootDSE.defaultNamingContext)"
    $ConfigurationPartition = [ADSI]"LDAP://$($RootDSE.configurationNamingContext)"

    Write-Verbose "Configuring the Active Directory Connector"
    Write-Verbose "  AD Forest:               $ForestDnsName"
    Write-Verbose "  AD OU:                   $OrganizationalUnit"
    Write-Verbose "  AD Credential:           $($ForestCredential.UserName)" 
    Write-Verbose "  AD Naming Partition:     $($RootDSE.defaultNamingContext)"
    Write-Verbose "  AD Config Partition:     $($RootDSE.configurationNamingContext)"

    $admaXmlFilePath = Join-Path $Path MA-ADMA.XML
    [xml]$admaXml = Get-Content -Path $admaXmlFilePath
    $admaXml.Save("$admaXmlFilePath.bak")

    ### Fix up the Private Configuration
    $ForestCredentialParts = $ForestCredential.UserName -split '\\'
    $admaXml.'saved-ma-configuration'.'ma-data'.'private-configuration'.'adma-configuration'.'forest-name'         = $ForestDnsName
    $admaXml.'saved-ma-configuration'.'ma-data'.'private-configuration'.'adma-configuration'.'forest-login-user'   = $ForestCredentialParts[1]
    $admaXml.'saved-ma-configuration'.'ma-data'.'private-configuration'.'adma-configuration'.'forest-login-domain' = $ForestCredentialParts[0]

    ### Fix up the Domain partition
    $domainPartition = Select-Xml -Xml $admaXml -XPath "//ma-partition-data/partition[name='DC=Litware,DC=com']"
    $domainPartition.Node.name = $DefaultNamingContext.distinguishedName.ToString()
    $domainPartition.Node.'custom-data'.'adma-partition-data'.dn = $DefaultNamingContext.distinguishedName.ToString()
    $domainPartition.Node.'custom-data'.'adma-partition-data'.name = $ForestDnsName
    $domainPartition.Node.'custom-data'.'adma-partition-data'.guid = (New-Object guid $DefaultNamingContext.objectGUID).ToString('B').ToUpper() 
    $domainPartition.Node.filter.containers.inclusions.inclusion = $DefaultNamingContext.distinguishedName.ToString()
    $domainPartition.Node.filter.containers.exclusions.exclusion = $ConfigurationPartition.distinguishedName.ToString()
    $domainPartition.Node.filter.containers.inclusions.inclusion = $OrganizationalUnit

    ### Fix up the Configuration partition
    $configPartition = Select-Xml -Xml $admaXml -XPath "//ma-partition-data/partition[name='CN=Configuration,DC=Litware,DC=com']"
    $configPartition.Node.name = $ConfigurationPartition.distinguishedName.ToString()
    $configPartition.Node.'custom-data'.'adma-partition-data'.dn = $ConfigurationPartition.distinguishedName.ToString()
    $configPartition.Node.'custom-data'.'adma-partition-data'.name = $ForestDnsName
    $configPartition.Node.'custom-data'.'adma-partition-data'.guid = (New-Object guid $ConfigurationPartition.objectGUID).ToString('B').ToUpper() 
    $configPartition.Node.filter.containers.inclusions.inclusion = "CN=Partitions," + $ConfigurationPartition.distinguishedName.ToString()

    $admaXml.Save($admaXmlFilePath)

    Write-Verbose "Importing the Synchronization Service configuration"
    Write-Verbose "  Path: $Path"
    Import-MIISServerConfig -Path $Path -Verbose    

    Write-Verbose "Set-MIISADMAConfiguration -MAName ADMA -Forest $ForestDnsName -Credentials $ForestCredential -Verbose"
    Set-MIISADMAConfiguration -MAName ADMA -Credentials $ForestCredential -Forest $ForestDnsName -Verbose    
    
    Write-Verbose "Configuring the SharePoint Connector"
    Write-Verbose "  SharePoint URL:          $SharePointUrl"
    Write-Verbose "  SharePoint Host:         $($SharePointUrl.Host)"
    Write-Verbose "  SharePoint Port:         $($SharePointUrl.Port)"
    Write-Verbose "  SharePoint Picture Flow: $PictureFlowDirection"
    Write-Verbose "  SharePoint Protocol:     $($SharePointUrl.Scheme)"
    Write-Verbose "  SharePoint Credential:   $($SharePointCredential.UserName)"
    Set-MIISECMA2Configuration -MAName SPMA -ParameterUse ‘connectivity’ -HTTPProtocol $SharePointUrl.Scheme -HostName $SharePointUrl.Host -Port $SharePointUrl.Port -PictureFlowDirection $PictureFlowDirection -Credentials $SharePointCredential -Verbose

    Write-Verbose "Copying the extension DLL to the Sychronization Service extensions folder"
    Write-Verbose "  Extensions Folder:       $(Get-SynchronizationServicePath)"
    Copy-Item -Path (Join-Path $Path *.dll) -Destination (Join-Path (Get-SynchronizationServicePath) Extensions)
}##Closing: function Install-SharePointSyncConfiguration

function Start-SharePointSync<commit_msg>Adding the Publish-SynchronizationAssembly command to the install function Commenting out the call to Set-MIISADMAConfiguration until that command is fixed (wipes out our configuration partition)<commit_after>﻿
$namespace = @{dsml="http://www.dsml.org/DSML"; 'ms-dsml'="http://www.microsoft.com/MMS/DSML"}

function Install-SharePointSyncConfiguration
{
<#
.Synopsis
   Configures the Synchronization Service for SharePoint User Profile Synchronization
.DESCRIPTION
   Long description
.EXAMPLE
   Install-SharePointSyncConfiguration -Path C:\SharePointSync -ForestDnsName litware.ca -ForestCredental (Get-Credential LITWARE\Administrator) -OrganizationalUnit 'ou=Litwarians,dc=Litware,dc=ca' -SharePointUrl http://SharePointServer:5555 -SharePointCredential (Get-Credential LITWARE\Administrator)
.EXAMPLE
    $spProps = @{
        Path                 = 'C:\Temp\SharePointSync'
        ForestDnsName        = 'litware.ca'
        ForestCredential     = New-Object PSCredential ("LITWARE\administrator", (ConvertTo-SecureString 'J$p1ter' -AsPlainText -Force))
        OrganizationalUnit   = 'ou=Litwarians,dc=Litware,dc=ca'
        SharePointUrl        = 'http://cmvm38386:9140'
        SharePointCredential = New-Object PSCredential ("LITWARE\administrator", (ConvertTo-SecureString 'J$p1ter' -AsPlainText -Force))
    }
    Install-SharePointSyncConfiguration @spProps -Verbose

#>
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Path to the configuration XML files
        [Parameter(Mandatory=$true, Position=0)]
        $Path,

        # DNS name of the Active Directory forest to synchronize (ie - litware.ca)
        [Parameter(Mandatory=$true, Position=1)]
        $ForestDnsName,

        # Credential for connecting to Active Directory
        [Parameter(Mandatory=$true, Position=2)]
        [PSCredential]
        $ForestCredential,

        # OU to synchronize to SharePoint
        [Parameter(Mandatory=$true, Position=3)]
        $OrganizationalUnit,

        # URL for SharePoint
        [Parameter(Mandatory=$true, Position=4)]
        [Uri]    
        $SharePointUrl,

        # Credential for connecting to SharePoint
        [Parameter(Mandatory=$true, Position=5)]
        [PSCredential]
        $SharePointCredential,

        # Flow Direction for Profile Pictures
        [Parameter(Mandatory=$false, Position=6)]
        [ValidateSet('Export only (NEVER from SharePoint)', 'Import only (ALWAYS from SharePoint)')]
        [String]
        $PictureFlowDirection = 'Export only (NEVER from SharePoint)'
    )

    #region Pre-requisites
    if (-not (Get-SynchronizationServiceRegistryKey))
    {
        throw "The Synchronization Service is not installed on this computer.  Please install the MIM Synchronization Service on this computer, or run this script on a computer where the MIM Synchronization Service is installed." 
    }

    if (-not (Get-Service -Name FimSynchronizationService))
    {
        throw "The Synchronization Service is installed but not running.  Please start the MIM Synchronization Service before running this script (Start-Service -Name FimSynchronizationService).  If the service fails to start please see the event log for details." 
    }

    if ((Test-SynchronizationServicePermission) -eq $false)
    {
        throw "The current user must be a member of the Synchronization Service Admins group before this command can be run.  You may need to logoff/logon before the group membership takes effect."
    }
    #endregion

    ### Load the Synchronization PowerShell snap-in
    #Add-PSSnapin miis.ma.config
    Import-Module -Name (Join-Path (Get-SynchronizationServicePath) UIShell\Microsoft.DirectoryServices.MetadirectoryServices.Config.dll) 



    Write-Verbose "Contacting AD to get the partition details"
    $RootDSE                = [ADSI]"LDAP://$ForestDnsName/RootDSE"
    $DefaultNamingContext   = [ADSI]"LDAP://$($RootDSE.defaultNamingContext)"
    $ConfigurationPartition = [ADSI]"LDAP://$($RootDSE.configurationNamingContext)"

    Write-Verbose "Configuring the Active Directory Connector"
    Write-Verbose "  AD Forest:               $ForestDnsName"
    Write-Verbose "  AD OU:                   $OrganizationalUnit"
    Write-Verbose "  AD Credential:           $($ForestCredential.UserName)" 
    Write-Verbose "  AD Naming Partition:     $($RootDSE.defaultNamingContext)"
    Write-Verbose "  AD Config Partition:     $($RootDSE.configurationNamingContext)"

    $admaXmlFilePath = Join-Path $Path MA-ADMA.XML
    [xml]$admaXml = Get-Content -Path $admaXmlFilePath
    $admaXml.Save("$admaXmlFilePath.bak")

    ### Fix up the Private Configuration
    $ForestCredentialParts = $ForestCredential.UserName -split '\\'
    $admaXml.'saved-ma-configuration'.'ma-data'.'private-configuration'.'adma-configuration'.'forest-name'         = $ForestDnsName
    $admaXml.'saved-ma-configuration'.'ma-data'.'private-configuration'.'adma-configuration'.'forest-login-user'   = $ForestCredentialParts[1]
    $admaXml.'saved-ma-configuration'.'ma-data'.'private-configuration'.'adma-configuration'.'forest-login-domain' = $ForestCredentialParts[0]

    ### Fix up the Domain partition
    $domainPartition = Select-Xml -Xml $admaXml -XPath "//ma-partition-data/partition[name='DC=Litware,DC=com']"
    $domainPartition.Node.name = $DefaultNamingContext.distinguishedName.ToString()
    $domainPartition.Node.'custom-data'.'adma-partition-data'.dn = $DefaultNamingContext.distinguishedName.ToString()
    $domainPartition.Node.'custom-data'.'adma-partition-data'.name = $ForestDnsName
    $domainPartition.Node.'custom-data'.'adma-partition-data'.guid = (New-Object guid $DefaultNamingContext.objectGUID).ToString('B').ToUpper() 
    $domainPartition.Node.filter.containers.inclusions.inclusion = $DefaultNamingContext.distinguishedName.ToString()
    $domainPartition.Node.filter.containers.exclusions.exclusion = $ConfigurationPartition.distinguishedName.ToString()
    $domainPartition.Node.filter.containers.inclusions.inclusion = $OrganizationalUnit

    ### Fix up the Configuration partition
    $configPartition = Select-Xml -Xml $admaXml -XPath "//ma-partition-data/partition[name='CN=Configuration,DC=Litware,DC=com']"
    $configPartition.Node.name = $ConfigurationPartition.distinguishedName.ToString()
    $configPartition.Node.'custom-data'.'adma-partition-data'.dn = $ConfigurationPartition.distinguishedName.ToString()
    $configPartition.Node.'custom-data'.'adma-partition-data'.name = $ForestDnsName
    $configPartition.Node.'custom-data'.'adma-partition-data'.guid = (New-Object guid $ConfigurationPartition.objectGUID).ToString('B').ToUpper() 
    $configPartition.Node.filter.containers.inclusions.inclusion = "CN=Partitions," + $ConfigurationPartition.distinguishedName.ToString()

    $admaXml.Save($admaXmlFilePath)

    Write-Verbose "Importing the Synchronization Service configuration"
    Write-Verbose "  Path: $Path"
    Import-MIISServerConfig -Path $Path -Verbose    

    #region BUG - Avoiding the call to Set-MIISADMAConfiguration because it deletes the ADMA partitions
    #TODO - fix this part of the function once we get an updated Set-MIISADMAConfiguration PowerShell cmdlet from MIM 
    Write-Warning "======================================================================================="
    Write-Warning "IMPORTANT: the Password must be set on the AD Connector before sychronization will work"
    Write-Warning "======================================================================================="
    #Write-Verbose "Set-MIISADMAConfiguration -MAName ADMA -Forest $ForestDnsName -Credentials $ForestCredential -Verbose"
    #Set-MIISADMAConfiguration -MAName ADMA -Credentials $ForestCredential -Forest $ForestDnsName -Verbose  
    #endregion  
    
    Write-Verbose "Configuring the SharePoint Connector"
    Write-Verbose "  SharePoint URL:          $SharePointUrl"
    Write-Verbose "  SharePoint Host:         $($SharePointUrl.Host)"
    Write-Verbose "  SharePoint Port:         $($SharePointUrl.Port)"
    Write-Verbose "  SharePoint Picture Flow: $PictureFlowDirection"
    Write-Verbose "  SharePoint Protocol:     $($SharePointUrl.Scheme)"
    Write-Verbose "  SharePoint Credential:   $($SharePointCredential.UserName)"
    Set-MIISECMA2Configuration -MAName SPMA -ParameterUse ‘connectivity’ -HTTPProtocol $SharePointUrl.Scheme -HostName $SharePointUrl.Host -Port $SharePointUrl.Port -PictureFlowDirection $PictureFlowDirection -Credentials $SharePointCredential -Verbose

    Write-Verbose "Publishing the Sync Rules Extension DLL to the Sychronization Service extensions folder"      
    Publish-SynchronizationAssembly -Path (Join-Path $Path SynchronizationRulesExtensions.cs) -Verbose
}##Closing: function Install-SharePointSyncConfiguration

function Start-SharePointSync
{'fraction_non_alphanumeric': 0.11459497617506913, 'fraction_numerical': 0.002941349491146538, 'mean_word_length': 3.9606069448497228, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5100789', 'n_tokens_mistral': 5045, 'n_tokens_neox': 4880, 'n_words': 1057}