AAG trigger a query when failing over
I can not see your script but I am assuming you get orphaned users on the database after the failover because the users are linked to SQL Server local logins with different SIDs on the different instances.
The best way would be to recreate the login with the same SID in both instances CREATE USER SID=&lt;sid&gt; so you do not have to fix orphaned users every time you failover.
If this solution doesn't suit your needs, perhaps you can set up an alert for event 1480 - AG Role Change, and run the script as the response action.