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
Run it once with the domain flag... then look at the OScustSpec to see if it has changed to the value you have defined.
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
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
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
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.
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
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?
I don't see the DomainPassword property, didn't you specify that ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
As seen above I'm using the property: -domaincredentials $adcredentials
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
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
Did you try with a hardcoded admin password on the AdminPassword parameter ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I hard coded the password in -AdminPassword and it changed the local admin password to the hardcoded value.
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.