AnthonyWatherston Thanks for taking a look. I double-checked that the function has the reader role and the tag contributor role on the target subscription (which happens to be the same sub that contains the function itself).
To verify line 31, I added this bit on line 32:
Write-Host "is line 31 working? Tags: $tags"
It does seem to be working, or at least, the line 32 results are the same when the script executes successfully or unsuccessfully. The output is always this in both cases:
2022-09-09T11:19:31.030 [Information] INFORMATION: is line 31 working? Tags: Microsoft.Azure.Commands.Tags.Model.PSTagsObject
I also suspected the resrouceID variable might not be getting created properly in line 14, but line 15 also consistently produces the correct ID whether or not the script fails. Here's an example of the output from a failed run:
2022-09-09T11:17:56.458 [Information] INFORMATION: ResourceId: /subscriptions/xxxxxxxxxxxxxxxxx/resourcegroups/testrg10/providers/Microsoft.Compute/disks/testdisk
Lastly, I've tried PS version 7.0 and 7.2 but it made no difference.
I'm not familiar with how to directly interface with the function's app insights, but I'll look that up and check it next.