Public/Convert-ADUsersToBBUserExport.ps1

Function Convert-ADUsersToBBUserExport {
    <#
    .SYNOPSIS
        Convert-ADUsersToBBUserExport the user information from ActiveDirectory to the format needed for GoBright BrightBooking
    .DESCRIPTION
        Convert-ADUsersToBBUserExport the user information from ActiveDirectory to the format needed for GoBright BrightBooking. Expecting the output of Get-ADUsersForBB
    .PARAMETER ADUserPincodePropertyName
        Optional ActiveDirectory User Property which contains the pincode
    .PARAMETER ADUserNamePropertyName
        Optional ActiveDirectory User Property which contains the name of the user, in case you do not want to use the default property
    .PARAMETER ADSpecificUsername
        Optional way to get a specific username from ActiveDirectory which should be used to authenticate the users when he logs in into GoBright BrightBooking (app/portal). You can choose which username should be used, DOMAIN\UserName or the UserPrincipalName (UPN)
    .EXAMPLE
        Get-ADUsersForBB -Filter * | Convert-ADUsersToBBUserExport
        # Get all users in the Active Directory and convert the information to the needed format
    .EXAMPLE
        Get-ADUsersForBB -SearchBase "OU=Office,DC=Company,DC=com" -ADUserPincodePropertyName PersonnelNumber -ADUserNamePropertyName FullUserName | Convert-ADUsersToBBUserExport -ADUserPincodePropertyName PersonnelNumber -ADUserNamePropertyName FullUserName
        # Get the users in the Active Directory, which in the specified SearchBase path, and use the custom property 'PersonnelNumber' as pincode, and the custom property 'FullUserName' as username
    .LINK
        https://support.gobright.com/
    .LINK
        https://technet.microsoft.com/library/hh852208.aspx
    .LINK
        Get-ADUsersForBB
    .LINK
        Export-ADUsersToBB
    #>


    [CmdletBinding()]
    Param(
      [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
       [System.Object[]]$pipelineADUsers,

      [Parameter(Mandatory=$False,Position=1)]
       [string]$ADUserPincodePropertyName,
       
      [Parameter(Mandatory=$False,Position=2)]
       [string]$ADUserNamePropertyName,
       
      [Parameter(Mandatory=$False)]
       [string]$ADUserMobilePropertyName = "Mobile",

      [Parameter(Mandatory=$False,Position=3)]
      [ValidateSet("None","UserPrincipalName","DomainPlusUsername")]
       [string]$ADSpecificUsername = "None"
    )

    Begin {
        # Process the incoming ADUsers
        $outputUsers = @()
    }

    Process {
        $lastDCParts = ""
        $lastDomainNetbiosName = ""
        
        Foreach ($ADUser in $pipelineADUsers)
        {
            $userName = ""
            If ($ADUserNamePropertyName)
            {
                $userName = $ADUser.$ADUserNamePropertyName
            }
            Else
            {
                $userName = $ADUser.DisplayName
            }
                        
            $userMobile = ""
            If ($ADUserMobilePropertyName)
            {
                $userMobile = $ADUser.$ADUserMobilePropertyName
            }

            $userEmailAddress = $ADUser.Mail
            
            $userEmailEnabled = $false
            If ($ADUser.Enabled -And $userEmailAddress)
            {
                $userEmailEnabled = $true
            }

            $userAuthenticationUsername = "";
            If ($ADSpecificUsername -eq "UserPrincipalName")
            {
                $userAuthenticationUsername = $ADUser.UserPrincipalName;    
            }
            ElseIf ($ADSpecificUsername -eq "DomainPlusUsername")
            {                
                $dcParts = (($ADUser.DistinguishedName.Split(",") | Where-Object {$_ -like "DC=*"}) -join ",")
                If ($dcParts -ne $lastDCParts)
                {
                    $lastDomainNetbiosName = (Get-ADDomain $dcParts).NetBIOSName
                    $lastDCParts = $dcParts
                }
                $userAuthenticationUsername = "$($lastDomainNetbiosName)\$($ADUser.SamAccountName)"
            }
            
            $userPincode = ""
            If ($ADUserPincodePropertyName)
            {
                $userPincode = $ADUser.$ADUserPincodePropertyName
            }

            $outputUserPropertiesHash = [ordered]@{
                EmailAddress              = $userEmailAddress
                Name                      = $userName
                TelephoneMobile           = $userMobile
                AuthenticationUsername    = $userAuthenticationUsername
                Pincode                   = $userPincode 
                Active                    = $userEmailEnabled
                UniqueImportID            = $ADUser.ObjectGUID        
            }

            $outputUser = New-Object PSObject -Property $outputUserPropertiesHash
            $outputUsers += $outputUser
        }
    }

    End {
        # Return the converted users
        Return $outputUsers
    }
}