VMware Cloud Community
zenivox
Hot Shot
Hot Shot
Jump to solution

VM customization partially not working through PowerCLI and Web GUI

Hello I am banging my head on a VM deployment that is not fully working. I'm setting this script that didn't exist before in this environment. If I go through the web GUI and use the customization spec it is the same. Basically it all works but the computer join to the AD. Here's below the script. I have to create the computer object before joining to the domain. If I don't create the object in AD before the join it works fine, but I cannot allow that to happen. I thought that creating the computer object before hand would not be a problem... also I'm using the cmdlet New-ADComputer which is recommended in these cases. Any hint anyone?

 
Import-Module .\Choose-ADOrganizationalUnit.ps1
Import-Module ActiveDirectory

$csv = import-csv .\New_VMs.csv
$Template = 'TPL-TEST'
$vmhost = "esx IP"

 
# Choose a Datastore to deploy to
$Datastores = Get-VMHost $vmhost | Get-Datastore | Select-Object Name | Sort-Object Name
$i = 1
$Datastores | ForEach-Object{Write-Host $i":" $_.Name$i++}
$DSIndex = Read-Host "Enter a number ( 1 -" $Datastores.count ")"
$SDatastore = $Datastores[$DSIndex - 1].Name
Write-Host "You have selected the $SDatastore datastore" -ForegroundColor Green 
Start-Sleep -Seconds 2

# Folder Selection
Write-Host "Select which folder to place the VM in"
$IFOLDER = Get-Folder | Select-Object Name | Sort-Object Name
$i = 1
$IFOLDER | ForEach-Object{Write-Host $i":" $_.Name$i++}
$FSIndex = Read-Host "Select a Folder. Enter a number ( 1 -" $IFOLDER.Count ")"
$SFOLDER = $IFOLDER[$FSIndex - 1].Name
write-host "."
write-host "You picked the $SFOLDER folder" -ForegroundColor Green
Start-Sleep -Seconds 2
 
# Read Name, Network, vLAN ID from CSV
foreach($Row in $csv) {
    
    # create AD computer object in right OU in AD
    $OU = Choose-ADOrganizationalUnit
    New-ADComputer -Name $Name -SamAccountName $Name -Path $OU.DistinguishedName 
    # You can also change -template to -contentlibraryitem 
    #$vmhost = Get-Cluster $SCluster | Get-VMHost | Select-Object -First 1
    
    ## Create Customisation
    Get-OSCustomizationSpec -Name "STATIC-IP-csv" | New-OSCustomizationSpec -Name "TMP" -Type NonPersistent

    ## Set Network Properties
    $IP =  @{OScustomizationNicMapping = Get-OSCustomizationNicMapping -OSCustomizationSpec "TMP"}
    $IP.IPMode = "UseStaticIP"
    $IP.IPAddress = $Row.IP
    $IP.SubnetMask = $Row.SN
    $IP.DefaultGateway = $Row.GW 
    $IP.dns = $Row.DNS1,$Row.DNS2 
    Set-OSCustomizationNicMapping @ip

    ## Deploy VM
    New-VM -Name $Row.Name -Template $Template `
    -VMHost $vmhost `
    -Datastore $SDatastore `
    -Location  $SFOLDER `
    -OSCustomizationSpec "TMP" `
    -ErrorAction Stop 

    ## Power UP VM to complete customisation
    Start-VM $Row.Name
}
0 Kudos
1 Solution

Accepted Solutions
zenivox
Hot Shot
Hot Shot
Jump to solution

I found my mistakes.. I was setting the vNIC without using proper syntax and parameter. Many thanks Luc anyway for your time! Here the final working script:

 

# Necessary to import the AD browser module
Import-Module .\Choose-ADOrganizationalUnit.ps1
Import-Module ActiveDirectory

$conn = Read-Host "Do you want to connect to the vCenter server? [Y/N]"
if($conn.ToUpper() -eq "Y") {
$creds = Get-Credential
Connect-VIServer "vimwfpromsbvc.global.wfp.org" -Credential $creds
}

$csv = import-csv .\New_VMs.csv
$Template = 'TPL-TEST'
$vmhost = "10.11.30.39"


# Choose a Datastore to deploy to
$Datastores = Get-VMHost $vmhost | Get-Datastore | Select-Object Name | Sort-Object Name
$i = 1
$Datastores | ForEach-Object{Write-Host $i":" $_.Name; $i++}
$DSIndex = Read-Host "Enter a number ( 1 -" $Datastores.count ")"
$SDatastore = $Datastores[$DSIndex - 1].Name
Write-Host "You have selected the $SDatastore datastore" -ForegroundColor Green
Start-Sleep -Seconds 2

# Folder Selection
Write-Host "Select which folder to place the VM in"
$IFOLDER = Get-Folder | Select-Object Name | Sort-Object Name
$i = 1
$IFOLDER | ForEach-Object{Write-Host $i":" $_.Name; $i++}
$FSIndex = Read-Host "Select a Folder. Enter a number ( 1 -" $IFOLDER.Count ")"
$SFOLDER = $IFOLDER[$FSIndex - 1].Name
write-host "."
write-host "You picked the $SFOLDER folder" -ForegroundColor Green
Start-Sleep -Seconds 2

# Read Name, Network, vLAN ID from CSV
foreach($Row in $csv) {

# create AD computer object in right OU in AD
$OU = Choose-ADOrganizationalUnit
New-ADComputer -Name $Row.Name -SamAccountName $Row.Name -Path $OU.DistinguishedName

# You can also change -template to -contentlibraryitem
#$vmhost = Get-Cluster $SCluster | Get-VMHost | Select-Object -First 1

## Create Customisation
if (!(Get-OSCustomizationSpec -Name "TMP")){
Get-OSCustomizationSpec -Name "STATIC-IP-csv" | New-OSCustomizationSpec -Name "TMP" -Type NonPersistent
}
## Set Network Properties
$IP = @{OScustomizationNicMapping = Get-OSCustomizationNicMapping -OSCustomizationSpec "TMP"}
$IP.IPMode = "UseStaticIP"
$IP.IPAddress = $Row.IP
$IP.SubnetMask = $Row.SN
$IP.DefaultGateway = $Row.GW
$IP.dns = $Row.DNS1,$Row.DNS2
Set-OSCustomizationNicMapping @ip

## Deploy VM
New-VM -Name $Row.Name -Template $Template `
-VMHost $vmhost `
-Datastore $SDatastore `
-Location $SFOLDER `
-OSCustomizationSpec "TMP" `
-ErrorAction Stop

## Power UP VM to complete customisation
Start-VM $Row.Name
Wait-Tools -VM $Row.Name
$myVDPortGroup = Get-VDPortgroup | Where-Object{$_.VlanConfiguration -match "VLAN $($Row.vlan)"}
Get-VM -Name $Row.Name | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $myVDPortGroup.Name -Connected:$true -Confirm:$false

}

View solution in original post

0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

This resembles the procedure for restoring a computer.
Instead of joining the domain, did you try resetting the secure channel between the computer and the AD DC?

You could try the Reset-ComputerMachinePassword cmdlet or the netdom resetpw command.


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

0 Kudos
zenivox
Hot Shot
Hot Shot
Jump to solution

Thanks Luc for your input! You said that this resembles a computer restore... Why? 

0 Kudos
zenivox
Hot Shot
Hot Shot
Jump to solution

before I try your suggestion... can this be the cause? In the past those messages used to say that it will be removed in a future release, while here it says it is already deprecated. But the property is hardcoded in the permanent  customization..

WARNING: The 'DomainAdminUsername' property of OSCustomizationSpec type is deprecated. Use the 'DomainUsername' property instead.
WARNING: The 'DomainAdminPassword' property of OSCustomizationSpec type is deprecated. Use the 'DomainPassword' property instead.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Those are just warning, and in fact only stating that the name of those parameters will be changed.
You can ignore the warning, for now, or use the new parameter names.


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

