I too am experiencing the same behavior and would appreciate any assistance or tips if anyone has managed to get around this. The intention would be to automate the process of:
1) Deploying a report to a workspace (works)
2) Configuring any parameters (works)
3) Taking over the dataset (works)
3) Specifying the use of the gateway (a mapped datasource should already exist, a single locally hosted MSSQL database in this instance). Struggling at this step.
So far I have noticed that the discover-gateways-in-group method seems to return no gateway id when running this as a service principal but does seem to return a gateway id when running as an impersonated user. This is true even if the service principal is added as a user to the datasource for the gateway or as a gateway admin. I've also tried giving the service principal the scope permissions mentioned in the documentation - DataSet.ReadWriteAll
Is there an additional step that is required in order for the service princiapal to make use of datasources that have already been manually created? I suspect I may be missing something.
I am primarily using PowerShell and making any required Rest calls using the Invoke-PowerBIRestMethod.
Would very much appreciate any guidance or suggestions!