Update Sunday August 16th 2015 - URL in script has changed. I have updated the script here leaving the legacy URL in place but commented out.
Windows Azure Pack and Service Management Automation are on fire. Having spent a little bit of time working with WAP, one of the first items that I found needing my own personal adjustment was to the Computer Names given to my VM’s after a Stand Alone Virtual Machine deployment. More to the point, if you deploy a Standalone VM, giving it a VM Name of VM001, you will end up with a Computer Name more like ZB0NLHY74MPYW0. However, I want my VM’s to have a common VM Name and Computer Name. That is if I name my VM VM001, I want my computer name to also be VM001.
Fortunately with the tight integration between Service Management Automation and Windows Azure Pack we are provided with a platform on which to automate post VM deployment tasks such as renaming a computer. In this blog I will be sharing a sample solution that demonstrates how to use an SMA Runbook to dynamically rename the computer to match the name chosen for the VM.
So to recap my intent here:
Issue: When deploying a standalone VM using the Windows Azure Pack a random computer name is given to the VM regardless of the VM name.
Desired Outcome: to have the VM Name and the Computer Name of the VM match after deployment with no manual intervention. I also want to ensure that this name is not currently is use, and adjust accordingly if it is.
Solution: Create a SMA Runbook that will consume the given VM Name, and once deployed, rename the VM’s Computer Name to match the VM Name.
Before We Get Started:
There are several core items to WAP VM automation that I will not be covering (like how to ‘link’ the automation to the VM creation process). Below is some content that can be used for ramp up on those core components.
Blog: SMA Capabilities in Depth.
TechNet: Global Resource or Assets.
Sample Runbook: I've used the Sample-Using-VMCloud-Automation which is included with SMA as a starting point for this Runbook. Be sure to examine this Runbook as it is very helpful reference for this post VM deployment type activities.
The sample Runbook in this post rely on a few SMA Assets. The following have been created and configured in support of the Runbook.
ADCred: this contains the credentials of an account with access to rename a computer.
spfUrl: Variable that contains the SPF endpoint URL. This URL should be similar to https://spf_end_point:8090/SC2012R2/VMM/Microsoft.Management.Odata.svc/ .
spfCredentials: this contains the credentials of an account with access to the SPF Endpoint.
A copy of this Runbook can be found here - Runbook .
Line 6 and 13 – initializing data. Note that in initializing $resourceObject we are consuming or loading several pieces of valuable data from the VM creation process such as the current VM Name ($resourceObject.Name) and Computer Name ($resourceObject.ComputerName).
Line 19 - Here I am consuming an SMA Credential asset ($ADCred) that is populated with credentials with the appropriate access to perform a computer rename.
Line 24 - 27 - when retrieving $resourceObject.ComputerName the value is returned as ComputerName.Domain. Here I am splitting off the 'Dot-Domain Name' and placing only the Computer Name into a new variable.
Line 30 - 35 - Sample logging. This is visible on the job output in Windows Azure Pack Admin Portal.
Line 37 - 64 - This chunk was taken from the Sample-Using-VMCloud-Automation Runbook that is included with SMA. The code is retrieving the current state of the VM deployment, looping until the VM deployment has completed, outputting progress during each loop. This code allows us to hold off on any post deployment routines (such as a computer rename) until the VM has been successfully deployed.
Line 67 - 68 - validating that values have been populated for the VMName and Computer Name.
Line 72 - 89 - Rename routine, note here that I am using straight PowerShell encapsulated in an Inlinescript. In order to ensure that a computer does not already exist with the intended name, this routine checks AD for any matches to that of $resourceObject.Name (or the VM Name). If none exist the computer is then renamed. If the intended name is in use, the variable is incremented with a numeric value, and AD is checked again. This occurs in a Do / Until loop until no match is found at which time the computer is renamed..
Line 77 and 82 - note that because I am working from within an inlineScript I am referring to the variables here using a $Using variable. More information can be found at this blog by Jim Britt .
The Runbook is configured to execute anytime a Standalone VM is created as seen below.
NOTE: In this configuration only Stand Alone VM deployments are effected. The Runbook will not execute when deploying a VM Role. These will inherit Computer Name's from the Gallery Item pattern. This is a good thing..
Finally once completed not only will your new VM have a Computer Name that matches the VM Name, we can also observe the logging in the job output.
While this was a simple project demonstrating the integration between Windows Azure Pack and Service Management Automation, I found it to be very helpful in terms of understanding the integration, understanding how to work with SMA, and really just a simple yet practical introduction to the two products. I am looking forward to a continuing my ramp up on these products and will continue to share significant examples here. Subscribe to my twitter feed using the button found on this page for up to date notification as these posts occur.
Thanks for reading.