- Machine Prefix/Property Definition – Part 1
- Powershell – Part 2
- Workflow Configuration – Part 3
- Blueprint Update – Part 4
Review
In part 1, we configured the Machine Prefix, Property Dictionary and layout to accommodate our change. In part 2, we’ll create our Powershell script that will create the new machine name from our inputs.
Recall that for this example, the name should use the initials of the Business Group, “V” for Virtual, a single letter for the OS (“W” for Windows, “L” for Linux), a three character “role” and a two digit sequence number for uniqueness.
Example Naming convention:
BG1VWAPP14
BG1 = Business Group initials
V = Virtual
W = Windows
APP = APPlication server
14 = Sequence number
We’ll pass the following variables to the script during execution:
Name | contains |
---|---|
$originalName | The name originally created by vCAC based on the Machine Prefix (“BG1-14” in the example above) |
$vmwareOS | The value in the “VMware.VirtualCenter.OperatingSystem” property of the request (This is set when selecting the appropriate VMware O/S Property Set on the blueprint) |
$machineRole | The value that the user selected in the “Machine Role” dropdown that we configured in Part 1 (“APP” in the example above) |
Script
On the IaaS (or DEM Agent) server, save the following to “C:\scripts\CreateNewMachineName.ps1”:
$machineName=$originalName
write "Original name of the VM – $originalName" | out-file c:\scripts\nametest.txt
write "VMwareOS – $vmwareOS" | out-file c:\scripts\nametest.txt -append
if ($machineName -match "-"){
write "Name contains a dash" | out-file c:\scripts\nametest.txt -append
if ($vmwareOS -match "rhel") {$machineRole = "VL" + $machineRole}
if ($vmwareOS -match "sles") {$machineRole = "VL" + $machineRole}
if ($vmwareOS -match "sola") {$machineRole = "VS" + $machineRole}
if ($vmwareOS -match "wind") {$machineRole = "VW" + $machineRole}
$machineName=$machineName -replace "-", $machineRole}
write "machineRole – $machineRole" | out-file c:\scripts\nametest.txt -append
write "New Name – $machineName" | out-file c:\scripts\nametest.txt -append
$Global:PowerShellOutVar = $machineName
Line-by-line Explanation
Line Num | Code (should not wrap) | meaning |
---|---|---|
1 | $machineName=$originalName |
Set the $machineName variable to the value of the incoming variable $originalName |
2 | write “Original name of the VM – $originalName” | out-file c:\scripts\nametest.txt | Create the “C:\scripts\nametext.txt” file and write the value of the $originalName variable in it (just for validation/testing) |
3 | write "VMwareOS – $vmwareOS" | out-file c:\scripts\nametest.txt -append |
Append the value in the $vmwareOS variable to the text file (again, just for validation/testing) |
4 | if ($machineName -match "-"){ |
Begin a conditional loop if the $machineName variable contains a dash |
5 | write "Name contains a dash" | out-file c:\scripts\nametest.txt -append |
Append to the text file (again, just for validation/testing) |
6 | if ($vmwareOS -match "rhel") {$machineRole = "VL" + $machineRole} |
Nested Loop that prepends “VL” to the $machineRole value if the $vmwareOS variable contains “rhel” |
7 | if ($vmwareOS -match "sles") {$machineRole = "VL" + $machineRole} |
Nested Loop that prepends “VL” to the $machineRole value if the $vmwareOS variable contains “sles” |
8 | if ($vmwareOS -match "sola") {$machineRole = "VL" + $machineRole} |
Nested Loop that prepends “VS” to the $machineRole value if the $vmwareOS variable contains “sola” |
9 | if ($vmwareOS -match "wind") {$machineRole = "VL" + $machineRole} |
Nested Loop that prepends “VW” to the $machineRole value if the $vmwareOS variable contains “wind” |
10 | $machineName=$machineName -replace "-", $machineRole} |
Update the $machineName variable by replacing the dash with the string built above |
11 | write "machineRole – $machineRole" | out-file c:\scripts\nametest.txt -append |
Append the new $machineRole value to the text file (again, just for validation/testing) |
12 | write "New Name – $machineName" | out-file c:\scripts\nametest.txt -append |
Append the new $machineName value to the text file (again, just for validation/testing) |
13 | $Global:PowerShellOutVar = $machineName |
Create the PowerShellOutVar Global variable and assign it the value in $machineName |
More info
This is a pretty simple script. The line-by-line is mostly for my own benefit, so I remember what it’s supposed to do later. 🙂 If your scenario requires that the same workflow is used for provisioning other than Virtual, you’d want to verify that the $vmwareOS exists and perhaps follow some other execution path if it does not. You’ll probably want to comment-out the lines that write to the text file once you’re satisfied that it works as desired.