When you restore an OS, the secure channel between the computer and the AD DCs will most probably be not working.
That secure channel is reset at regular times, resulting in a similar effect as what you seem to be experiencing.

The AD object is in a kind of "out of phase" with the OS on the computer.


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

0 Kudos
zenivox
Hot Shot
Hot Shot
Jump to solution

I realized that I was not changing the vlan anywhere even when trying from the web console. So from web console it works. Not from powercli. I get en error that I cannot read because it is covered by the progress bar:

zenivox_0-1605631781574.png

 

 

and also this:

zenivox_1-1605630520393.png

 

however in spite of the last error the vlan gets changed.

This is the script now:

# Necessary to import the AD browser module
Import-Module .\Choose-ADOrganizationalUnit.ps1
Import-Module ActiveDirectory

$conn = Read-Host "Do you want to connect to the vCenter server? [Y/N]"
if($conn.ToUpper() -eq "Y") {
$creds = Get-Credential
Connect-VIServer "vcenter" -Credential $creds
}

$csv = import-csv .\New_VMs.csv
$Template = 'TPL-TEST'
$vmhost = "esx ip"


# Choose a Datastore to deploy to
$Datastores = Get-VMHost $vmhost | Get-Datastore | Select-Object Name | Sort-Object Name
$i = 1
$Datastores | ForEach-Object{Write-Host $i":" $_.Name; $i++}
$DSIndex = Read-Host "Enter a number ( 1 -" $Datastores.count ")"
$SDatastore = $Datastores[$DSIndex - 1].Name
Write-Host "You have selected the $SDatastore datastore" -ForegroundColor Green
Start-Sleep -Seconds 2

# Folder Selection
Write-Host "Select which folder to place the VM in"
$IFOLDER = Get-Folder | Select-Object Name | Sort-Object Name
$i = 1
$IFOLDER | ForEach-Object{Write-Host $i":" $_.Name; $i++}
$FSIndex = Read-Host "Select a Folder. Enter a number ( 1 -" $IFOLDER.Count ")"
$SFOLDER = $IFOLDER[$FSIndex - 1].Name
write-host "."
write-host "You picked the $SFOLDER folder" -ForegroundColor Green
Start-Sleep -Seconds 2

