VMware Cloud Community
aaronmiller85
Enthusiast
Enthusiast

Domain not adding in Build script

I'm writing a script to help build some servers, below is a switch statement that asks if you want a workgroup or domain. If domain, specify the Domain and AD credentials.

If i run through it and specify a domain it still creates the VM as a workgroup. Once I log in I use the same credentials specified in the script to add it to the domain. Am I missing something here, or is my logic flawed?

$title = "Domain or Workgroup"

$message = "Will this server be on a domain?"

$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", ""

$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", ""

$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

$result = $host.ui.PromptForChoice($title, $message, $options, 0)

switch ($result)

    {

    0 {

        $domaintrue=1

        $domainname = read-host "Enter the domain name. Ex. domain.local, you will be prompted for domain credentials"

        $adcredentials = get-credential

      }

    1 {

        $domaintrue=0

      }

    }

function CreateTemplatespec

{

    if($domaintrue -eq 0)

    {

        write-host " workgroup"

        New-OSCustomizationSpec -Type nonpersistent -Name "Tempspec" -OSType Windows -Description "Temp scripted Spec $description" -FullName "IBM" -OrgName "Name" -NamingScheme Fixed -NamingPrefix $computername -AdminPassword         $localadmincreds.GetNetworkCredential().password -TimeZone 035 -ChangeSid -workgroup "WORKGROUP" -AutoLogonCount 0 

        Get-OSCustomizationSpec Tempspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IPAddress $NetIP -SubnetMask $NetMask -DefaultGateway $NetGW -DNS $NetDNS 

    }

     else

    {

        write-host "the domain is $domainname" -foreground "yellow"   

        New-OSCustomizationSpec -Type nonpersistent -Name "Tempspec" -OSType Windows -Description "Temp scripted Spec $description" -FullName "IBM" -OrgName "Name" -NamingScheme Fixed -NamingPrefix $computername -AdminPassword $localadmincreds.GetNetworkCredential().password -TimeZone 035 -ChangeSid -domain $domainname -domaincredentials $adcredentials -AutoLogonCount 0 

        Get-OSCustomizationSpec Tempspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IPAddress $NetIP -SubnetMask $NetMask -DefaultGateway $NetGW -DNS $NetDNS 

    }

}

New-VM -Name $displayname -Location $vclocation -ResourcePool $cluster -Datastore $datastore -Template $sourcetemplate -OSCustomizationspec Tempspec -HARestartPriority $haprio -DiskStorageFormat Thick

0 Kudos
14 Replies
vFantastic
Enthusiast
Enthusiast

Run it once with the domain flag... then look at the OScustSpec to see if it has changed to the value you have defined.

0 Kudos
LucD
Leadership
Leadership

You seem to have placed the logic to set up the OSCustomizationSpec in the function CreateTemplatespec, but I don't see where you call this function.


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

0 Kudos
aaronmiller85
Enthusiast
Enthusiast

Sorry, left that bit out of the snippet...

CreateTemplateSpec | out-null

New-VM -Name $displayname -Location $vclocation -ResourcePool $cluster -Datastore $datastore -Template $sourcetemplate -OSCustomizationspec Tempspec -HARestartPriority $haprio -DiskStorageFormat Thick

remove-OSCustomizationSpec Tempspec -confirm:$false | out-null

UpdateVMSettings | out-null

0 Kudos
LucD
Leadership
Leadership

And are you sure the nonpresistent OSCustomizationSpec has actually been updated when you reach the NewVM cmdlet ?

Could be that PowerShell scoping plays a role here.


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

0 Kudos
aaronmiller85
Enthusiast
Enthusiast

So I changed it to persistent and just tried to create a VM manually with the customization. When I run through to the summary page it displays the following below. I notice no domain information is mentioned on it. I then went back and hit the 'Use the customization wizard to temporarily adjust the specification before deployment' check box, and in the summary page. The image for it is at the bottom. I'm not sure if this is evidence of anything or normal behavior as I dont have a lot of experience here.

withoutedit.jpg

edit.jpg

0 Kudos
LucD
Leadership
Leadership

Did you try displaying the content of the OSCustomizationSpec before and after the changes you are doing ?

Just to make sure your changes are accepted.

You can do that with

Get-OSCustomizationSpec -Name tempSpec | Select *


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

0 Kudos
aaronmiller85
Enthusiast
Enthusiast

Added that after the customization function is ran, and before it applies it to new-vm.

Same result, no domain added. Could this be because the DNS section is blank?

domain.jpg

0 Kudos
LucD
Leadership
Leadership

I don't see the DomainPassword property, didn't you specify that ?


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

0 Kudos
aaronmiller85
Enthusiast
Enthusiast

As seen above I'm using the property: -domaincredentials $adcredentials

0 Kudos
LucD
Leadership
Leadership

I saw that, but shouldn't the property be there ?

Could you try, just for the sake of it, with a hard-coded account and password ?


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

0 Kudos
aaronmiller85
Enthusiast
Enthusiast

I added the below properties on the OSC string and same results...still not adding to the domain. This is really starting to give me a headache ;(

New-OSCustomizationSpec -Type nonpersistent -Name "Tempspec" -OSType Windows -Description "Temp scripted Spec $description" -FullName "IBM" -OrgName "orgname" -NamingScheme Fixed -NamingPrefix $displayname -AdminPassword $localadmincreds.GetNetworkCredential().password -TimeZone 035 -ChangeSid -domain $domainfqdn  -AutoLogonCount 0  -DNSserver $ddns -domainusername "domainname\jj02800" -domainpassword "MyPassword"

        Get-OSCustomizationSpec Tempspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IPAddress $NetIP -SubnetMask $NetMask -DefaultGateway $NetGW -DNS $NetDNS  

0 Kudos
LucD
Leadership
Leadership

Did you try with a hardcoded admin password on the AdminPassword parameter ?


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

0 Kudos
aaronmiller85
Enthusiast
Enthusiast

I hard coded the password in -AdminPassword and it changed the local admin password to the hardcoded value.

0 Kudos
aaronmiller85
Enthusiast
Enthusiast

Well it appears this may actually be an issue with the template. After ditching it from frustration I tried just adding it to the domain manually and it again did not add to the domain with accurate credentials.

I dont really have an enviornment currently to test it on but I'm gonna go ahead and call this script piece working. Thanks again LucD for your help and assistance.

0 Kudos