VMware Cloud Community
knoble88
Contributor
Contributor

OS/NIC Customization Properties Not Applying

Hello I am looking for some help.

I am running vSphere 6.7 and am attempting to start a PowerShell script that will automatically set the custom properties of a Windows 10 VM after being cloned from a template and join it to our domain.  I have been trying to set the IP Address, Subnet Mask, Default Gateway, DNS and Naming Prefix.

the script I am running to set the properties is:

 

# Save the credential object with permission to join the domain.
$DomainCredentials = Get-Credential username@vcenter

# Clone the Spec adding the domain information.
$Spec = Get-OSCustomizationSpec 'Win10Template' |
    New-OSCustomizationSpec -Name 'tmp01' -Type Persistent |
    Set-OSCustomizationSpec -Domain mydomain.com `
        -DomainCredentials $DomainCredentials

#Change DNS Name? Trying to set it from the DNS Name of the template I cloned to a different name
$Spec = Set-OSCustomizationSpec -OSCustomizationSpec $Spec -NamingScheme Fixed -NamingPrefix 'win10vmTest'

# Update Spec with the new VLAN's IP information
Get-OSCustomizationNicMapping -OSCustomizationSpec $Spec |
    Set-OSCustomizationNicMapping -IPmode UseStaticIP `
        -IpAddress '192.168.1.1' `
        -SubnetMask '255.255.255.0' `
        -DefaultGateway '192.168.1.254' `
        -Dns '192.168.1.4','192.168.1.5'

# Get the VM
$VM = Get-VM -Name win10vmTest

# Shutdown guest to make change.
Stop-VMGuest -VM $VM -Confirm:$false

# Wait while guest shuts down
While ((Get-VM $VM).PowerState -ne 'poweredOff')
{
    Start-Sleep -Seconds 1
}
# Change network settings
$vDSPG = (Get-VDPortgroup -Name External-NIC)
$VM |
    Get-NetworkAdapter |
    Set-NetworkAdapter -Portgroup $vDSPG -Connected:$true -Confirm:$false |
    Out-Null
# Apply customization Spec to apply new network settings
$VM | Set-VM -OSCustomizationSpec $Spec -Confirm:$false |
    Start-VM

#Remove Old Customization Spec Because I get an error when I do NonPeristent
Get-OSCustomizationSpec -Name 'tmp01' | Remove-OSCustomizationSpec -Confirm:$false

 

 

when I run the script I get no errors whatsoever.  The VM shuts down, I see the reconfiguration task in vCenter go to 100% and the VM power back on with the same configurations as before.  I've checked the logs in C:\Windows\Temp and from what I can tell nothing seems to be failing; I don't see any errors. I can see where the custom settings are applied and everything but they don't seem to be saving to the VM.

 

Labels (2)
Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership

Did you check the OSCustomization logs, see KB2001932?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
knoble88
Contributor
Contributor

I did check them.  I can see that sysprep is applying the settings but I don't see any errors.  I am going to try again with a different template and see if maybe the current template I am trying to clone/configure from is the issue.

Reply
0 Kudos
knoble88
Contributor
Contributor

Alright,

After a few weeks of tinkering around I finally have a baseline that works.  The script clones a VM from the template, gives it an IP Address, Renames and joins it to the domain.

The only issue I am having now is if I add '-AsSecureString' to the domain password field, the VM won't join to the domain.

Is there another way to go about securing the password so the VM will still join to the domain?

Here is my code:

#Prompt For User Info
$dUser = Read-host 'What is the Domain Username?'
$dPwd = Read-Host 'Password: '
$lPwd = Read-Host 'Password for Local Admin Account: ' 

#Create OS Customization Spec
Get-OSCustomizationSpec -Name Spec | Remove-OSCustomizationSpec
$TestSpec = New-OSCustomizationSpec -Name Spec `
	-OSType Windows `
	-Domain domain.com `
	-DomainUsername $dUser `
	-DomainPassword $dPwd `
	-Description "This spec adds a computer in a domain." `
	-FullName C2 `
	-OrgName DevOps `
	-AutoLogonCount 10 `
	-TimeZone 010 `
	-AdminPassword $lPwd `
    -ChangeSid

#Create Network Configs for Customization Spec
Get-OSCustomizationNicMapping -OSCustomizationSpec $TestSpec |
    Set-OSCustomizationNicMapping -IPmode UseStaticIP `
        -IpAddress 'IPHere' `
        -SubnetMask 'SubnetHere' `
        -DefaultGateway 'GatewayHere' `
        -Dns 'DNS1','DNS2'

#Choose Template, Datastore & Cluster
$vmTemplate = Get-Template -Name TemplateName
$vmCluster = Get-Cluster -Name "ClusterName"
$vmDatastore = "DataStoreName"

#Create VM
New-VM -Name VMName -Template $vmTemplate -ResourcePool $vmCluster -Datastore $vmDatastore -DiskStorageFormat Thin

#Apply Spec and Power On VM
$VM = Get-VM -Name VMName
$VM | Set-VM -OSCustomizationSpec $TestSpec -Confirm:$false | Start-VM
Reply
0 Kudos
LucD
Leadership
Leadership

Not with a PowerCLI cmdlet afaik.

You will have to use the encryptionKey property of the CustomizationSpec
You can first create the OSCustomizationSpec with PowerCLI, then use the GetCustomizationSpec, update the required fields (key & password), followed by an OverwriteCustomizationSpec to push your new settings.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos