Public/SEPPmailAPI-User.ps1

<#
.SYNOPSIS
    Get a single locally existing users properties
.DESCRIPTION
    This CmdLet lets you read the detailed properties of an existing user.
.EXAMPLE
    PS C:\> Get-SMAUser
.EXAMPLE
    PS C:\> Get-SMAUser -eMailAddress 'alice.miller@contoso.com'
    Get information about a SEPPmail user
.EXAMPLE
    PS C:\> Get-SMAUser -eMailAddress 'alice.miller@contoso.com' -Customer 'Contoso'
    Get information about a SEPPmail user of a specific customer
.EXAMPLE
    PS C:\> 'alice.miller@contoso.com','bob.brown@contoso.com'|Get-SMAUser
    Use the pipeline to retrieve users
#>

function Get-SMAUser
{
    [CmdletBinding()]
    param (
        #region REST-API path and query parameters
        [Parameter(
            Mandatory                       = $true,
            ValueFromPipelineByPropertyName = $true,
            ValueFromPipeline               = $true,
            HelpMessage                     = 'User E-Mail address'
            )]
        [string]$eMail,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'For MSP´s and multi-customer environments, set the GINA users customer'
            )]
        [string]$customer,
        #endregion

        #region SMA host parameters
        [Parameter(
            Mandatory = $false
            )]
        [String]$host = $Script:activeCfg.SMAHost,

        [Parameter(
            Mandatory = $false
            )]
        [int]$port = $Script:activeCfg.SMAPort,

        [Parameter(
            Mandatory = $false
            )]
        [String]$version = $Script:activeCfg.SMAPIVersion,

        [Parameter(
            Mandatory=$false
            )]
            [System.Management.Automation.PSCredential]$cred=$Script:activeCfg.SMACred,

        [Parameter(
            Mandatory=$false
            )]
        [switch]$SkipCertCheck=$Script:activeCfg.SMAskipCertCheck
        #endregion
    )

    begin {
        if (! (verifyVars -VarList $Script:requiredVarList))
        {
            Throw($missingVarsMessage);
        }; # end if
    }
    process {
        try {
            Write-Verbose "Creating URL path"
            $uriPath = "{0}/{1}" -f 'user', $eMail
    
            Write-Verbose "Building full request uri"
            if ($customer) {
                $boundParam = @{
                    customer = $customer
                }
            }
            $smaParams=@{
                Host=$Host;
                Port=$Port;
                Version=$Version;
            }; # end smaParams
            $uri = New-SMAQueryString -uriPath $uriPath -qParam $boundParam @smaParams
            
            Write-verbose "Crafting Invokeparam for Invoke-SMARestMethod"
            $invokeParam = @{
                Uri         = $uri 
                Method      = 'GET'
                Cred        =  $cred
                SkipCertCheck = $SkipCertCheck
            }
    
            Write-Verbose "Call Invoke-SMARestMethod $uri" 
            $UserRaw = Invoke-SMARestMethod @invokeParam
    
            Write-Verbose 'Filter data and return as PSObject'
            $GetUser = $userraw.Psobject.properties.value
    
            Write-Verbose 'Converting Umlauts from ISO-8859-1'
            $user = ConvertFrom-SMAPIFormat -inputObject $Getuser
    
            # Userobject
            if ($User) {
                return $User
            }
            else {
                Write-Information 'Nothing to return'
            }
        }
        catch {
            Write-Error "An error occured, see $error"
        }
    
    }
    end {

    }
}

<#
.SYNOPSIS
    Find a locally existing users by email address, name or uid.
.DESCRIPTION
    This CmdLet lets you quickly find users in the SEPPmail Database by various properties.
    It may be used with 4 different variants.
    1. By Name with the -name parameter and a part of the users name.
    2. By uid with the -uid parameter and an EXACT match of the uid.
    3. By email address with the -email parameter and and EXACT match of the email address
    4. By a part of the email address with the -partialMatch parameter.
 
    By Default
.EXAMPLE
    PS C:\> Find-SMAUser
    Emits all users and their details - may take some time
.EXAMPLE
    PS C:\> Find-SMAUser -email 'john.doe@fabrikam.eu'
    Emits the specific user with this email address.
.EXAMPLE
    PS C:\> Find-SMAUser -name 'john'
    Emits all users with john in the email address
.EXAMPLE
    PS C:\> Find-SMAUser -partialMatch 'fabrikam.eu'
    Emits all users with fabrikam.eu in the email address
.EXAMPLE
    PS C:\> Find-SMAUser -uid 'john'
    Emits the specific user john as UID
.EXAMPLE
    PS C:\> Find-SMAUser -customer 'Contoso'
    Emits e-mail addresses of all users of a particular customer
.EXAMPLE
    PS C:\> Find-SMAUser -List
    Emits all users and their details
.EXAMPLE
    PS C:\> Find-SMAUser -limit 5
    Emits all users but stop output at 5 objects
.EXAMPLE
    PS C:\> Find-SMAUser -active:$false
    Emits only active users
.EXAMPLE
    PS C:\> Find-SMAUser -activeWithinDays 14
    Emits only users which have sent emails within the last 14 days
.EXAMPLE
    PS C:\> Find-SMAUser -partialMatch 'tailspintoys.com' -activeWithinDays 14 -customer 'tailspintoys' -Limit 50
    Use a combination of parameters to pre-filter the output as good as possible.
#>

function Find-SMAUser
{
    [CmdletBinding(
        DefaultParameterSetName             = 'partialMatch'
    )]

    param (
        #region Define the 4 parametersets and the uniqe params in it
        [Parameter(
            ParameterSetName                = 'email',
            Mandatory                       = $true,
            Position                        = 0,
            HelpMessage                     = 'Find users by their e-Mail address'
            )]
        [string]$email,

        [Parameter(
            ParameterSetName                = 'partialMatch',
            Mandatory                       = $false,
            Position                        = 0,
            HelpMessage                     = 'Find users by a partial match of their e-Mail address'
            )]
        [string]$partialMatch = ' ',

        [Parameter(
            ParameterSetName                = 'name',
            Mandatory                       = $true,
            Position                        = 0,
            HelpMessage                     = 'Find users by their display name (parts of the name allowed)'
            )]
        [string]$name,

        [Parameter(
            ParameterSetName                = 'uid',
            Mandatory                       = $true,
            Position                        = 0,
            HelpMessage                     = 'Find users by their exact uid' 
            )]
        [string]$uid,
        #endregion

        #region common API params for all parametersets
        [Parameter(
                Mandatory                   = $false,
                HelpMessage                 = 'For MSP´s and multi-customer environments, limit query for a specific customer'
                )]
        [string]$customer,

        [Parameter(
            Mandatory                       = $false,
            HelpMessage                     = 'Show list with e-mail address only'
            )]
        [bool]$list = $true,

        [Parameter(
            Mandatory                       = $false,
            HelpMessage                     = 'limit output to <n> objects' 
            )]
        [int]$limit = 0,

        [Parameter(
            Mandatory                       = $false,
            HelpMessage                     = 'limit output to active or inactive objects' 
            )]
        [bool]$active,

        [Parameter(
            Mandatory                       = $false,
            HelpMessage                     = 'limit output to users which have sent e-mails within a certain timeframe (days)'
            )]
        [int]$activeWithinDays,
        #endregion

        #region SMAParameters
        [Parameter(Mandatory = $false)]
        [String]$host = $Script:activeCfg.SMAHost,

        [Parameter(Mandatory = $false)]
        [int]$port = $Script:activeCfg.SMAPort,

        [Parameter(Mandatory = $false)]
        [String]$version = $Script:activeCfg.SMAPIVersion,

        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential]$cred=$Script:activeCfg.SMACred,

        [Parameter(Mandatory=$false)]
        [switch]$SkipCertCheck=$Script:activeCfg.SMAskipCertCheck
        #endregion SMAParameters
    )

    if (! (verifyVars -VarList $Script:requiredVarList))
    {
        Throw($missingVarsMessage);
    }; # end if

    try {
        Write-Verbose "Building full request uri icluding all bound parameters"
        #$boundParam = $psCmdLet.MyInvocation.BoundParameters
        # Build querystring hashtable for conversion from parameters. Boundparams are eiter mandatory or have default values
        $boundParam = @{
                   'list' = $list
                  'limit' = $limit
            }

            # Now check if any of the parameters was set by the commandline and set their values for the querystring
                     if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('name')) {$boundParam.name = $name}
                    if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('email')) {$boundParam.email = $email}
             if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('partialMatch')) {$boundParam.partialMatch = $partialMatch}
                      if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('uid')) {$boundParam.uid = $uid}
                 if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('customer')) {$boundParam.customer = $customer}
                   if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('active')) {$boundParam.active = $active}
         if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('activeWithinDays')) {$boundParam.activeWithinDays = $activeWithinDays}

        $smaParams=@{
            Host=$Host;
            Port=$Port;
            Version=$Version;
        }; # end smaParams

        $uri = New-SMAQueryString -uriPath 'user' -qParam $boundParam @smaParams;

        Write-verbose "Crafting Invokeparam for Invoke-SMARestMethod"
        $invokeParam = @{
            Uri         = $uri 
            Method      = 'GET'
            Cred        =  $cred
            SkipCertCheck = $SkipCertCheck
        }

        Write-Verbose "Call Invoke-SMARestMethod $uri" 
        $UserRaw = Invoke-SMARestMethod @invokeParam

        Write-Verbose 'Filter data and return as PSObject'

        if ($list) {
            $Finduser = $userraw
        }
        else {
            $FindUser = $userraw.Psobject.properties.value
        }

        Write-Verbose 'Converting Umlauts from ISO-8859-1 and DateTime correctly'
        $user = foreach ($u in $finduser) {ConvertFrom-SMAPIFormat -inputobject $u}

        if ($User) {
            return $User
        }
        else {
            Write-Information 'Nothing to return'
        }

    }
    catch {
        Write-Error "An error occured, see $error"
    }
}

