Search code examples
azurecloudazure-virtual-machineazure-sdk-python

Creating Azure VirtualMachineExtension failure


I have a Windows Machine that I want to add VM extension using the azure python SDK , I send the following request

{'location': 'westus', 
'tags': None, 
'publisher': 'Microsoft.Compute', 
'virtual_machine_extension_type': 'CustomScriptExtension', 
'type_handler_version': '1.4', 
'settings': '{
"file_uris": ["https://mysite.azurescripts.net/ps_enable_winrm_http.ps1"],
 "command_to_execute": "powershell -ExecutionPolicy Unrestricted -file ps_enable_winrm_http.ps1"}'
}

but what happens is that it gives the following exception

configure virtual_machine '946b4246-a604-4b01-9e6a-09ed64a93bdb' failed with this error : 
VM has reported a failure when processing extension '13da0dc5-09c0-4e56-a35d-fdbc42432e11'.
Error message: "Invalid handler configuration. Exiting. 
Error Message: Expecting state 'Element'.. Encountered 'Text'  with name '', namespace ''. "

More information on troubleshooting is available at https://aka.ms/VMExtensionCSEWindowsTroubleshoot 

adding a simple code snippet that I use

vm_extension_name = "{0}".format(uuid4())
vm_extension_params = {
    'location': location_val,
    'tags': tags_val,
    'publisher': 'Microsoft.Compute',
    'virtual_machine_extension_type': 'CustomScriptExtension',
    'type_handler_version': type_handler_version,
    'auto_upgrade_minor_version': True,
    'settings': json.dumps({
        'fileUris': file_uris,
        'commandToExecute': command_to_execute
     })
}
logger.info("sending {0}".format(vm_extension_params))

any ideas , should I send something differently or am I missing something from the above request that cause the issue

thanks for the help in advance

Regards,


Solution

  • When we use python sdk to install custom script extension, we should create Object VirtualMachineExtension. Its parameter settings should be Object. But you define it as str. Please update it with removing ''. For more details, please refer to the document

    For example

    from azure.mgmt.compute import ComputeManagementClient
    from azure.common.credentials import ServicePrincipalCredentials
    AZURE_TENANT_ID= ''
    AZURE_CLIENT_ID=''
    AZURE_CLIENT_SECRET='' 
    AZURE_SUBSCRIPTION_ID=''
    
    credentials = ServicePrincipalCredentials(client_id=AZURE_CLIENT_ID,secret=AZURE_CLIENT_SECRET,tenant=AZURE_TENANT_ID)
    compute_client = ComputeManagementClient(credentials, AZURE_SUBSCRIPTION_ID)
    
    resource_group_name='stan'
    vm_name='win2016'
    params_create = {
            'location':'CentralUS',
            'tags': None,
            'publisher': 'Microsoft.Compute',
            'virtual_machine_extension_type': 'CustomScriptExtension',
            'type_handler_version': '1.4',
            'settings':
            {
                'fileUris': ['https://***/test/test.ps1'],
                'commandToExecute': 'powershell -ExecutionPolicy Unrestricted -File test.ps1'
            }
     }
    
    ext_poller = compute_client.virtual_machine_extensions.create_or_update(
        resource_group_name,
        vm_name,
        'test',
        params_create,
    )
    ext = ext_poller.result()
    print(ext)
    

    enter image description here