@danny_v1 have you looked at the functionality provided:
https://duet3d.dozuki.com/Wiki/GCode#Section_M581_Configure_external_trigger

Any trigger number # greater then 1 causes the macro file sys/trigger#.g to be executed.

so you can setup a macro to do what you want and then use a trigger to call that macro.