When you are writing Azure automation script, the code may thrown exception or error with message like below.'The client '093e9259-xxxx-xxxx-xxxx-xxxxxe6d6b67' with object id '093e9259-xxxx-xxxx-xxxx-9a5xxe6d6b67' does not have authorization to perform action 'Microsoft.Resources/subscriptions/resourcegroups/write' over scope '/subscriptions/xx/resourcegroups/xx' or the scope is invalid. If access was recently granted, please refresh your credentials.'
In my case, I was trying to create a resource group using C# code. In your case, error may be due to some other type of resource. But the general cause is same. As the error suggests that the service principal account that I am using, does not have authorization to perform write action on the specified resource type. To fix this error, all you have to do is, fix the permissions associated with your service principal account. To create resources in Azure subscription, your service principal account needs to have Contributor level role for that resource. In Azure portal goto Subscription node and then use Access Control node. Then use "Grant Access To This Resource" widget in main view and select the role you want to assign to this service principal.