Azure/azure-sdk-for-python

azure vm password reset using python

vaishnavav99 opened this issue · 4 comments

I am trying to reset azure vm (both windows and linux) password using python as part of automation.Does azure.mgmt.compute module support VM password rotation. If so please help me with a basic snippet.

when ever I try to do :

compute_client.virtual_machines.begin_update( resource_group_name, vm_name, { 'osProfile': { 'adminPassword': new_password } } ).wait()

Password is getting reset but am not able to login with old or new password

Thanks for the feedback, we’ll investigate asap.

msyyc commented

Add @ChenxiJiang333 for help on this issue

Hi @vaishnavav99 You can use the following code to reset the password.
For linux:

from azure.mgmt.compute.models import VirtualMachineExtension
response = client.virtual_machine_extensions.begin_create_or_update(
    resource_group_name={resource_group_name}, 
    vm_name={vm_name}, 
    vm_extension_name='enablevmaccess', 
    extension_parameters=VirtualMachineExtension(
        location={location}, 
        protected_settings={'username': {username}, 'password': {password}}))

For Windows:

response = client.virtual_machine_extensions.begin_create_or_update(
    resource_group_name={resource_group_name}, 
    vm_name={vm_name}, 
    vm_extension_name='enablevmaccess', 
    extension_parameters=VirtualMachineExtension(
        location={location}, 
        publisher='Microsoft.Compute',
        type_properties_type='VMAccessAgent', 
        type_handler_version='2.4', 
        protected_settings={'password': {password}}, 
        settings={'userName': {username}}))

Hi @ChenxiJiang333 ,
Thanks, it worked for me.

Thanks @xiangyan99 @msyyc .