I have a VM build script I built a couple of years ago, and it works pretty well. However, the company I am currently contracting with has a large number of different domains that a VM could be joined to. If I want to do this the traditional way with a customization spec, I would need one for each domain. They also keep their templates in the Content Library to keep them in synch between all sites. So I am playing around with simply getting the Library item and building the VM from there, based off a nonpersistent customization spec I create on the fly from the info provided in a csv. Below is what I am testing with:
$aHosts = @( "MyHost0001", "MyHost0002" ) $creds = Get-Credential Connect-VIServer -Server $aHosts -Credential $creds $newVM = "JLoTest1" $domain = "my.test.domain.com" $name = "mtdc" $key = "ABCDE-FGHIJ-KLMNO-PQRST-UVWXY" $description = "Custom Spec Created by JLo" $IP = "192.168.1.120" $netMask = "255.255.255.0" $gateWay = "192.168.1.1" $DNS1 = "192.168.1.80" $DNS2 = "192.168.1.81" $template = "WS_2012_R2_DE" $localAdminPW = "myLocalPassword!" $VLAN = "dvPortGroup-VLAN-22-192-168-1-0-24" $sDStore = Get-Datastore -Name "OK_P_LUN002_ISO" $sFolder = Get-Folder -Name "Testing" -Server "MyHost0001" $sCluster = Get-Cluster -Name "CXO Production" New-OSCustomizationSpec -Name $newVM ` -Type NonPersistent ` -Domain $domain ` -NamingScheme vm ` -Description $description ` -ChangeSid: $true ` -AutoLogonCount 2 ` -TimeZone 035 ` -DomainCredentials $creds ` -DnsServer $DNS1, $DNS2 ` -DnsSuffix $domain ` -FullName $name ` -OrgName $name ` -OSType Windows ` -ProductKey $key ` -LicenseMode PerSeat ` -AdminPassword $localAdminPW | Out-Null Get-OSCustomizationSpec -Name $newVM | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP ` -IpAddress $IP ` -SubnetMask $netMask ` -DefaultGateway $gateWay ` -Dns $DNS1, $DNS2 | Out-Null Start-Sleep 5 Get-ContentLibraryItem -Name $template | New-VM -Name $newVM ` -Datastore $sDStore ` -ResourcePool $sCluster ` -Location $sFolder ` -DiskStorageFormat Thin | Out-Null Start-Sleep 5 Get-VM -Name $newVM | Get-NetworkAdapter -Name "Network adapter 1" | Set-NetworkAdapter -Portgroup $VLAN -Confirm: $false | Out-Null Start-Sleep 5 Get-VM -Name $newVM | Set-VM -MemoryGB 8 ` -NumCpu 4 ` -OSCustomizationSpec $newVM ` -Confirm: $false | Out-Null Start-Sleep 5 Get-VM -Name $newVM | Start-VM | Out-Null Disconnect-VIServer -Server * -Confirm:$false Remove-Variable * -ErrorAction SilentlyContinue; $error.Clear()
The script is bugging me, because it keeps throwing the following:
New-VM : 12/3/2018 3:39:44 PM New-VM The object 'vim.ClusterComputeResource:domain-c7' has already been deleted or has not been completely created
At line:63 char:21
+ New-VM -Name $newVM `
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-VM], ManagedObjectNotFound
+ FullyQualifiedErrorId : Client20_QueryServiceImpl_RetrievePropertiesEx_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM
New-VM : 12/3/2018 3:39:45 PM New-VM com.vmware.vapi.std.errors.not_found {'messages': [com.vmware.vapi.std.localizable_message {'id': com.vmware.vdcs.cls-main.library_item_not_found,
'default_message': Library item df549b8e-0fb0-4abc-9c3b-b1847f540b37 not found., 'args': [df549b8e-0fb0-4abc-9c3b-b1847f540b37]}], 'data':}
At line:63 char:21
+ New-VM -Name $newVM `
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-VM], ViError
+ FullyQualifiedErrorId : VICore_VMServiceImpl_DeployFromLibraryItem_Error,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM
It seems like it is unable to find the Library item. The thing that gets me is the script runs just fine; the VM is built and reconfigured as I would expect, the customization spec is attached, and the VM goes through the customization process and joins the proper domain. I just cannot figure out why it is throwing an error. I tried adding the -Server parameter to the Get-ContentLibraryItem command, thinking that it might be that I am connecting to an array of VIServers, but this did not resolve the issue.