#Requires -Version 7.4

function Test-vSEALServiceExists {
    <#
    .SYNOPSIS
        Tests if a Windows service exists.

    .DESCRIPTION
        Checks whether a Windows service with the specified name exists on the system.
        Uses Get-Service with error suppression to determine existence by default,
        or CIM/WMI when -UseCIM is specified.

    .PARAMETER Name
        The name of the service to check for.

    .PARAMETER UseCIM
        Use CIM/WMI to check for service existence instead of Get-Service.
        This can be useful when Get-Service has issues with certain services.

    .PARAMETER PassThru
        Return the service object instead of a boolean value.
        Returns the service object if it exists, $null otherwise.

    .OUTPUTS
        [bool] $true if the service exists, $false otherwise (default).
        [System.ServiceProcess.ServiceController] or [CimInstance] when -PassThru is specified.

    .EXAMPLE
        Test-vSEALServiceExists -Name 'Spooler'
        Returns $true if the Print Spooler service exists.

    .EXAMPLE
        Test-vSEALServiceExists -Name 'NonExistentService'
        Returns $false if the service does not exist.

    .EXAMPLE
        if (Test-vSEALServiceExists -Name 'wuauserv') {
            Write-Host 'Windows Update service exists'
        }
        Conditional check for service existence.

    .EXAMPLE
        $service = Test-vSEALServiceExists -Name 'Spooler' -PassThru
        if ($service) {
            Write-Host "Service status: $($service.Status)"
        }
        Get the service object directly.

    .EXAMPLE
        Test-vSEALServiceExists -Name 'Spooler' -UseCIM
        Use CIM to check if the service exists.
    #>
    [CmdletBinding()]
    [OutputType([bool], [System.ServiceProcess.ServiceController], [Microsoft.Management.Infrastructure.CimInstance])]
    param(
        [Parameter(Mandatory, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        [Parameter()]
        [Alias('UseWMI')]
        [switch]$UseCIM,

        [Parameter()]
        [switch]$PassThru
    )

    Write-vSEALLog -Message "Testing if service [$Name] exists..." -Level 'VERBOSE'

    if ($UseCIM) {
        try {
            $service = Get-CimInstance -ClassName Win32_Service -Filter "Name='$Name'" -ErrorAction Stop
            if ($service) {
                Write-vSEALLog -Message "Service [$Name] exists (via CIM)." -Level 'VERBOSE'
                if ($PassThru) {
                    return $service
                }
                return $true
            }
            else {
                Write-vSEALLog -Message "Service [$Name] does not exist (via CIM)." -Level 'VERBOSE'
                if ($PassThru) {
                    return $null
                }
                return $false
            }
        }
        catch {
            Write-vSEALLog -Message "Service [$Name] does not exist or CIM query failed." -Level 'VERBOSE'
            if ($PassThru) {
                return $null
            }
            return $false
        }
    }
    else {
        try {
            $service = Get-Service -Name $Name -ErrorAction Stop
            Write-vSEALLog -Message "Service [$Name] exists." -Level 'VERBOSE'
            if ($PassThru) {
                return $service
            }
            return $true
        }
        catch {
            Write-vSEALLog -Message "Service [$Name] does not exist or is inaccessible." -Level 'VERBOSE'
            if ($PassThru) {
                return $null
            }
            return $false
        }
    }
}
