Hi Mark Dicks,
We understand your concern, as the inquiries you provided, we might not have enough data to troubleshoot it effectively. Could you send more additional information to assist you better via "Private Message":
- What version of SharePoint are you using?
- Could you send me entire the script in so we can have a look and try to fix it for you?
If you feel uncertain about the privacy and security related to your business when sending the entire script in the Q&A forum, we would like to suggest you raising a ticket with Microsoft Support. They have the required resources and in-depth backend knowledge to resolve this issue more efficiently and faster. As forum moderators, we do not have the necessary testing environment to effectively handle this request so it may take a long time to troubleshoot it.
If you need to raise ticket, you can go to Microsoft 365 admin center and click "Help & support" icon to raise your ticket.
If you need further assistance, please let us know.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread