VMware Cloud Community
JLogan2016
Enthusiast
Enthusiast

Error when building a VM from a Library Item

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.

0 Kudos
13 Replies
LucD
Leadership
Leadership

Does the same happen on all clusters on all vCenters?

And I assume that the Get-ContentLibraryItem by itself returns a valid object?


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

0 Kudos
JLogan2016
Enthusiast
Enthusiast

I did confirm that it is returning the correct item, yes. And yes, it is happening in all vCenters I have tried thus far. I tried previously specifying -server, thinking the issue was that it is returning multiple objects since the ovfs will be named the same, but got the same error. I thought about trying to select the library item that matches the $_.ContentLibrary field, but as they are all named very similarly I am still working on coming up with a consistent match that does not return multiple objects.

0 Kudos
LucD
Leadership
Leadership

Which PowerShell and PowerCLI version are you using?


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

0 Kudos
JLogan2016
Enthusiast
Enthusiast

5.1 and 10.1.0, respectively

0 Kudos
LucD
Leadership
Leadership

I'm afraid I don't see any obvious issues or possible causes for the behaviour you are seeing.

Perhaps best to open a SR, because this looks like a possible "issue".

PowerCLI is supported and does not require you to have a Developer Support contract.

See PowerCLI Support Breakdown


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

0 Kudos
JLogan2016
Enthusiast
Enthusiast

I will try that, thanks.

Funny enough, I did some renaming on my Content Libraries to incorporate the site name. So now, when I run the code like this:

    try {

        $t = Get-ContentLibraryItem -Name $LibItem |

                Where {$_.ContentLibrary -match $Location} |

                    New-VM -Name $VM.Name `

                           -Server $vCenter `

                           -Datastore $dStore `

                           -ResourcePool $Cluster `

                           -Location $destFolder `

                           -DiskStorageFormat Thin | Out-Null

it runs just fine, build the VM with no errors. However, it does not attach the customization spec. Very strange.

0 Kudos
JLogan2016
Enthusiast
Enthusiast

So I opened an SR for this, and received the response back:

I see that you have opened up a case with us about a custom PowerCLI script that you are using within your environment. I just wanted to let you know that the Production Support Agreement does not cover troubleshooting custom scripting utilizing PowerCLI.

VMware has a separate contract available (PowerCLI / SDK) that is available to customers who would like to purchase extended scripting support fur use within their environments.  I could provide you more information about that contract if you are interested.

Alternatively, I would suggest copying that error into the PowerCLI communities forum - there are a lot of avid power CLI customers out there who continuously share about the errors and issues they run into on specific builds, and it is a very active community, so you will likely get a reply there from soemone else who is running into the same issue.
 

0 Kudos
LucD
Leadership
Leadership

There we go again ....
Let me see if I can trigger someone

Update: you can already point GSS to the blog post I referenced earlier


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

0 Kudos
LucD
Leadership
Leadership

Did you mention, or refer to, that blog post about PowerCLI Support?
If the issue is with a cmdlet, and not your script, they should take your SR.


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

0 Kudos
JLogan2016
Enthusiast
Enthusiast

I did, my response was to include the link not only to this discussion and the blog post you pointed me to. I am waiting for a response.

Update: The response I received states:

The way I understand it, the cmdlet works and executes the correct function but throws an error. Is there any functionality from PowerCLI that is not working as intended? I.e. any features or cmdlets that you are attempting to run that are not correctly performing the task that they are supposed to do?

I responded that I would think a cmdlet throwing a critical error while apparently running as intended would qualify as "not correctly performing the task that they are supposed to do", but the TSE I have gotten with this ticket seems desperate not to own this issue.

The way I understand your original request, the cmdlet works and executes the correct function (creating a new VM, attaching the template from the content library, etc) but throws a generic error.

Is there any functionality from PowerCLI that is not working as intended? I.e. any features or cmdlets that you are attempting to run that are not correctly performing the task that they are supposed to do?

0 Kudos
LucD
Leadership
Leadership

I think you might have gotten a VMTN PM (see the @ sign at the top of the screen) asking for the SR#.
Can you check and reply, they might be able to help you further.


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

0 Kudos
JLogan2016
Enthusiast
Enthusiast

Thanks, I have not seen a PM yet but will keep an eye out for it.

0 Kudos
maxweca
Contributor
Contributor

Hey JLogan2016,

I was running into the same thing. I would see errors in vSphere when applying a template pulled by name onto the pipeline from a content library since it tried to pull from every content library. I used your idea of filtering the results and had success with the following:

... where-object {$_.ContentLibrary.Name -eq "INTENDED_CONTENT_LIBNAME"} ...

0 Kudos