Hi,
In the task sequence, we could input the product key directly in it instead of the script.
Here is the related article we could refer to:
https://learn.microsoft.com/en-us/mem/configmgr/osd/understand/task-sequence-steps#product-key
As we mentioned, use this script locally on the machine it worked, which account we used? is it current user account or system account? Please use the system account, and check if it is successful.
If the answer is the right solution, 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.