Functions/BusinessLayer/Person.ps1

function Get-OIMPerson {
    <#
    .SYNOPSIS
    Gets one or more Person objects.

    .EXAMPLE
    Get-OIMPerson -CentralAccount jdoe

    .EXAMPLE
    Get-OIMPerson -id $uid
    #>

    [CmdletBinding(DefaultParameterSetName = 'Search')]
    param(
        [Parameter(ParameterSetName = 'Single', Mandatory = $true)]
        [Alias('uid')]
        [string]$id,

        [Parameter(ParameterSetName = 'Search')]
        [string]$CentralAccount,

        [Parameter(ParameterSetName = 'Search')]
        [string]$PersonnelNumber,

        [Parameter(ParameterSetName = 'Search')]
        [string]$Where,

        [Parameter(ParameterSetName = 'Search')]
        [int]$First
    )

    if ($PSCmdlet.ParameterSetName -eq 'Single') {
        Get-OIMObject -ObjectName Person -id $id
        return
    }

    $conditions = [Collections.Generic.List[string]]::new()
    if ($PSBoundParameters.ContainsKey('CentralAccount')) { $conditions.Add("CentralAccount = '$($CentralAccount | ConvertTo-SQLLiteral)'") }
    if ($PSBoundParameters.ContainsKey('PersonnelNumber')) { $conditions.Add("PersonnelNumber = '$($PersonnelNumber | ConvertTo-SQLLiteral)'") }
    if ($PSBoundParameters.ContainsKey('Where')) { $conditions.Add("($Where)") }

    $getParams = @{ ObjectName = 'Person' }
    if ($conditions.Count -gt 0) { $getParams['Where'] = $conditions -join ' AND ' }
    if ($PSBoundParameters.ContainsKey('First')) { $getParams['First'] = $First }

    Get-OIMObject @getParams
}

function New-OIMPerson {
    <#
    .SYNOPSIS
    Creates a Person object.

    .PARAMETER checkexists
    If a Person with the given -CentralAccount already exists, return it instead of creating a duplicate.

    .EXAMPLE
    New-OIMPerson -FirstName John -LastName Doe -CentralAccount jdoe
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true)]
        [string]$FirstName,

        [Parameter(Mandatory = $true)]
        [string]$LastName,

        [string]$CentralAccount,

        [hashtable]$Properties = @{},

        [switch]$checkexists
    )

    if ($checkexists -and $PSBoundParameters.ContainsKey('CentralAccount')) {
        $existing = Get-OIMPerson -CentralAccount $CentralAccount
        if ($null -ne $existing) { return $existing }
    }

    $allProperties = @{
        FirstName = $FirstName
        LastName  = $LastName
    }
    if ($PSBoundParameters.ContainsKey('CentralAccount')) { $allProperties['CentralAccount'] = $CentralAccount }
    foreach ($key in $Properties.Keys) { $allProperties[$key] = $Properties[$key] }

    if ($PSCmdlet.ShouldProcess("$FirstName $LastName", 'Create Person')) {
        New-OIMObject -ObjectName Person -Properties $allProperties -Confirm:$false
    }
}

function Set-OIMPerson {
    <#
    .SYNOPSIS
    Updates properties on one or more Person objects.

    .EXAMPLE
    Get-OIMPerson -CentralAccount jdoe | Set-OIMPerson -Properties @{IsInActive = $true}
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $Object,

        [Parameter(Mandatory = $true)]
        [hashtable]$Properties
    )

    Process {
        if ($PSCmdlet.ShouldProcess("$($Object.UID_Person)", 'Update Person')) {
            $Object | Set-OIMObject -Properties $Properties -Confirm:$false
        }
    }
}

function Remove-OIMPerson {
    <#
    .SYNOPSIS
    Removes one or more Person objects.

    .EXAMPLE
    Get-OIMPerson -CentralAccount jdoe | Remove-OIMPerson
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $Object
    )

    Process {
        if ($PSCmdlet.ShouldProcess("$($Object.UID_Person)", 'Remove Person')) {
            $Object | Remove-OIMObject -Confirm:$false
        }
    }
}