Once you have the code downloaded from above link then just create new runbook inside azureautomation account and copy that code to runbook as it is and start runbook . Once you click on start the runbook will prompt you for few details like workspace name, vm name, resource group name and then it will connect vm to OMS workspace.
This solution can only manage VMs that are in the same subscription as where the Automation account resides. but if in case you want to manage vm from another subscription then that is possible , you just need to add another subscription connect asset and have to add code to connect another subscription before get-azurermvm commands
This solution must need OMS installed in subscription
you must Need AzureRM.OperationalInsights module in azure automation account to perform this operation.