Functions/Remove.ps1

function Remove-LinaAgent {
    <#
.SYNOPSIS
Deletes a Lina Agent.
.DESCRIPTION
Deletes a Lina Agent. Unique data will be reclaimed automatically by server after some time (configurable).
.INPUTS
Accept pipelining of LinaAgent objects (from Get-LinaAgent for example)
.OUTPUTS
None
.PARAMETER Name
Name of the agent to delete. Wildcards are accepted (don't forget to enable Bulk mode for actions on multiple agents).
.PARAMETER WhatIf
No actual deletion will happen if set. It will only display what agents would be deleted.
Sometimes also called "Dry Run mode"
.PARAMETER Bulk
Security parameter to enable deletion of multiple agents. If not set only one agent will be deleted.
.EXAMPLE
Remove-LinaAgent -Name "AGENT132"
Real deletion of a single agent by name
.EXAMPLE
Get-LinaAgent -Name "AGENT132" | Remove-LinaAgent
Same example as #1 but using piped commands
.EXAMPLE
Get-LinaAgent -Name "AGENT1*" | Remove-LinaAgent -Bulk -WhatIf
Simulates deletion of a multiple agents by name (WhatIf mode)
.EXAMPLE
Get-LinaAgent -Name "AGENT1*" | Remove-LinaAgent -Bulk
Real deletion of a multiple agents by name
.EXAMPLE
Get-LinaAgent -ID 164 | Remove-LinaAgent -WhatIf
Simulates deletion of a single agent by ID
#>

    [cmdletbinding(DefaultParameterSetName = "ByName")]
    Param(
        [Parameter(ValueFromPipeline = $True, Mandatory = $True, ParameterSetName = "ByPipeline")]
        [pscustomobject[]]$LinaAgents,

        [Parameter(Mandatory = $True, ParameterSetName = "ByName")]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        # Do not delete, only show will be done
        [Parameter(Mandatory = $False, ParameterSetName = "ByPipeline")]
        [Parameter(Mandatory = $False, ParameterSetName = "ByName")]
        [switch]$WhatIf,

        # Enable deleting multiple agents
        [Parameter(Mandatory = $False, ParameterSetName = "ByPipeline")]
        [Parameter(Mandatory = $False, ParameterSetName = "ByName")]        
        [switch]$Bulk
    )
    BEGIN {
        if ($global:GLOBAL_LINA_CONNECTED -eq $False) { 
            Write-Error "$($global:GLOBAL_DISCONNNECTED_MESSAGE)" 
            return
        }
        $nb_of_agents_deleted = 0
        
        if ($Name) {
            $LinaAgents = Get-LinaAgent -Name $Name 6>$null
        }
    }
    PROCESS {
        foreach ($LinaAgent in $LinaAgents) {
            # Should be useless now
            If (!$LinaAgent.ID) { return }

            $Name = $LinaAgent.Name
            $delete_id = $LinaAgent.ID

            if ($nb_of_agents_deleted -ge 1 -AND !$Bulk) {
                Write-Host2 "WARNING : Bulk mode has not been enabled. Agent $Name will not be deleted."
                return
            }

            
            if (!$delete_id) {
                Write-Host2 "WARNING : No agent found with this name."
                return
            }
            else {
                $body = 'data=<?xml version="1.0" encoding="UTF-8"?><HNConfig><Func>DEL</Func><Client><ID>' + $delete_id + '</ID></Client></HNConfig>'
                if (!$WhatIf) {
                    #$request = Invoke-WebRequest -Uri "$GLOBAL_LINA_SERVER/mng_client.html" -Method "POST" -ContentType "application/xml" -Body $body -WebSession $LoggedSession
                    if ($INT_LINA_API_VERSION -lt 60) {
                        # Lina < 6.0
                        $request = CallAPI -Path "/mng_client.html" -ContentType "application/xml" -Body $body
                    } else {
                        # Lina > 6.0
                        $request = CallAPI -Path "/mng_client.json" -ContentType "application/xml" -Body $body
                    } 
                }
                else {
                    # WhatIf mode enabled => does not delete agent
                    $request = "WHATIF"
                }
                if ([string]$request -like "*[0]*") {
                    Write-Host2 "Agent $Name successfully deleted."
                }
                elseif ($request -eq "WHATIF") {
                    Write-Host2 "Agent $Name will be deleted if not using 'WhatIf' mode"
                }
                else {
                    TranslateErrorCode -LinaError $request -FailedAction "delete agent $Name"
                }
            }
            $nb_of_agents_deleted += 1
 
        }
    }
    END { }

}

function Remove-LinaStrategy {
    <#
    .SYNOPSIS
    Deletes a Lina Strategy.
    .DESCRIPTION
    Deletes a Lina Strategy.
    .INPUTS
    Accept pipelining of LinaStrategy objects (from Get-LinaStrategy for example)
    .OUTPUTS
    None
    .PARAMETER Name
    Name of the Strategy to delete. Wildcards are accepted (don't forget to enable Bulk mode for actions on multiple agents).
    Note : Be careful, multiple Strategies can have the same name in different tenants.
    .PARAMETER WhatIf
    No actual deletion will happen if set. It will only display what strategy would be deleted.
    Sometimes also called "Dry Run mode"
    .PARAMETER Bulk
    Security parameter to enable deletion of multiple strategies. If not set only one strategy will be deleted.
    .EXAMPLE
    Remove-LinaStrategy -Name "MyStrat"
    Real deletion of a single strategy by name
    .EXAMPLE
    Get-LinaStrategy -Name "MyStrat" | Remove-LinaStrategy
    Same example as #1 but using piped commands
    .EXAMPLE
    Get-LinaStrategy -Name "Strat*" | Remove-LinaStrategy -Bulk -WhatIf
    Simulates deletion of a multiple strategies by name (WhatIf mode)
    .EXAMPLE
    Get-LinaStrategy -Name "Strat" | Remove-LinaStrategy -Bulk
    Real deletion of a multiple strategies by name
    #>

    [cmdletbinding(DefaultParameterSetName = "ByName")]
    Param(
        [Parameter(ValueFromPipeline = $True, Mandatory = $True, ParameterSetName = "ByPipeline")]
        [pscustomobject[]]$LinaStrategies,
    
        [Parameter(Mandatory = $True, ParameterSetName = "ByName")]
        [ValidateNotNullOrEmpty()]
        [string]$Name,
    
        # Do not delete, only show will be done
        [Parameter(Mandatory = $False, ParameterSetName = "ByPipeline")]
        [Parameter(Mandatory = $False, ParameterSetName = "ByName")]
        [switch]$WhatIf,
    
        # Enable deleting multiple strategies
        [Parameter(Mandatory = $False, ParameterSetName = "ByPipeline")]
        [Parameter(Mandatory = $False, ParameterSetName = "ByName")]        
        [switch]$Bulk
    )
    BEGIN {
        if ($global:GLOBAL_LINA_CONNECTED -eq $False) { 
            Write-Error "$($global:GLOBAL_DISCONNNECTED_MESSAGE)" 
            return
        }
        $nb_of_strategies_deleted = 0
        if ($Name) {
            $LinaStrategies = Get-LinaStrategy -Name $Name
        }
    }
    PROCESS {
        foreach ($LinaStrategy in $LinaStrategies) {
            # Should be useless now
            If (!$LinaStrategy.ID) { return }
    
            $Name = $LinaStrategy.Name
            $delete_id = $LinaStrategy.ID
    
            if ($nb_of_strategies_deleted -ge 1 -AND !$Bulk) {
                Write-Host2 "WARNING : Bulk mode has not been enabled. Strategy $Name will not be deleted."
                return
            }
    
                
            if (!$delete_id) {
                Write-Host2 "WARNING : No strategy found with this name."
                return
            }
            else {
                $body = 'data=<?xml version="1.0" encoding="UTF-8"?><HNConfig><Func>DEL</Func><ServiceProfile><ID>' + $delete_id + '</ID></ServiceProfile></HNConfig>'
                if (!$WhatIf) {
                    $request = CallAPI -Path "/mng_serviceprofile.html" -ContentType "application/xml" -Body $body 
                }
                else {
                    # WhatIf mode enabled => does not delete strategy
                    $request = "WHATIF"
                }
                if ([string]$request -like "*[0]*") {
                    Write-Host2 "Strategy $Name successfully deleted."
                }
                elseif ($request -eq "WHATIF") {
                    Write-Host2 "Strategy $Name will be deleted if not using 'WhatIf' mode"
                }
                else {
                    TranslateErrorCode -LinaError $request -FailedAction "delete strategy $Name"
                }
            }
            $nb_of_strategies_deleted += 1
     
        }
    }
    END { }
    
}

function Remove-LinaUser {
    <#
        .SYNOPSIS
        Deletes a User.
        .DESCRIPTION
        Deletes a User.
        .INPUTS
        Accept pipelining of LinaUser objects (from Get-LinaUser for example)
        .OUTPUTS
        None
        .PARAMETER Name
        Name of the User(s) to delete. Wildcards are accepted (don't forget to enable Bulk mode for actions on multiple users).
        Note : Be careful, multiple Users can have the same name in different tenants.
        .PARAMETER WhatIf
        No actual deletion will happen if set. It will only display what User(s) would be deleted.
        Sometimes also called "Dry Run mode"
        .PARAMETER Bulk
        Security parameter to enable deletion of multiple Users. If not set only one User will be deleted.
        .EXAMPLE
        Remove-LinaUser -Name "MyUser"
        Real deletion of a single User by name
        .EXAMPLE
        Get-LinaUser -Name "MyUser" | Remove-LinaUser
        Same example as #1 but using piped commands
        .EXAMPLE
        Get-LinaUser -Name "MyUser*" | Remove-LinaUser -Bulk -WhatIf
        Simulates deletion of a multiple Users by name (WhatIf mode)
        .EXAMPLE
        Get-LinaUser -Name "MyUser*" | Remove-LinaUser -Bulk
        Real deletion of a multiple Users by name.
        #>

    [cmdletbinding(DefaultParameterSetName = "ByName")]
    Param(
        [Parameter(ValueFromPipeline = $True, Mandatory = $True, ParameterSetName = "ByPipeline")]
        [pscustomobject[]]$LinaObjects,
        
        [Parameter(Mandatory = $True, ParameterSetName = "ByName")]
        [ValidateNotNullOrEmpty()]
        [string]$Name,
        
        # Do not delete, only show will be done
        [Parameter(Mandatory = $False, ParameterSetName = "ByPipeline")]
        [Parameter(Mandatory = $False, ParameterSetName = "ByName")]
        [switch]$WhatIf,
        
        # Enable deleting multiple objects
        [Parameter(Mandatory = $False, ParameterSetName = "ByPipeline")]
        [Parameter(Mandatory = $False, ParameterSetName = "ByName")]        
        [switch]$Bulk
    )
    BEGIN {
        if ($global:GLOBAL_LINA_CONNECTED -eq $False) { 
            Write-Error "$($global:GLOBAL_DISCONNNECTED_MESSAGE)" 
            return
        }
        $nb_of_objects_deleted = 0
        if ($Name) {
            $LinaObjects = Get-LinaUser -Name $Name
        }
    }
    PROCESS {
        $current_tenant_id = Get-LinaCurrentTenantID 6>$null
        foreach ($LinaObject in $LinaObjects) {
            # Should be useless now
            If (!$LinaObject.ID) { return }
        
            $Name = $LinaObject.Name
            $Tenant = $LinaObject.Tenant
            $TenantID = $LinaObject.TenantID
            $delete_id = $LinaObject.ID
        
            if ($nb_of_objects_deleted -ge 1 -AND !$Bulk) {
                Write-Host2 "WARNING : Bulk mode has not been enabled. User $Name will not be deleted."
                return
            }
        
                    
            if (!$delete_id) {
                Write-Host2 "WARNING : No User found with this name."
                return
            }
            else {
                if (!$WhatIf) {
                    # Looks like we need to be in the tenant before deleting a user ?!
                    $void = Set-LinaCurrentTenant -ID $TenantID 6>$null
                    $request = CallAPI -Path "/ADM/del_user.json?user_id=$delete_id" -ContentType "application/json"
                    $void = Set-LinaCurrentTenant -ID $current_tenant_id 6>$null

                }
                else {
                    # WhatIf mode enabled => does not delete object
                    $request = "WHATIF"
                }
                if ($request.status -eq 0) {
                    Write-Host2 "User $Name successfully deleted from Tenant $Tenant"
                }
                elseif ($request -eq "WHATIF") {
                    Write-Host2 "User $Name (in Tenant $Tenant) will be deleted if not using 'WhatIf' mode"
                }
                else {
                    TranslateErrorCode -LinaError $request -FailedAction "delete user $Name (ID : $delete_id) in Tenant $Tenant"
                }
            }
            $nb_of_objects_deleted += 1
         
        }
    }
    END { }
        
}


function Remove-LinaUserProfile {
    <#
        .SYNOPSIS
        Deletes a User Profile.
        .DESCRIPTION
        Deletes a User Profile.
        .INPUTS
        Accept pipelining of LinaUserProfile objects (from Get-LinaUserProfile for example)
        .OUTPUTS
        None
        .PARAMETER Name
        Name of the User Profile(s) to delete. Wildcards are accepted (don't forget to enable Bulk mode for actions on multiple profiles).
        Note : Be careful, multiple User Profiles can have the same name in different tenants.
        .PARAMETER WhatIf
        No actual deletion will happen if set. It will only display what User Profile(s) would be deleted.
        Sometimes also called "Dry Run mode"
        .PARAMETER Bulk
        Security parameter to enable deletion of multiple User Profiles. If not set only one User Profile will be deleted.
        .EXAMPLE
        Remove-LinaUserProfile -Name "MyUserProfile"
        Real deletion of a single User Profile by name
        .EXAMPLE
        Get-LinaUserProfile -Name "MyUserProfile" | Remove-LinaUserProfile
        Same example as #1 but using piped commands
        .EXAMPLE
        Get-LinaUserProfile -Name "MyUserProfile*" | Remove-LinaUserProfile -Bulk -WhatIf
        Simulates deletion of a multiple User Profiles by name (WhatIf mode)
        .EXAMPLE
        Get-LinaUserProfile -Name "MyUserProfile" | Remove-LinaUserProfile -Bulk
        Real deletion of a multiple User Profiles by name.
        #>

    [cmdletbinding(DefaultParameterSetName = "ByName")]
    Param(
        [Parameter(ValueFromPipeline = $True, Mandatory = $True, ParameterSetName = "ByPipeline")]
        [pscustomobject[]]$LinaObjects,
        
        [Parameter(Mandatory = $True, ParameterSetName = "ByName")]
        [ValidateNotNullOrEmpty()]
        [string]$Name,
        
        # Do not delete, only show will be done
        [Parameter(Mandatory = $False, ParameterSetName = "ByPipeline")]
        [Parameter(Mandatory = $False, ParameterSetName = "ByName")]
        [switch]$WhatIf,
        
        # Enable deleting multiple objects
        [Parameter(Mandatory = $False, ParameterSetName = "ByPipeline")]
        [Parameter(Mandatory = $False, ParameterSetName = "ByName")]        
        [switch]$Bulk
    )
    BEGIN {
        if ($global:GLOBAL_LINA_CONNECTED -eq $False) { 
            Write-Error "$($global:GLOBAL_DISCONNNECTED_MESSAGE)" 
            return
        }
        $nb_of_objects_deleted = 0
        if ($Name) {
            $LinaObjects = Get-LinaUserProfile -Name $Name
        }
    }
    PROCESS {
        foreach ($LinaObject in $LinaObjects) {
            # Should be useless now
            If (!$LinaObject.ID) { return }
        
            $Name = $LinaObject.Name
            $Tenant = $LinaObject.Tenant
            $delete_id = $LinaObject.ID
            
            if ($nb_of_objects_deleted -ge 1 -AND !$Bulk) {
                Write-Host2 "WARNING : Bulk mode has not been enabled. User Profile $Name will not be deleted."
                return
            }
        
                    
            if (!$delete_id) {
                Write-Host2 "WARNING : No User Profile found with this name."
                return
            }
            else {
                if (!$WhatIf) {
                    $request = CallAPI -Path "/ADM/del_profile.json?id=$delete_id" -ContentType "application/json" 
                }
                else {
                    # WhatIf mode enabled => does not delete object
                    $request = "WHATIF"
                }
                if ([string]$request -like "*[0]*") {
                    Write-Host2 "User Profile $Name successfully deleted from Tenant $Tenant"
                }
                elseif ($request -eq "WHATIF") {
                    Write-Host2 "User Profile $Name (in Tenant $Tenant) will be deleted if not using 'WhatIf' mode"
                }
                else {
                    TranslateErrorCode -LinaError $request -FailedAction "delete user profile $Name in Tenant $Tenant"
                }
            }
            $nb_of_objects_deleted += 1
         
        }
    }
    END { }
        
}

function Remove-LinaUserGroup {
    <#
        .SYNOPSIS
        Deletes a User Group.
        .DESCRIPTION
        Deletes a User Group.
        .INPUTS
        Accept pipelining of LinaUserGroup objects (from Get-LinaUserGroup for example)
        .OUTPUTS
        None
        .PARAMETER Name
        Name of the User Group(s) to delete. Wildcards are accepted (don't forget to enable Bulk mode for actions on multiple groups).
        Note : Be careful, multiple User Profiles can have the same name in different tenants.
        .PARAMETER WhatIf
        No actual deletion will happen if set. It will only display what User Group(s) would be deleted.
        Sometimes also called "Dry Run mode"
        .PARAMETER Bulk
        Security parameter to enable deletion of multiple User Groups. If not set only one User Group will be deleted.
        .EXAMPLE
        Remove-LinaUserGroup -Name "MyUserGroup"
        Real deletion of a single User Group by name
        .EXAMPLE
        Get-LinaUserGroup -Name "MyUserGroup" | Remove-LinaUserGroup
        Same example as #1 but using piped commands
        .EXAMPLE
        Get-LinaUserGroup -Name "MyUserGroup*" | Remove-LinaUserGroup -Bulk -WhatIf
        Simulates deletion of a multiple User Groups by name (WhatIf mode)
        .EXAMPLE
        Get-LinaUserGroup -Name "MyUserGroup*" | Remove-LinaUserGroup -Bulk
        Real deletion of a multiple User Groups by name.
        #>

    [cmdletbinding(DefaultParameterSetName = "ByName")]
    Param(
        [Parameter(ValueFromPipeline = $True, Mandatory = $True, ParameterSetName = "ByPipeline")]
        [pscustomobject[]]$LinaObjects,
        
        [Parameter(Mandatory = $True, ParameterSetName = "ByName")]
        [ValidateNotNullOrEmpty()]
        [string]$Name,
        
        # Do not delete, only show will be done
        [Parameter(Mandatory = $False, ParameterSetName = "ByPipeline")]
        [Parameter(Mandatory = $False, ParameterSetName = "ByName")]
        [switch]$WhatIf,
        
        # Enable deleting multiple objects
        [Parameter(Mandatory = $False, ParameterSetName = "ByPipeline")]
        [Parameter(Mandatory = $False, ParameterSetName = "ByName")]        
        [switch]$Bulk
    )
    BEGIN {
        if ($global:GLOBAL_LINA_CONNECTED -eq $False) { 
            Write-Error "$($global:GLOBAL_DISCONNNECTED_MESSAGE)" 
            return
        }
        $nb_of_objects_deleted = 0
        if ($Name) {
            $LinaObjects = Get-LinaUserGroup -Name $Name
        }
    }
    PROCESS {
        foreach ($LinaObject in $LinaObjects) {
            # Should be useless now
            If (!$LinaObject.ID) { return }
        
            $Name = $LinaObject.Name
            $Tenant = $LinaObject.Tenant
            $delete_id = $LinaObject.ID
        
            if ($nb_of_objects_deleted -ge 1 -AND !$Bulk) {
                Write-Host2 "WARNING : Bulk mode has not been enabled. User Group $Name will not be deleted."
                return
            }
        
                    
            if (!$delete_id) {
                Write-Host2 "WARNING : No User Group found with this name."
                return
            }
            else {
                if (!$WhatIf) {
                    $request = CallAPI -Path "/ADM/del_user_group.json?id=$delete_id" -ContentType "application/json" 
                }
                else {
                    # WhatIf mode enabled => does not delete object
                    $request = "WHATIF"
                }
                if ([string]$request -like "*[0]*") {
                    Write-Host2 "User Group $Name successfully deleted from Tenant $Tenant"
                }
                elseif ($request -eq "WHATIF") {
                    Write-Host2 "User Group $Name (in Tenant $Tenant) will be deleted if not using 'WhatIf' mode"
                }
                else {
                    TranslateErrorCode -LinaError $request -FailedAction "delete user group $Name in Tenant $Tenant"
                }
            }
            $nb_of_objects_deleted += 1
         
        }
    }
    END { }
        
}


function Remove-LinaAgentGroup {
    <#
    .SYNOPSIS
    Deletes a Lina Agent Group.
    .DESCRIPTION
    Deletes a Lina Agent Group. Agents in this group will go to the "unsorted" group.
    .INPUTS
    Accept pipelining of LinaAgentGroup objects (from Get-LinaAgentGroup for example)
    .OUTPUTS
    None
    .PARAMETER Name
    Name of the agent group to delete. Wildcards are accepted (don't forget to enable Bulk mode for actions on multiple groups).
    .PARAMETER WhatIf
    No actual deletion will happen if set. It will only display what agent groups would be deleted.
    Sometimes also called "Dry Run mode"
    .PARAMETER Bulk
    Security parameter to enable deletion of multiple agent group. If not set only one group will be deleted.
    .EXAMPLE
    Remove-LinaAgentGroup -Name "Group132"
    Real deletion of a single agent group by name
    .EXAMPLE
    Get-LinaAgentGroup -Name "Group132" | Remove-LinaAgentGroup
    Same example as #1 but using piped commands
    .EXAMPLE
    Get-LinaAgentGroup -Name "Group1*" | Remove-LinaAgentGroup -Bulk -WhatIf
    Simulates deletion of a multiple agent groups by name (WhatIf mode)
    .EXAMPLE
    Get-LinaAgentGroup -Name Group1*" | Remove-LinaAgentGroup -Bulk
    Real deletion of a multiple agent groups by name
    .EXAMPLE
    Get-LinaAgentGroups -ID 164 | Remove-LinaAgentGroups -WhatIf
    Simulates deletion of a single agent group by ID
    #>

    [cmdletbinding(DefaultParameterSetName = "ByName")]
    Param(
        [Parameter(ValueFromPipeline = $True, Mandatory = $True, ParameterSetName = "ByPipeline")]
        [pscustomobject[]]$LinaAgentGroups,
    
        [Parameter(Mandatory = $True, ParameterSetName = "ByName")]
        [ValidateNotNullOrEmpty()]
        [string]$Name,
    
        # Do not delete, only show will be done
        [Parameter(Mandatory = $False, ParameterSetName = "ByPipeline")]
        [Parameter(Mandatory = $False, ParameterSetName = "ByName")]
        [switch]$WhatIf,
    
        # Enable deleting multiple groups
        [Parameter(Mandatory = $False, ParameterSetName = "ByPipeline")]
        [Parameter(Mandatory = $False, ParameterSetName = "ByName")]        
        [switch]$Bulk
    )
    BEGIN {
        if ($global:GLOBAL_LINA_CONNECTED -eq $False) { 
            Write-Error "$($global:GLOBAL_DISCONNNECTED_MESSAGE)" 
            return
        }
        $nb_of_agentgroups_deleted = 0
        if ($Name -ne "") {
            $LinaAgentGroups = Get-LinaAgentGroup -Name $Name
        }

    }
    PROCESS {

        foreach ($LinaAgentGroup in $LinaAgentGroups) {
            # Should be useless now
            If (!$LinaAgentGroup.ID) { return }
    
            $Name = $LinaAgentGroup.Name
            $delete_id = $LinaAgentGroup.ID
                
            If ($LinaAgentGroup.InternalName -eq "UNCAT") {
                Write-Host2 "WARNING : The default agent group ($Name) cannot be removed."
                return
            }

            if ($nb_of_agentgroups_deleted -ge 1 -AND !$Bulk) {
                Write-Host2 "WARNING : Bulk mode has not been enabled. Agent group $Name will not be deleted."
                return
            }
    
                
            if (!$delete_id) {
                Write-Host2 "WARNING : No agent group found with this name."
                return
            }
            else {

                if ($INT_LINA_API_VERSION -lt 60 ) {
                    # Legacy mode
                    $hierarchy_id=1 
                }else {
                    # New agents groups
                    $hierarchy_id=2
                }

                $body = 'data=<?xml version="1.0" encoding="UTF-8"?><HNConfig><Func>DELG</Func><Hierarchy><ID>'+$hierarchy_id+'</ID><Group><ID>' + $delete_id + '</ID></Group></Hierarchy></HNConfig>'
                if (!$WhatIf) {
                    #$request = Invoke-WebRequest -Uri "$GLOBAL_LINA_SERVER/mng_client.html" -Method "POST" -ContentType "application/xml" -Body $body -WebSession $LoggedSession
                    $request = CallAPI -Path "/mng_hierarchy.html" -ContentType "application/xml" -Body $body 
                }
                else {
                    # WhatIf mode enabled => does not delete agent
                    $request = "WHATIF"
                }
                if ([string]$request -like "*[0]*") {
                    Write-Host2 "Agent group $Name successfully deleted."
                }
                elseif ($request -eq "WHATIF") {
                    Write-Host2 "Agent group $Name will be deleted if not using 'WhatIf' mode"
                }
                else {
                    TranslateErrorCode -LinaError $request -FailedAction "delete agent group $Name"
                }
            }
            $nb_of_agentgroups_deleted += 1
     
        }
    }
    END { }
}
function Remove-LinaTenant {
    <#
    .SYNOPSIS
    Deletes a Tenant.
    .DESCRIPTION
    Deletes a Tenant.
    .INPUTS
    Accept pipelining of LinaTenant objects (from Get-LinaTenant for example)
    .OUTPUTS
    None
    .PARAMETER Name
    Name of the Tenant to delete. Wildcards are accepted (don't forget to enable Bulk mode for actions on multiple tenants).
    .PARAMETER WhatIf
    No actual deletion will happen if set. It will only display what tenants would be deleted.
    Sometimes also called "Dry Run mode"
    .PARAMETER Bulk
    Security parameter to enable deletion of multiple tenants. If not set only one tenant will be deleted.
    .EXAMPLE
    Remove-LinaTenant -Name "MyTenant"
    Real deletion of a single tenant by name
    .EXAMPLE
    Get-LinaTenant -Name "MyTenant" | Remove-LinaTenant
    Same example as #1 but using piped commands
    .EXAMPLE
    Get-LinaTenant -Name "Group1*" | Remove-LinaTenant -Bulk -WhatIf
    Simulates deletion of a multiple Tenants by name (WhatIf mode)
    .EXAMPLE
    Get-LinaTenant -Name Group1*" | Remove-LinaTenant -Bulk
    Real deletion of a multiple Tenants by name
    .EXAMPLE
    Get-LinaTenant -ID 164 | Remove-LinaTenant -WhatIf
    Simulates deletion of a single Tenant by ID
    #>

    [cmdletbinding(DefaultParameterSetName = "ByName")]
    Param(
        [Parameter(ValueFromPipeline = $True, Mandatory = $True, ParameterSetName = "ByPipeline")]
        [pscustomobject[]]$LinaTenants,
    
        [Parameter(Mandatory = $True, ParameterSetName = "ByName")]
        [ValidateNotNullOrEmpty()]
        [string]$Name,
    
        # Do not delete, only show will be done
        [Parameter(Mandatory = $False, ParameterSetName = "ByPipeline")]
        [Parameter(Mandatory = $False, ParameterSetName = "ByName")]
        [switch]$WhatIf,
    
        # Enable deleting multiple groups
        [Parameter(Mandatory = $False, ParameterSetName = "ByPipeline")]
        [Parameter(Mandatory = $False, ParameterSetName = "ByName")]        
        [switch]$Bulk
    )
    BEGIN {
        if ($global:GLOBAL_LINA_CONNECTED -eq $False) { 
            Write-Error "$($global:GLOBAL_DISCONNNECTED_MESSAGE)" 
            return
        }
        $nb_of_tenants_deleted = 0
        if ($Name) {
            $LinaTenants = LinaTenantShort -Name $Name
        }
    }
    PROCESS {
        foreach ($LinaTenant in $LinaTenants) {
            # Should be useless now
            If (!$LinaTenant.ID) { return }
    
            $Name = $LinaTenant.Name
            $delete_id = $LinaTenant.ID
                
            if ($nb_of_tenants_deleted -ge 1 -AND !$Bulk) {
                Write-Host2 "WARNING : Bulk mode has not been enabled. Tenant $Name will not be deleted."
                return
            }
    
                
            if (!$delete_id) {
                Write-Host2 "WARNING : No Tenant found with this name."
                return
            }
            else {
                if (!$WhatIf) {
                    $request = CallAPI -Path "/ADM/del_domain.json?id=$delete_id" -ContentType "application/json"
                } 
                else {
                    # WhatIf mode enabled => does not delete agent
                    $request = "WHATIF"
                }
                if ($request.status -eq 0) {
                    Write-Host2 "Tenant $Name successfully deleted."
                }
                elseif ($request -eq "WHATIF") {
                    Write-Host2 "Tenant $Name will be deleted if not using 'WhatIf' mode"
                }
                else {
                    TranslateErrorCode -LinaError $request.status -FailedAction "delete Tenant $Name"
                }
            }
            $nb_of_agentgroups_deleted += 1
     
        }
    }
    END { }
}