Samples/E2E-hub-devices/common/devices.ps1

# ------------------------------------------------------------------
# Lenovo Copyright
#
# (C) Copyright Lenovo 2026 - 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.
# ------------------------------------------------------------------

function DiscoverDevices()
{
    [CmdletBinding()]
    param (
        [Parameter(mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string[]] $DevicesIPs,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        $ManagerID
    )

    $result = Find-LXC1Devices -ManagerID $ManagerID `
                               -Hosts $DevicesIPs

    return $result
}

function GetDiscoveredDevices()
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        $ManagerIP
    )

    $discovered = $null
    do
    {
        $discovered = Get-LXC1DiscoveredDevice -ManagementHub $ManagerIP
        if ($discovered.Response.results.Count -eq 0)
        {
            Write-Host "Waiting for discovered devices to be available in portal ..."
            Start-Sleep -Seconds 5
        }
    } while ($discovered.Response.results.Count -eq 0)
    $devices = @()
    foreach ($device in $($discovered.Response.results))
    {
        $devices += $device.uuid
    }
    return $devices
}

function AddDiscoveredDevices()
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        $ManagerID,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        $DeviceUUIDs,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [PSCredential]$DeviceCredential
    )

    $result = Add-LXC1Devices -UserName $DeviceCredential.GetNetworkCredential().UserName `
                              -Password $DeviceCredential.Password `
                              -HubID $ManagerID `
                              -DeviceUUIDs $DeviceUUIDs                                   
    return $result
}

function GetDevicesInventory()
{
    $devices = $null
    do
    {
        $devices = Get-LXC1Device
        if ($devices.Response.results.Count -eq 0)
        {
            Write-Host "Waiting for devices to be available in portal ..."
            Start-Sleep -Seconds 5
        }
    } while ($devices.Response.results.Count -eq 0)
    return $devices
}

function DevicePowerOps()
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        $Devices,
        [Parameter(Mandatory = $true)]
        [ValidateSet("powerOn", "powerOffNormally", "powerOffImmediately",
         "restartNormally", "restartImmediately", "restartSystemSetup", "restartManagementController")]
        $Action
    )
    $deviceIDs = @()
    foreach($item in $Devices.Response.results)
    {
        $deviceIDs += $item.id
    }
    $result = Invoke-LXC1DevicePowerAction -PowerAction $Action `
                                           -DeviceIDs $deviceIDs
    
    return $result
}

function RemoveDevices()
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        $Devices
    )

    $devicesIDs = @()
    foreach($dev in $Devices.Response.results)
    {
        $devicesIDs += $dev.id
    }

    $result = Remove-LXC1Devices -DeviceIDs $devicesIDs
    return $result
}

#EOF