# Read Name, Network, vLAN ID from CSV
foreach($Row in $csv) {

# create AD computer object in right OU in AD
$OU = Choose-ADOrganizationalUnit
New-ADComputer -Name $Row.Name -SamAccountName $Row.Name -Path $OU.DistinguishedName

# You can also change -template to -contentlibraryitem
#$vmhost = Get-Cluster $SCluster | Get-VMHost | Select-Object -First 1

## Create Customisation
if (!(Get-OSCustomizationSpec -Name "TMP")){
Get-OSCustomizationSpec -Name "STATIC-IP-csv" | New-OSCustomizationSpec -Name "TMP" -Type NonPersistent
}
## Set Network Properties
$IP = @{OScustomizationNicMapping = Get-OSCustomizationNicMapping -OSCustomizationSpec "TMP"}
$IP.IPMode = "UseStaticIP"
$IP.IPAddress = $Row.IP
$IP.SubnetMask = $Row.SN
$IP.DefaultGateway = $Row.GW
$IP.dns = $Row.DNS1,$Row.DNS2
Set-OSCustomizationNicMapping @ip

## Deploy VM
New-VM -Name $Row.Name -Template $Template `
-VMHost $vmhost `
-Datastore $SDatastore `
-Location $SFOLDER `
-OSCustomizationSpec "TMP" `
-ErrorAction Stop

## Power UP VM to complete customisation
Start-VM $Row.Name
Wait-Tools -VM $Row.Name
$myVDPortGroup = Get-VDPortgroup | Where-Object{$_.VlanConfiguration -match "VLAN $($Row.vlan)"}
$vnic = Get-VM -Name $Row.Name | Get-NetworkAdapter
Set-NetworkAdapter -NetworkAdapter $vnic -Portgroup $myVDPortGroup.Name -Confirm:$false

}

0 Kudos
zenivox
Hot Shot
Hot Shot
Jump to solution

my previous post was deleted, too many changes?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

No clue, feel free to post it again.

Btw, you can suppress the progress bar by running the following before calling New-VM

$ProgressPreference = 'SilentlyContinue'


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

0 Kudos
zenivox
Hot Shot
Hot Shot
Jump to solution

I found my mistakes.. I was setting the vNIC without using proper syntax and parameter. Many thanks Luc anyway for your time! Here the final working script:

 

# Necessary to import the AD browser module
Import-Module .\Choose-ADOrganizationalUnit.ps1
Import-Module ActiveDirectory

$conn = Read-Host "Do you want to connect to the vCenter server? [Y/N]"
if($conn.ToUpper() -eq "Y") {
$creds = Get-Credential
Connect-VIServer "vimwfpromsbvc.global.wfp.org" -Credential $creds
}

$csv = import-csv .\New_VMs.csv
$Template = 'TPL-TEST'
$vmhost = "10.11.30.39"


# Choose a Datastore to deploy to
$Datastores = Get-VMHost $vmhost | Get-Datastore | Select-Object Name | Sort-Object Name
$i = 1
$Datastores | ForEach-Object{Write-Host $i":" $_.Name; $i++}
$DSIndex = Read-Host "Enter a number ( 1 -" $Datastores.count ")"
$SDatastore = $Datastores[$DSIndex - 1].Name
Write-Host "You have selected the $SDatastore datastore" -ForegroundColor Green
Start-Sleep -Seconds 2

# Folder Selection
Write-Host "Select which folder to place the VM in"
$IFOLDER = Get-Folder | Select-Object Name | Sort-Object Name
$i = 1
$IFOLDER | ForEach-Object{Write-Host $i":" $_.Name; $i++}
$FSIndex = Read-Host "Select a Folder. Enter a number ( 1 -" $IFOLDER.Count ")"
$SFOLDER = $IFOLDER[$FSIndex - 1].Name
write-host "."
write-host "You picked the $SFOLDER folder" -ForegroundColor Green
Start-Sleep -Seconds 2

# Read Name, Network, vLAN ID from CSV
foreach($Row in $csv) {

# create AD computer object in right OU in AD
$OU = Choose-ADOrganizationalUnit
New-ADComputer -Name $Row.Name -SamAccountName $Row.Name -Path $OU.DistinguishedName

# You can also change -template to -contentlibraryitem
#$vmhost = Get-Cluster $SCluster | Get-VMHost | Select-Object -First 1

## Create Customisation
if (!(Get-OSCustomizationSpec -Name "TMP")){
Get-OSCustomizationSpec -Name "STATIC-IP-csv" | New-OSCustomizationSpec -Name "TMP" -Type NonPersistent
}
## Set Network Properties
$IP = @{OScustomizationNicMapping = Get-OSCustomizationNicMapping -OSCustomizationSpec "TMP"}
$IP.IPMode = "UseStaticIP"
$IP.IPAddress = $Row.IP
$IP.SubnetMask = $Row.SN
$IP.DefaultGateway = $Row.GW
$IP.dns = $Row.DNS1,$Row.DNS2
Set-OSCustomizationNicMapping @ip

## Deploy VM
New-VM -Name $Row.Name -Template $Template `
-VMHost $vmhost `
-Datastore $SDatastore `
-Location $SFOLDER `
-OSCustomizationSpec "TMP" `
-ErrorAction Stop

## Power UP VM to complete customisation
Start-VM $Row.Name
Wait-Tools -VM $Row.Name
$myVDPortGroup = Get-VDPortgroup | Where-Object{$_.VlanConfiguration -match "VLAN $($Row.vlan)"}
Get-VM -Name $Row.Name | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $myVDPortGroup.Name -Connected:$true -Confirm:$false

}

0 Kudos