Hello @Prem Jha - Thanks for reaching Microsoft QnA platform
Instead of relying completely on FirstLogonCommands, please consider using run command, which supports asynchronous execution, and this allows you to run commands after the VM is deployed, independent of the boot process.
Steps for Implementation:
- Enable the VM Agent
- Make sure the Azure VM Agent is enabled. It’s required for the Run Command feature to function properly.
- Use Run Command After VM Creation - Once the VM is up, use the Run Command API to execute your commands asynchronously:
from azure.mgmt.compute import ComputeManagementClient
# Assuming authentication and client setup are complete
compute_client = ComputeManagementClient(credentials, subscription_id)
# Run command example to open port 445
run_command_parameters = {
'script': [
"CMD /c netsh advfirewall firewall add rule name='RWPort445In' dir=in localport=445 action=allow protocol=TCP",
"CMD /c netsh advfirewall firewall add rule name='RWPort445Out' dir=out remoteport=445 action=allow protocol=TCP"
],
'parameters': [],
'run_as_user': '<your_username>', # Optional
'run_as_password': '<your_password>' # Optional
}
response = compute_client.virtual_machines.run_command(
resource_group_name='your_resource_group',
vm_name='your_vm_name',
parameters=run_command_parameters
)
please monitor Command Execution - You can check the execution results through the Run Command instance view to confirm success or investigate failures.
Best Practices :
- Ensure scripts are idempotent to support retries without breaking functionality.
- Avoid commands that require user interaction.
- Keep commands short and simple to prevent syntax errors.
- Redirect outputs and errors to logs for better troubleshooting.
Advantages of Using Run Command :
- Reduces dependency on FirstLogonCommands, minimizing risks of commands hanging.
- Enables modular command execution that’s easier to monitor and manage.
- Simplifies troubleshooting by decoupling your boot-time operations from post-deployment configuration.
Useful References :