Mail Alert when threshold of 10000 pendingcmds are breached
The following suggestions are not fully tested.

Immediately after

Insert INTO #pendingcmds (pendingcmdcount, estimatedprocesstime)
select * FROM OPENROWSET('SQLOLEDB', 
'Server=Server\Instance;Trusted_Connection=yes;', 
'set fmtonly off; 
exec distribution..sp_replmonitorsubscriptionpendingcmds 
@publisher=''Server\Instance'', 
@publisher_db=''testproduction'', 
@publication=''Summary_Inventory_TestProduction'', 
@subscriber=''Server'', 
@subscriber_db=''TestReplAzure'', 
@subscription_type=0')


I would delete any rows where your threshold was not met.

DELETE FROM #pendingcmds WHERE pendingcmdcount &lt;= 10000


Then, I would change your test for whether to send the email or not 

From 

IF LEN(@HTML) &gt; 10 

            EXEC msdb.dbo.sp_send_dbmail
            @recipients = 'tester@test.com',
            @body_format =  'HTML',
            @body= @HTML,
            @subject = @MailSubject,
            @profile_name = 'MailRelay'


TO:

IF (SELECT COUNT(*) FROM #pendingcmds) &gt; 0 
    EXEC msdb.dbo.sp_send_dbmail
    @recipients = 'tester@test.com',
    @body_format =  'HTML',
    @body= @HTML,
    @subject = @MailSubject,
    @profile_name = 'MailRelay'