Hi,
I am not a powershell specialist, with limited knowledge for powershell script.
But from you description, I think what you need is to configure Remote Desktop listener certificate so that you can remove the RDP connection warning.
If so, there are another two ways to achieve that. One is to use Windows Management Instrumentation (WMI) script and the other is to use registry editor.
Please follow the detailed steps in below article:
Remote Desktop listener certificate configurations
https://learn.microsoft.com/en-us/troubleshoot/windows-server/remote/remote-desktop-listener-certificate-configurations
Thanks,
Eleven
----------
If the Answer is helpful, please click "Accept Answer" and upvote it. Thanks.