This is a blog article that touches on a few different configurations. Software Components, SovLabs LCT quick summary and Windows Drive Formatting via PowerShell. Hopefully it can save someone time in the future should a similar use-case be needed in the future.
Goal:
Configure my lab environment to execute PowerShell scripts in the Guest using both SovLabs LCT and vRealize Software Components. Secondary goal - validate it was possible to format drives in Windows guest that were added via "storage" tab during provisioning in response to CIBC POC issues.
Configuring Software Components/Installing the Guest Agent Service:
VMware white paper (https://docs.vmware.com/en/vRealize-Automation/7.5/com.vmware.vra.prepare.use.doc/GUID-C5BD3D30-FCCC-4125-971E-4E6A27617BF8.html)
Clone a Windows template in vCenter.
Power on the template.
Open browser to vRealize instance you want to use template in: https://vra75-01.sovlabs.net/software/
Download the "prepare_vra_template_windows.zip" (RIGHT CLICK and choose SAVE TARGET AS).
Execute the PowerShell script on the template.
Reference this article for screenshots: https://vmguru.com/2017/01/prepare-windows-for-vra-software-deployment/
Once done Power Down and Set VM back as template.
Note: This Process creates a trust via the certificate thumbprint from the vRA server. This runs as a service on the Windows guest. So the service(guest agent) is specific to the vRA instance specified.
Configuring Blueprint for Software Components:
Create a new Software Component. Design -> software Components - new.
Create a Name - choose container as "Machine" in vRA 7.4 and above.
In actions (menu 3), Paste the contents of the PowerShell script in "Configure" Lifecycle stage. Change the script Type to PowerShell.
Complete wizard and you should now have a software component created under the Design tab.
Clone a working Windows blueprint in vRA.
Run an Data Collection. Infrastructure -> Compute Resources -> Data Collection on Clusters the template was saved.
Edit the Blueprint, Map the new Software Component blueprint to clone from.
On the canvas then drag the software component onto the VM machine component. This will execute the Software Component at the Lifecycle staged defined.
Save the Blueprint. Make sure it's active and assign to a Service.
Then kick off a test build.
Note: Execution failures within the software component will cause the build to fail. You can see this in the history/events on the deployment in vRA. An example error below.
Note 2: If you misconfigured the Guest Agent on the template for the vRA being use it will show a timeout error:
Configuring SovLabs LCT to execute PowerShell on Windows post Machine Provisioning:
Follow our documentation for configuration. Quick summary below. http://docs.sovlabs.com/latest/vRA/7.5/modules/vra-toolkits/lifecycle-toolkit/setup/
Create a Lifecycle EBS (Manage Lifecycle EBS -> add) for when the PowerShell script should execute in the EBS. I chose '1770' to execute after other integrations. Make sure to make a Machine Building definition.
Add a LCT script Definition. I chose to use Vmtools connection (requires vCenter endpoint) and also choose "on provisioned VM" You should be select the Machine Building EBS you created above. Also paste in the PowerShell code you want to execute. Can choose to block on errors (not exit code 0) or ignore.
Create an LCT Profile. Select the LCT definition created.
Attach the LCT Profile Property group to a Windows blueprint.
Run a test build.
The script execution result can be found in vRO in the SovLabs -> Lifecycle Components Toolkit -> EBS Workflows.
Note: Keep in mind if this is set as blocking it can mark as complete but not actually work.
Note 2: Errors about authorization maybe from WinRM connection/Switch to Vmtools to test script execution. Or double check the credentials used on the Script Definition. I used local administrator for testing.
Test Validation Process:
Build from the Blueprints above. Add 2 additional drives in the storage tab when building. Validate there were no execution errors. A machine/ip address should be available under deployments.
Log into the Windows Guest OS. Open Disk Manager. Validate Disks are present. Open File explorer, 2 additional formatted disks (D: and E: should be formatted and listed as well as the CD rom being changed to X:).
The PowerShell Drive Script:
A copy was place here: \\isilon.sovlabs.net\windowsdev\drive_formatting_test.ps1
##############################################################################
# Drive_cfg_SQL2012
# Created by Adam Kardell
# Date: 6/4/2014
# Auto Initalize and Format SQL drives on 2012 Servers Only!!
# Requires PowerShell 4.0 or greater
# Formats Drives in Numberical Order based on Disk Number
# Updated 1/7/2019 for graceful exit.
###############################################################################
#Set variables for drive letters in need of formatting
$args = @("D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:Y:")
$driveCount = 0
$drives = $Null
$totalDrives = $Null
$drive = $Null
# Check For Unpartitioned Drives
$drives = gwmi Win32_diskdrive | where {$_.Partitions -eq "0"} | sort-object -property DeviceID
If ($drives -eq $Null)
{
Write-Host "* All Drives already Partitioned!"
exit 0
}
#Required custom attributes.
$DRVCFG_ORI = $args
# Check to make sure there are enough disks for the requested volumes
write-host "DRVCFG_ORI: " $DRVCFG_ORI
[array]$DRVCFG = $DRVCFG_ORI -split ':'
write-host "DRVCFG: " $DRVCFG
$Unpartitioned_Drive_Count = $drives.count
$Requested_Drive_Count = ($DRVCFG.count - 1)
Write-Host " - Number of unpartitoned volumes:" $Unpartitioned_Drive_Count
Write-Host " - Number of requested volumes :" $Requested_Drive_Count
if ($Requested_Drive_Count -gt $Unpartitioned_Drive_Count)
{
Write-Host "* The Number of drives requested is more than the available unpartitioned disks!"
Write-Host "* Volumes will still attempt to be created but some may be missing."
#exit 0
}
#Hash table for File System Label for each drive letter
$DriveHash = @{
"D" = "BINARIES"
"E" = "SQLData1"
"F" = "SQLData2"
"G" = "SQLData3"
"H" = "SQLData4"
"I" = "SQLBACKUPS"
"L" = "SQLTlogs"
"M" = "MSDTC"
"O" = "Snapshot"
"P" = "Paging"
"Q" = "Quorum"
"R" = "SSRS"
"S" = "SSAS"
"T" = "SQLTempdb"
"U" = "SQLTemplog"
"V" = "APP"
"W" = "AuditLog"
}
#hash table for allocation size per drive letter
$unit =@{
"D" = "65536"
"E" = "65536"
"F" = "65536"
"G" = "65536"
"H" = "65536"
"I" = "65536"
"L" = "65536"
"M" = "4096"
"O" = "4096"
"P" = "4096"
"Q" = "4096"
"R" = "65536"
"S" = "32768"
"T" = "65536"
"U" = "65536"
"V" = "4096"
"W" = "65536"
}
# Change the DVD Drive Letter to X:
Write-Host " - Changing DVD rom drive letter to X:"
(gwmi Win32_cdromdrive).drive | %{$a = mountvol $_ /l;mountvol $_ /d;$a = $a.Trim();mountvol x: $a}
#Main Function to Format unpartitioned drives
Foreach ($Drive in $DRVCFG){
if($DriveHash.ContainsKey("$Drive")){
$check = Get-Volume | Select DriveLetter
$check1 = $check.DriveLetter
If($check1 -contains $Drive){
Write-Host " - Drive $Drive Already Exists, Skipping"
}
elseif($check1 -notcontains $Drive){
$disk = Get-Disk | Where {$_.partitionstyle -eq 'raw'}
$active = $disk.Number | Select-object -First 1
if($active -eq $null){Write-Host "No more unpartioned disks for requested inputs: $DRVCFG, exiting!"
exit 0}
$label = $DriveHash.Get_Item("$Drive")
write-host "Drive Label is $label"
Write-Host "Active Disk Number is $active"
Initialize-Disk -Number $active -PartitionStyle MBR -PassThru
$newpartition = New-Partition -DiskNumber $active -AssignDriveLetter -UseMaximumSize
$newpartition | Format-Volume -FileSystem NTFS -NewFileSystemLabel $label -AllocationUnitSize $unit.Get_Item("$Drive") -Confirm:$false
$temp = Get-Volume -FileSystemLabel $label | Select DriveLetter
$temp2 = $temp.DriveLetter
$temp3 = [string]$temp2 + ":"
write-host "Temp Drive is $temp3"
$driveletter = [string]$drive + ":"
Write-Host "Correct Drive is $driveletter"
$change = Get-WmiObject -Class win32_volume -Filter "DriveLetter = '$temp3'"
Set-WmiInstance -input $change -Arguments @{DriveLetter=$driveletter}
}
}
}
#Display Drive Information once completed
foreach ($Drive in $DRVCFG){
$driveletter2 = [string]$drive + ":"
write-Host $driveletter2
fsutil fsinfo ntfsinfo $driveletter2
}
Write-Host "Completed Drive creation and removal of Everyone Group for the following: $DRVCFG"
0 Comments