Sample/Find-Duplicates/common/deviceManipulation.ps1

# ------------------------------------------------------------------
# Lenovo Copyright
#
# (C) Copyright Lenovo 2024 - present.
#
# LIMITED AND RESTRICTED RIGHTS NOTICE:
# If data or software is delivered pursuant a General Services
# Administration (GSA) contract, use, reproduction, or disclosure
# is subject to restrictions set forth in Contract No. GS-35F-05925.
# ------------------------------------------------------------------

using namespace System.Collections.Generic

function AddDevice()
{
    param (
        [Parameter(Mandatory = $true)]
        [Lenovo.SysMgmt.LXCA.Integration.Data.Node] $Device,
        [Parameter(Mandatory = $true)]
        [AllowEmptyCollection()]
        [List[DeviceData]] $Devices,
        [Parameter(Mandatory = $true)]
        [string] $LXCAHost

    )

    if ($Devices.Count -eq 0)
    {
        [DeviceData]$data = FillDeviceData -Device $Device 
        $Devices.Add($data)
    }
    else
    {
        $duplicate = $false
        #check if $Device has a match in $Devices
        foreach ($server in $Devices)
        {
            if ($server.Uuid -eq $Device.Uuid)
            {
                $server.LXCAs += $LXCAHost
                $duplicate = $true
            }
        }
        # no duplicate found: add as a new device
        if ($duplicate -eq $false)
        {
            [DeviceData]$data = FillDeviceData -Device $Device 
            $Devices.Add($data)
        }
    }
}

function FillDeviceData {
    param (
        [Parameter(Mandatory = $true)]
        [Lenovo.SysMgmt.LXCA.Integration.Data.Node] $Device
    )

    $data = [DeviceData]::new()
    $data.FQDN = $Device.FQDN
    $data.MgmtProcIPaddress = $Device.MgmtProcIPaddress
    $data.Uuid = $Device.Uuid
    $data.LXCAs += $LXCAHost
    return $data
}

function RemoveSingleAppearance
{
    param (
        [Parameter(Mandatory = $true)]
        [AllowEmptyCollection()]
        [List[DeviceData]] $OutList,
        [Parameter(Mandatory = $true)]
        [List[DeviceData]] $InList
    )

    foreach ($device in $InList)
    {
        if ($device.LXCAs.Count -ge 2)
        {
            $OutList.Add($device)
        }
    }
}

class DeviceData
{
    [string] $FQDN
    [string] $MgmtProcIPaddress
    [string] $Uuid
    [string[]] $LXCAs = @()
}

#EOF