<#
.SYNOPSIS
    Create a new SEPPmail users
.DESCRIPTION
    This CmdLet lets you create a new user. You need at least 3 properties to create a user (name, uid and email)
.EXAMPLE
    PS C:\> New-SMAUser -uid 'm.musterfrau@contoso.com' -email 'm.musterfrau@contoso.com' -Name 'Maria Musterfrau'
    Basic information about a user. uid and email are identical
.EXAMPLE
    PS C:\> $UID = (New-Guid).guid
    PS C:\> New-SMAUser -uid $uid -email 'm.musterfrau@contoso.com' -Name 'Maria Musterfrau'
    Basic information about a user. uid is a GUID
.EXAMPLE
    PS C:\> $userinfo = @{
        uid = '245b8741-4724-4434-8343-bc26c9a10586'
        email = 'm.musterfrau@contoso.com'
        Name = 'Maria Musterfrau'
        locked = $false
        mayNotEncrypt = $false
        mayNotSign = $false
        password = ('aBc1$6tgR'|ConvertTo-SecureString -AsPlainText)
        customer = 'Contoso'
        notifications = 'never'
        mpkiSubjectPart = ''
    }
    PS C:\> New-SMAUser @userInfo
    Example of all parameters possible to create a user using parameter splatting
#>

function New-SMAUser
{
    [CmdletBinding(SupportsShouldProcess)]
    param (
        #region REST-API Parameters
        [Parameter(
            Mandatory                       = $true,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'Unique ID, mostly the e-Mail address'
            )]
        [string]$uid,

        [Parameter(
            Mandatory                       = $true,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'User E-Mail address'
            )]
        [string]$eMail,

        [Parameter(
            Mandatory                       = $true,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'The users full name'
            )]
        [string]$name,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'You may set the password for the user or leave it blank. API default is blank'
            )]
        [SecureString]$password,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = '!!CASE_SENSITIVE!! For MSP´s, multi-customer and cloud environments, set the users customer, API default is blank'
            )]
        [string]$customer,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'Disable the encrypt functionality for the user, API default is $false'
            )]
        [switch]$mayNotEncrypt,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'Disable the sign functionality for the user, API default is $false'
            )]
        [switch]$mayNotSign,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'Lock this user, API default is $false'
            )]
        [switch]$locked,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'Define if and how the user gets notified, API standard is domain default'
            )]
            [ValidateSet('never','always','domain default')]
        [string]$notifications,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'Userspecific static subject part'
            )]
        [string]$mpkiSubjectPart,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'SEPPmail group membership'
            )]
        [string[]]$memberOf,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'POP (IMAP) account userID (e-mail address)'
            )]
        [string]$mailAccountUID,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'POP (IMAP) account password as securestring'
            )]
        [secureString]$mailAccountPassword,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'POP (IMAP) account host'
            )]
        [string]$mailAccountHost,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'POP (IMAP) account host uses SSL'
            )]
        [boolean]$mailAccountSSL,
        #endregion

        #region SMAparams
        [Parameter(Mandatory = $false)]
        [String]$host = $Script:activeCfg.SMAHost,

        [Parameter(Mandatory = $false)]
        [int]$port = $Script:activeCfg.SMAPort,

        [Parameter(Mandatory = $false)]
        [String]$version = $Script:activeCfg.SMAPIVersion,

        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential]$cred=$Script:activeCfg.SMACred,

        [Parameter(Mandatory=$false)]
        [switch]$SkipCertCheck=$Script:activeCfg.SMAskipCertCheck
        #endregion
    )

    begin {
        if (! (verifyVars -VarList $Script:requiredVarList))
        {
            Throw($missingVarsMessage);
        }; # end if

        try {
            Write-Verbose "Building full request uri"
            $smaParams=@{
                Host=$Host;
                Port=$Port;
                Version=$Version;
            }; # end smaParams

            $uri = New-SMAQueryString -uriPath 'user' @smaParams;
        }
        catch {
            Write-Error "Error $error.CategoryInfo occured"
        }
    }

    process {
        try {
            Write-Verbose 'Crafting mandatory $body JSON'
            $bodyht = @{
                uid = $uid
                name = $name
                email = $email
            }
            Write-Verbose 'Adding Optional values to $body JSON'
                        if ($customer) {$bodyht.customer = $customer}
                          if ($locked) {$bodyht.locked = $locked}
                   if ($mayNotEncrypt) {$bodyht.mayNotEncrypt = $mayNotEncrypt}
                      if ($mayNotSign) {$bodyht.mayNotSign = $mayNotSign}
                    if ($notifications) {$bodyht.notifications = $notifications}
                        if ($password) {$bodyht.password = ($password|ConvertFrom-SecureString -asplaintext)}
                 if ($mpkiSubjectPart) {$bodyht.mpkiSubjectPart = $mpkiSubjectPart}
                        if ($memberOf) {$bodyht.memberOf = $memberOf}
                  if ($mailAccountUID) {$bodyht.mailAccountUID = $mailAccountUID}
             if ($mailAccountPassword) {$bodyht.mailAccountPassword = ($mailAccountPassword|ConvertFrom-Securestring -AsPlainText)}
                 if ($mailAccountHost) {$bodyht.mailAccountHost = $mailAccountHost}
                  if ($mailAccountSSL) {$bodyht.mailAccountSSL = $mailAccountSSL}

            
            $body = $bodyht|ConvertTo-JSON
            Write-verbose "Crafting Invokeparam for Invoke-SMARestMethod"
            $invokeParam = @{
                Uri         = $uri 
                Method      = 'POST'
                body        = $body
                Cred        =  $cred
                SkipCertCheck = $SkipCertCheck
            }

            if ($PSCmdLet.ShouldProcess($($bodyht.Email),"Create user")) {
                Write-Verbose "Call Invoke-SMARestMethod $uri"
                $UserRaw = Invoke-SMARestMethod @invokeParam
                #debug $userraw
                Write-Verbose 'Returning e-Mail address of new users'
                ($userraw.message -split ' ')[3]
            }
        }
        catch {
            Write-Error "An error occured, see $error"
        }
    }
    end {}
}

<#
.SYNOPSIS
    Modyfies a SEPPmail user
.DESCRIPTION
    This CmdLet lets you modity an existing user. You need the email address to identify the user.
.EXAMPLE
    PS C:\> Set-SMAUser -email 'm.musterfrau@contoso.com' -Name 'Martha Musterfrau'
    Change the UserName of m.musterfrau@contoso.com
.EXAMPLE
    PS C:\> $userinfo = @{
        email = 'm.musterfrau@contoso.com'
        Name = 'Marithe Musterfrau'
        locked = $true
        mayNotEncrypt = $false
        mayNotSign = $false
        password = 'aBc1$6tgR' # (As securestrig)
        customer = 'Contoso'
        notifications = 'never'
        mpkiSubjectPart = ''
        notifications = 'never'
        memberOf = 'myOwnGroup'
        mailAccountUID = 'm.musterfrau'
        mailAccountPassword = 'aBc1$6tgR' # (As securestrig)
        mailAccountHost = 'mail.contoso.com'
        mailAccountSSL = $true
    }
    PS C:\> Set-SMAUser @userInfo
    Example of all parameters possible to change a user using parameter splatting
#>

function Set-SMAUser
{
    [CmdletBinding(SupportsShouldProcess)]
    param (
        #region API parameters
        [Parameter(
            Mandatory                       = $true,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'User E-Mail address'
            )]
        [string]$eMail,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'The users full name'
            )]
        [string]$name,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'You may set the password for the user or leave it blank. API default is blank'
            )]
        [SecureString]$password,

