Azure Virtual Machines Management Samples - Node.js

This sample demonstrates how to manage your Azure virtual machines using a node.js client and specifically how to:

  • Create a virtual machine
  • Start a virtual machine
  • Stop a virtual machine
  • List virtual machines
  • Delete a virtual machine.

On this page

Run this sample

  1. If you don't already have it, get node.js.

  2. Clone the repository.

    git clone
  3. Install the dependencies.

    cd compute-node-manage-vm
    npm install
  4. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  5. Set the following environment variables using the information from the service principle that you created.

    export AZURE_SUBSCRIPION_ID={your subscription id}
    export CLIENT_ID={your client id}
    export APPLICATION_SECRET={your client secret}
    export DOMAIN={your tenant id as a guid OR the domain name of your org <>}

    [AZURE.NOTE] On Windows, use set instead of export.

  6. Run the sample.

    node index.js
  7. To clean up after index.js, run the cleanup script.

    node cleanup.js <resourceGroupName> <websiteName>

What does index.js do?

The sample creates, lists, restarts and deletes virtual machines. It starts by logging in using your service principal.

msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain, function (err, credentials))

The sample then creates a virtual machine and displays information about it.

createVM(function (err, result));

computeClient.virtualMachines.get(resourceGroupName, vmName, function (err, result))

Next, the sample stops and then starts the virtual machine created.

computeClient.virtualMachines.powerOff(resourceGroupName, vmName, function (err, result))
computeClient.virtualMachines.start(resourceGroupName, vmName, function (err, result))

And then lists all virtual machines under the current subscription.

computeClient.virtualMachines.listAll(function (err, result))

Finally, the code in the file 'cleanup.js' deletes the virtual machine created, as well as the resource group.

deleteVirtualMachine(function (err, result))
deleteResourceGroup(function (err, result))

More information

Please refer to Azure SDK for Node for more information. Additionally, here some other helpful links:

If you don't have a Microsoft Azure subscription you can get a FREE trial account here.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact with any additional questions or comments.