@Rajkumar Pawar , Thank you for posting this question.
Based on the error that you are getting as available in the question, the error is related to one of the script line prompting for response from use (note the highlighted section in the error below)
"a command that prompts the user failed because the host program or the command type does not support user interaction. the host was attempting to request confirmation with the following message: are you sure you want to perform this action?"
This is happening because Azure Automation does not allow user interaction during script execution. Based on the script, it looks like the message is being prompted for the line below:
Set-User -identity $user.UserPrincipalName -RemotePowerShellEnabled $true
I would suggest using the -Force and -confirm parameters with the set-user cmdlet, so that the script does not attempt to read user inputs during script execution. For more details, see the links below:
I would also suggest adding try{} catch{} blocks in the script so that you can get additional information about the error. For details about try, catch in powerhsll, see about_Try_Catch_Finally.
A sample catch block that I generally use in runbook as below:
catch
{
write-error $_.Exception.Message
#Response Code received
$StatusCode = $_.Exception.Response.StatusCode.value__
write-error "Error Code - $StatusCode"
#The scriptStackTrace would contains the line number for faulting code
write-error $_.ScriptStackTrace
throw ($_)
}
Hope this helps.
If the answer did not help, please add more context/follow-up question for it, and we will help you out. Else, if the answer helped, please click Accept answer so that it can help others in the community looking for help on similar topics.