T-SQL to Exclude the primary from List of AOAG Replicas
You could adapt the query from this link (which is looking for PRIMARY replicas) to look for secondary replicas.  Example below:

IF SERVERPROPERTY('IsHadrEnabled') = 1
BEGIN
    SELECT AGC.NAME -- Availability Group
        ,RCS.replica_server_name -- SQL cluster node name
        ,ARS.role_desc -- Replica Role
        ,AGL.dns_name -- Listener Name
    FROM sys.availability_groups_cluster AS AGC
    INNER JOIN sys.dm_hadr_availability_replica_cluster_states AS RCS ON RCS.group_id = AGC.group_id
    INNER JOIN sys.dm_hadr_availability_replica_states AS ARS ON ARS.replica_id = RCS.replica_id
    INNER JOIN sys.availability_group_listeners AS AGL ON AGL.group_id = ARS.group_id
    WHERE ARS.role_desc = 'SECONDARY'
END