Azure Automation VM Start and Stop in Python runbook

%3CLINGO-SUB%20id%3D%22lingo-sub-189916%22%20slang%3D%22en-US%22%3EAzure%20Automation%20VM%20Start%20and%20Stop%20in%20Python%20runbook%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-189916%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20are%20trying%20to%20do%20VM%20Start%20and%20Stop%20by%20using%20Python.%20And%20we%20did%20for%20single%20VM%20and%20we%20tested%20it%20in%20Azure%20Portal%2C%20It%20was%20successfully%20done%20both%20start%20and%20stop.%20Now%20we%20have%20to%20do%20for%20Multiple%20VMS%2C%20start%20and%20stop.%20Can%20anyone%20please%20help%20us%20to%20do%20for%20multiple%20VMS%2C%20start%20and%20stop%20in%20Python.%20Please%20find%20the%20attached%20script%20for%20the%20single%20VM.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Esource%20from%20-%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fautomation%2Fautomation-first-runbook-textual-python2%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fautomation%2Fautomation-first-runbook-textual-python2%3C%2FA%3E%3C%2FP%3E%3CP%3EPython%20Script%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3Eimport%3C%2FSPAN%3E%3CSPAN%3E%20os%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eimport%3C%2FSPAN%3E%3CSPAN%3E%20traceback%3C%2FSPAN%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3Eimport%3C%2FSPAN%3E%3CSPAN%3E%20os%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eimport%3C%2FSPAN%3E%3CSPAN%3E%20sys%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Efrom%3C%2FSPAN%3E%3CSPAN%3E%20azure.mgmt.compute%20%3C%2FSPAN%3E%3CSPAN%3Eimport%3C%2FSPAN%3E%3CSPAN%3E%20ComputeManagementClient%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eimport%3C%2FSPAN%3E%3CSPAN%3E%20azure.mgmt.resource%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eimport%3C%2FSPAN%3E%3CSPAN%3E%20automationassets%3C%2FSPAN%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3Edef%3C%2FSPAN%3E%3CSPAN%3E%20get_automation_runas_credential(runas_connection)%3A%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Efrom%3C%2FSPAN%3E%3CSPAN%3E%20OpenSSL%20%3C%2FSPAN%3E%3CSPAN%3Eimport%3C%2FSPAN%3E%3CSPAN%3E%20crypto%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eimport%3C%2FSPAN%3E%3CSPAN%3E%20binascii%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Efrom%3C%2FSPAN%3E%3CSPAN%3E%20msrestazure%20%3C%2FSPAN%3E%3CSPAN%3Eimport%3C%2FSPAN%3E%3CSPAN%3E%20azure_active_directory%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eimport%3C%2FSPAN%3E%3CSPAN%3E%20adal%3C%2FSPAN%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23%20Get%20the%20Azure%20Automation%20RunAs%20service%20principal%20certificate%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Ecert%20%3D%20automationassets.get_automation_certificate(%3C%2FSPAN%3E%3CSPAN%3E%22AzureRunAsCertificate%22%3C%2FSPAN%3E%3CSPAN%3E)%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Epks12_cert%20%3D%20crypto.load_pkcs12(cert)%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Epem_pkey%20%3D%20crypto.dump_privatekey(crypto.FILETYPE_PEM%2Cpks12_cert.get_privatekey())%3C%2FSPAN%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23%20Get%20run%20as%20connection%20information%20for%20the%20Azure%20Automation%20service%20principal%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eapplication_id%20%3D%20runas_connection%5B%3C%2FSPAN%3E%3CSPAN%3E%22ApplicationId%22%3C%2FSPAN%3E%3CSPAN%3E%5D%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Ethumbprint%20%3D%20runas_connection%5B%3C%2FSPAN%3E%3CSPAN%3E%22CertificateThumbprint%22%3C%2FSPAN%3E%3CSPAN%3E%5D%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Etenant_id%20%3D%20runas_connection%5B%3C%2FSPAN%3E%3CSPAN%3E%22TenantId%22%3C%2FSPAN%3E%3CSPAN%3E%5D%3C%2FSPAN%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23%20Authenticate%20with%20service%20principal%20certificate%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eresource%20%3D%3C%2FSPAN%3E%3CSPAN%3E%22%3CA%20href%3D%22https%3A%2F%2Fmanagement.core.windows.net%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmanagement.core.windows.net%2F%3C%2FA%3E%22%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eauthority_url%20%3D%20(%3C%2FSPAN%3E%3CSPAN%3E%22%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flogin.microsoftonline.com%2F%3C%2FA%3E%22%3C%2FSPAN%3E%3CSPAN%3E%2Btenant_id)%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Econtext%20%3D%20adal.AuthenticationContext(authority_url)%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Ereturn%3C%2FSPAN%3E%3CSPAN%3E%20azure_active_directory.AdalAuthentication(%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Elambda%3C%2FSPAN%3E%3CSPAN%3E%3A%20context.acquire_token_with_client_certificate(%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eresource%2C%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eapplication_id%2C%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Epem_pkey%2C%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Ethumbprint)%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E)%3C%2FSPAN%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23%20Authenticate%20to%20Azure%20using%20the%20Azure%20Automation%20RunAs%20service%20principal%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Erunas_connection%20%3D%20automationassets.get_automation_connection(%3C%2FSPAN%3E%3CSPAN%3E%22AzureRunAsConnection%22%3C%2FSPAN%3E%3CSPAN%3E)%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eazure_credential%20%3D%20get_automation_runas_credential(runas_connection)%3C%2FSPAN%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23%20Initialize%20the%20compute%20management%20client%20with%20the%20RunAs%20credential%20and%20specify%20the%20subscription%20to%20work%20against.%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Ecompute_client%20%3D%20ComputeManagementClient(%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eazure_credential%2C%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Estr%3C%2FSPAN%3E%3CSPAN%3E(runas_connection%5B%3C%2FSPAN%3E%3CSPAN%3E%22SubscriptionId%22%3C%2FSPAN%3E%3CSPAN%3E%5D)%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E)%3C%2FSPAN%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23%20Resource%20Group%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eresource_group_name%20%3D%20%3C%2FSPAN%3E%3CSPAN%3Estr%3C%2FSPAN%3E%3CSPAN%3E(sys.argv%5B%3C%2FSPAN%3E%3CSPAN%3E1%3C%2FSPAN%3E%3CSPAN%3E%5D)%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Evm_name%20%3D%20%3C%2FSPAN%3E%3CSPAN%3Estr%3C%2FSPAN%3E%3CSPAN%3E(sys.argv%5B%3C%2FSPAN%3E%3CSPAN%3E2%3C%2FSPAN%3E%3CSPAN%3E%5D)%3C%2FSPAN%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23%20Start%20the%20VM%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eprint%3C%2FSPAN%3E%3CSPAN%3E(%3C%2FSPAN%3E%3CSPAN%3E'%5CnStart%20VM'%3C%2FSPAN%3E%3CSPAN%3E)%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Easync_vm_start%20%3D%20compute_client.virtual_machines.start(resource_group_name%2C%20vm_name)%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Easync_vm_start.wait()%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20the%20below%20mentioned%20screen%20shot%2C%20if%20we%20enter%20the%20multiple%20parameter%2FVM%20name%2C%20it%20is%20taking%20only%20the%20first%20name%20(parameter%2FVM%20name)%20Please%20Suggest.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22AzureVM.JPG%22%20style%3D%22width%3A%20699px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F33494iB76AC2025E0EB4FC%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22AzureVM.JPG%22%20alt%3D%22AzureVM.JPG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-189916%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20Automation%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epython%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Erunbooks%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Estartvm%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Estopvm%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Deleted
Not applicable

Hello ,

 

We are trying to do VM Start and Stop by using Python. And we did for single VM and we tested it in Azure Portal, It was successfully done both start and stop. Now we have to do for Multiple VMS, start and stop. Can anyone please help us to do for multiple VMS, start and stop in Python. Please find the attached script for the single VM.

 

source from - https://docs.microsoft.com/en-us/azure/automation/automation-first-runbook-textual-python2

Python Script: 

 

import os
import traceback

import os
import sys
from azure.mgmt.compute import ComputeManagementClient
import azure.mgmt.resource
import automationassets

def get_automation_runas_credential(runas_connection):
from OpenSSL import crypto
import binascii
from msrestazure import azure_active_directory
import adal

# Get the Azure Automation RunAs service principal certificate
cert = automationassets.get_automation_certificate("AzureRunAsCertificate")
pks12_cert = crypto.load_pkcs12(cert)
pem_pkey = crypto.dump_privatekey(crypto.FILETYPE_PEM,pks12_cert.get_privatekey())

# Get run as connection information for the Azure Automation service principal
application_id = runas_connection["ApplicationId"]
thumbprint = runas_connection["CertificateThumbprint"]
tenant_id = runas_connection["TenantId"]

# Authenticate with service principal certificate
authority_url = ("https://login.microsoftonline.com/"+tenant_id)
context = adal.AuthenticationContext(authority_url)
return azure_active_directory.AdalAuthentication(
lambda: context.acquire_token_with_client_certificate(
resource,
application_id,
pem_pkey,
thumbprint)
)


# Authenticate to Azure using the Azure Automation RunAs service principal
runas_connection = automationassets.get_automation_connection("AzureRunAsConnection")
azure_credential = get_automation_runas_credential(runas_connection)

# Initialize the compute management client with the RunAs credential and specify the subscription to work against.
compute_client = ComputeManagementClient(
azure_credential,
str(runas_connection["SubscriptionId"])
)

# Resource Group
resource_group_name = str(sys.argv[1])
vm_name = str(sys.argv[2])

# Start the VM
print('\nStart VM')
async_vm_start = compute_client.virtual_machines.start(resource_group_name, vm_name)
async_vm_start.wait()

 

In the below mentioned screen shot, if we enter the multiple parameter/VM name, it is taking only the first name (parameter/VM name) Please Suggest.

AzureVM.JPG

0 Replies