<# [Parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = '!!CASE_SENSITIVE!! For MSP´s, multi-customer and cloud environments, set the users customer, API default is blank'
            )]
        [string]$customer,
#>

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'Disable the encrypt functionality for the user, API default is $false'
            )]
        [boolean]$mayNotEncrypt,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'Disable the sign functionality for the user, API default is $false'
            )]
        [boolean]$mayNotSign,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'Lock this user, API default is $false'
            )]
        [boolean]$locked,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'Define if and how the user gets notified, API standard is domain default'
            )]
        [ValidateSet('never','always','domain default')]
        [string]$notifications,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'Userspecific static subject part'
            )]
        [string]$mpkiSubjectPart,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'SEPPmail group membership'
            )]
        [string[]]$memberOf,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'POP (IMAP) account userID (e-mail address)'
            )]
        [string]$mailAccountUID,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'POP (IMAP) account password as securestring'
            )]
        [secureString]$mailAccountPassword,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'POP (IMAP) account host'
            )]
        [string]$mailAccountHost,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'POP (IMAP) account host uses SSL'
            )]
        [boolean]$mailAccountSSL,    
        #endregion

        #region SMAParams
        [Parameter(Mandatory = $false)]
        [String]$host = $Script:activeCfg.SMAHost,

        [Parameter(Mandatory = $false)]
        [int]$port = $Script:activeCfg.SMAPort,

        [Parameter(Mandatory = $false)]
        [String]$version = $Script:activeCfg.SMAPIVersion,

        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential]$cred=$Script:activeCfg.SMACred,

        [Parameter(Mandatory=$false)]
        [switch]$SkipCertCheck=$Script:activeCfg.SMAskipCertCheck 
        #endregion
    )
    begin {
        if (! (verifyVars -VarList $Script:requiredVarList))
        {
            Throw($missingVarsMessage);
        }; # end if
    }
    process {
        try  {
            
            Write-Verbose "Creating URL path"
            $uriPath = "{0}/{1}" -f 'user', $eMail
            Write-Verbose "Building full request uri"
            $boundParam = @{}
            
            $smaParams=@{
                Host=$Host;
                Port=$Port;
                Version=$Version;
            }; # end smaParams

            $uri = New-SMAQueryString -uriPath $uriPath -qParam $boundParam @smaParams;
            
            Write-Verbose 'Crafting mandatory $body JSON'
            $bodyht = @{
                email = $email
            }
            Write-Verbose 'Adding optional values to $body JSON'
                                                    if ($name) {$bodyht.name = $name}
                                                if ($customer) {$bodyht.customer = $customer}
                                                  if ($locked) {$bodyht.locked = $locked}
                                           if ($mayNotEncrypt) {$bodyht.mayNotEncrypt = $mayNotEncrypt}
                                              if ($mayNotSign) {$bodyht.mayNotSign = $mayNotSign}
                                         if ($mpkiSubjectPart) {$bodyht.mpkiSubjectPart = $mpkiSubjectPart}
                                            if ($notifications) {$bodyht.notifications = $notifications}
                                                if ($password) {$bodyht.password = ($password|ConvertFrom-SecureString -asplaintext)}
                                                if ($memberOf) {$bodyht.memberOf = $memberOf}
                                          if ($mailAccountUID) {$bodyht.mailAccountUID = $mailAccountUID}
                                     if ($mailAccountPassword) {$bodyht.mailAccountPassword = ($mailAccountPassword|ConvertFrom-Securestring -AsPlainText)}
                                         if ($mailAccountHost) {$bodyht.mailAccountHost = $mailAccountHost}
                                          if ($mailAccountSSL) {$bodyht.mailAccountSSL = $mailAccountSSL}

            $body = $bodyht|ConvertTo-JSON
            Write-verbose "Crafting Invokeparam for Invoke-SMARestMethod"
            $invokeParam = @{
                Uri         = $uri 
                Method      = 'PUT'
                body        = $body
                Cred        =  $cred
                SkipCertCheck = $SkipCertCheck
            }
            #debug $uri
            if ($PSCmdLet.ShouldProcess($($bodyht.Email),"Change user")) {
                Write-Verbose "Call Invoke-SMARestMethod $uri" 
                $UserRaw = Invoke-SMARestMethod @invokeParam
                #debug $userraw
                Write-Verbose 'Returning e-Mail addresses of updated user'
                ($userraw.message -split ' ')[3]
            }
        }
        catch {
            Write-Error "An error occured, see $error.CategoryInfo"
        }
    }
}

<#
.SYNOPSIS
    Remove a SEPPmail user
.DESCRIPTION
    This CmdLet lets you delete a SEPPmail user. You need the e-Mail address of the user. Optionally it is possible to leave the certificates and keys in the appliance.
.EXAMPLE
    PS C:\> Remove-SMAUser -email 'm.musterfrau@contoso.com'
    Delete a user and all keys and certificates.
.EXAMPLE
    PS C:\> Remove-SMAUser -email 'm.musterfrau@contoso.com' -keepKeys
    Delete a user but leave the keys. If you recreate the user with the same email address, the keys will be re-attached.
#>

function Remove-SMAUser
{
    [CmdletBinding(SupportsShouldProcess)]
    param (

        #region API Parameters
        [Parameter(
            Mandatory                       = $true,
            ValueFromPipelineByPropertyName = $true,
            ValueFromPipeline               = $true,
            HelpMessage                     = 'User E-Mail address'
            )]
        [string]$eMail,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'If true, certificates and private keys will not be deleted'
            )]
        [switch]$keepKeys,

        [Parameter(
            Mandatory                       = $false,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage                     = 'If true, the user is REALLY deleted from the database and is LOST forever'
            )]
        [switch]$purge,
        #endregion

        #region SMA Parameters
        [Parameter(Mandatory = $false)]
        [String]$host = $Script:activeCfg.SMAHost,

        [Parameter(Mandatory = $false)]
        [int]$port = $Script:activeCfg.SMAPort,

        [Parameter(Mandatory = $false)]
        [String]$version = $Script:activeCfg.SMAPIVersion,

        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential]$cred=$Script:activeCfg.SMACred,

        [Parameter(Mandatory=$false)]
        [switch]$SkipCertCheck=$Script:activeCfg.SMAskipCertCheck
        #endregion

    )

    begin {
        if (! (verifyVars -VarList $Script:requiredVarList))
        {
            Throw($missingVarsMessage);
        }; # end if

        try {
            Write-Verbose "Creating URL path"
            $uriPath = "{0}/{1}" -f 'user', $eMail
        }
        catch {
            Write-Error "Error$.categoryInfo happened"
        }
    }
    process {
        try {
            Write-Verbose "Building full request uri"
            $boundParam = @{ }

            if ($keepkeys) {$boundParam.keepkeys = $keepkeys}
               if ($purge) {$boundParam.purge = $purge}

            $smaParams=@{
                Host=$Host;
                Port=$Port;
                Version=$Version;
            }; # end smaParams
            $uri = New-SMAQueryString -uriPath $uriPath -qParam $boundParam @smaParams;
    
            Write-verbose "Crafting Invokeparam for Invoke-SMARestMethod"
            $invokeParam = @{
                Uri           = $uri 
                Method        = 'DELETE'
                Cred          = $cred
                SkipCertCheck = $SkipCertCheck
                }
            
            if ($PSCmdLet.ShouldProcess($email,"Remove User")) {
                Write-Verbose "Call Invoke-SMARestMethod $uri"
                $UserRaw = Invoke-SMARestMethod @invokeParam
                Write-Verbose 'Returning e-Mail addresses of removed user'
                ($userraw.message -split ' ')[3]
            }
        }
        catch {
            Write-Error "An error occured, see $error.CategoryInfo"
        }
    }
}

# SIG # Begin signature block
# MIIVzAYJKoZIhvcNAQcCoIIVvTCCFbkCAQExDzANBglghkgBZQMEAgEFADB5Bgor
# BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG
# KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCBON4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