Public/Get-specCameraInfo.ps1

Function Get-SpecCameraInfo {
    <#
    .SYNOPSIS
    Gets information for up to two cameras on a computer.
 
    .DESCRIPTION
    This function retrieves details about cameras connected to the local or a remote computer. It uses the Get-PnpDevice cmdlet to find cameras, then outputs information in a specific format that includes:
 
    * Camera Device ID
    * Friendly Name
    * Manufacturer
    * Hardware ID
 
    The function is designed to prioritize the first two cameras found. If only one camera is present, the second camera's fields will be null.
 
    .PARAMETER ComputerName
    The name of the computer to check for cameras. Defaults to the local computer.
 
    .EXAMPLE
    Get-SpecCameraInfo
 
    Retrieves camera information from the local computer.
 
    .EXAMPLE
    Get-SpecCameraInfo -ComputerName "Computer1"
 
    Retrieves camera information from the remote computer named "Computer1".
 
    .OUTPUTS
    A custom object containing information about the first two cameras found.
 
    .NOTES
    Author: owen.heaume
    Change log:
        - 1.0 Initial release
        - 1.1 Added error handling for when no cameras are found
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $false)]
        [string]$ComputerName = $env:ComputerName
    )

    begin {}

    process {
        try {
            $CameraInfo = Get-PnpDevice -PresentOnly -Class Camera -ea stop
        } catch {
            Write-Host "No cameras found" -ForegroundColor DarkGray
            $camerainfo = @()
        }

        $CameraList = @()

        foreach ($Camera in $CameraInfo) {
            $CameraList += [pscustomobject]@{
                CameraName         = $Camera.FriendlyName
                CameraHardwareID   = $Camera.HardwareID
                CameraManufacturer = $Camera.Manufacturer
                CameraDeviceID     = $Camera.DeviceID
            }
        }

        # Assuming the table needs information for only two cameras
        if ($CameraList.Count -gt 1) {
            $cam1 = $CameraList[0]
            $cam2 = $CameraList[1]

            $result = [pscustomobject]@{
                cam1DeviceID     = $cam1.CameraDeviceID
                cam1FriendlyName = $cam1.CameraName
                cam1Manufacturer = $cam1.CameraManufacturer
                cam1hardwareID   = $cam1.CameraHardwareID
                cam2DeviceID     = $cam2.CameraDeviceID
                cam2FriendlyName = $cam2.CameraName
                cam2Manufacturer = $cam2.CameraManufacturer
                cam2hardwareID   = $cam2.CameraHardwareID
            }
        } elseif ($CameraList.Count -eq 1) {
            $cam1 = $CameraList[0]

            $result = [pscustomobject]@{
                cam1DeviceID     = $cam1.CameraDeviceID
                cam1FriendlyName = $cam1.CameraName
                cam1Manufacturer = $cam1.CameraManufacturer
                cam1hardwareID   = $cam1.CameraHardwareID
                cam2DeviceID     = $null
                cam2FriendlyName = $null
                cam2Manufacturer = $null
                cam2hardwareID   = $null
            }
        } else {
            $result = [pscustomobject]@{
                cam1DeviceID     = $null
                cam1FriendlyName = $null
                cam1Manufacturer = $null
                cam1hardwareID   = $null
                cam2DeviceID     = $null
                cam2FriendlyName = $null
                cam2Manufacturer = $null
                cam2hardwareID   = $null
            }
        }

        # Output the result
        $result
    }
    end {}
}