Autoconfig userdata scripts for ec2 is failing to start\stop windows service

0

I am creating an ec2 instance and trying to execute some powershell scripts during userdata script execution to stop service -> change logon -> start service.

I am able to run get-service to get the detail of the service. while running stop-service the cmdlet is failing with below error:

command used: Invoke-Command -Credential $credential -ComputerName . -ScriptBlock {sc.exe start $service_name}

tried using normal cmdlet too: Stop-service $service_name

Error: Connecting to remote server localhost failed with the following error message: Illegal operation attempted on a registry key that has been marked for deletion. For more information, see the about_Remote_Troubleshooting help topic.

I tried various methods using sc.exe ex: sc.exe stop $service_name net stop $service_name.

asked 4 months ago277 views
1 Answer
0

Dear Customer , Hope you are doing good!!

The error message "Illegal operation attempted on a registry key that has been marked for deletion" typically indicates an issue with accessing or modifying registry keys during the execution of PowerShell scripts. Here are a few steps you can take to troubleshoot and potentially resolve this issue:

[1] Check Service Dependencies: Ensure that the service you're trying to stop doesn't have dependencies that are preventing it from stopping properly. You can use the Get-Service cmdlet to check the service dependencies.

[2] Run PowerShell Script Locally : Try running your PowerShell script locally on the EC2 instance to see if you encounter the same error. This can help determine if the issue is specific to running the script remotely.

[3] Use the -Force Parameter**: When stopping a service with Stop-Service, you can use the -Force parameter to forcefully stop the service, which might help bypass any issues related to dependencies or conflicting operations.

[4] Check Permissions: Ensure that the user account running the PowerShell script has the necessary permissions to stop and start services on the system. You may need administrative privileges.

[5] Review System Event Logs : Check the system event logs on the EC2 instance for any relevant error messages or warnings that might provide additional insight into why the service stop operation is failing.

[6]Alternative Methods : If Stop-Service continues to fail, you can try alternative methods for stopping the service, such as using sc.exe or net stop commands directly. Make sure to include error handling in your script to capture any potential errors and debug the issue further.

Thanks and Regards, Damini

Damini
answered 8 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions