Skip to content

Instantly share code, notes, and snippets.

@nshores
Created July 1, 2021 18:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nshores/49310716229584a725e9701fdce681b7 to your computer and use it in GitHub Desktop.
get_az_vm.py
#!/usr/bin/env python
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute import ComputeManagementClient
import json
from json2html import *
# Tenant ID for your Azure subscription
TENANT_ID = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
# Your service principal App ID
CLIENT = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
# Your service principal password
KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
# Your Azure Subscription ID
subscription_id = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
credentials = ServicePrincipalCredentials(
client_id=CLIENT,
secret=KEY,
tenant=TENANT_ID
)
result = []
# Create a Resource Management client
compute_client = ComputeManagementClient(credentials, subscription_id)
vm_list = compute_client.virtual_machines.list_all()
for vm in vm_list:
resource_group = vm.id.split("/")[4]
vm_name = vm.name
details = compute_client.virtual_machines.instance_view(resource_group, vm_name, expand='instanceView')
status = len(details.statuses) >= 2 and details.statuses[1]
if status and status.code == 'PowerState/running':
tagz = compute_client.virtual_machines.get(resource_group, vm_name).tags
if not tagz:
tagz = ""
admin = ''
if hasattr(details, 'os_profile'):
admin = details.os_profile.admin_username
row = {"ComputerName": vm_name, "ResourceGroup": resource_group, "Admin": admin,
"Status": status.code, "Tags": tagz}
result.append(row)
data = json.dumps(result)
html = json2html.convert(json=data)
print(html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment