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.
Blog:Calling an Orchestrator Runbook using SMA (Good example of using SMA parameter data).
TechNet:Using Automation with Virtual Machine Clouds.
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 Runbook:
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 .
01 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 | workflow RenameVM { param ( [Parameter(Mandatory=$true)] [string] $vmmJobId, [Parameter(Mandatory=$true)] [object] $params, [Parameter(Mandatory=$true)] [object] $resourceObject ) #Prepare Environment $spfUrl = Get-AutomationVariable -Name "spfUrl" $spfCredentials = Get-AutomationPSCredential -Name "spfCredentials" $ADCred = Get-AutomationPSCredential -Name "ADCred" $stampId = $params.StampId $fullUrl = $spfUrl + "Jobs?`$filter=ID eq guid'" + $vmmJobId + "' and StampId eq guid'" + $stampId + "'" #Gather and Prepare Computer Name Information $VMName = $resourceObject.Name $initComputerName = $resourceObject.ComputerName $compNameSplit = $initComputerName.split('.') $ComputerName = $compNameSplit[0] #Sample Logging Write-Output " " Write-Output "SPF URL: $fullUrl" Write-Output " " Write-Output "Computer Name Initially Set To: $ComputerName" Write-Output "Computer Name Will Be Reset To: $VMName" Write-Output " " do { Start-Sleep -s 30 $Output = InlineScript { # Construct response to SPF. [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} $request = [System.Net.HttpWebRequest]::Create($Using:fullUrl) $request.Credentials = $Using:spfCredentials $request.Accept = "application/json" $request.Headers.Add("Accept-Charset", "UTF-8") $request.ContentType = "application/json" $request.Method = "GET" $response = $request.GetResponse() $requestStream = $response.GetResponseStream() $readStream = new-object System.IO.StreamReader $requestStream $outputJson = $readStream.ReadToEnd() $output = ConvertFrom-Json $outputJson $readStream.Close() $response.Close() $output } Write-Output "Current Progress: $($output.value.Progress)" } while (!$output.value.IsCompleted) Write-Output "Final Status: $($output.value.Status)" #Rename Routine If (!$ComputerName -or !$VMName ) { Write-Output "Computer Name Not Defined, No Change Will Occur." } else { Inlinescript { $i = 1 $newComputerName = $Using:VMName Do { $checkComp = Get-ADComputer -LDAPFilter "(Name=$newComputerName)" if (!$checkComp) { Rename-Computer -ComputerName $Using:ComputerName -NewName $newComputerName -DomainCredential $Using:ADCred -Restart } else { $newComputerName = $newComputerName + $i $i ++ } } until ((!$checkComp)) } } } |
The Breakdown:
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.
Conclusion:
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.