VMware Cloud Community
pargit
Enthusiast
Enthusiast

New-VM error when cloning

Hi

i have an automation script that get csv and deploy vm accordingly.

the script works ok but in the last few days i see the following error that the vm already exist.

my script uses the following command:
$taskTab[(New-VM -Name $VM.Name -Server $vcserver -ResourcePool (&{if($VM.alfa -eq 1) {$(Get-ResourcePool "alp-user-vm")} elseif($VM.alfa -eq 2) {$(Get-ResourcePool "alp-lab-user-vm")} else {$hostname}}) -Location $VM.Folder -Datastore $ds -Notes $notes -VM $source -OSCustomizationSpec $tempSpec -RunAsync -EA SilentlyContinue).Id] = $tid
Start-Sleep -s 5

i see that the vm start the clone, and after the sleep command i get this error (as there was another new-vm command running)

From the console output:
12-31-2023 14:43:22 Error:###Line 602: Error count=1
12-31-2023 14:43:22 Error:###Line 604: 12/31/2023 2:43:22 PM Get-VM VM with name 'vll-test2' was not found using the specified filter(s).
12-31-2023 14:43:22 Line 619: Setting DS Cluster Lab_DS_Cluster
12-31-2023 14:43:22 Line 622: Datastore for deploy = ENG_LAB_DS_08
@{Install=1; Division=1; PID=6884; ID=6985; Farm=7; Cluster=; Datastore=; Name=vll-test2; Boot=TRUE; OSType=Linux; Template=Linux-RedHat_84_lab; CustSpec=linux_config_lab; Folder=New Servers Lab; CPU=2; RAM=4; Disk1=; Disk2=; Disk3=; Disk4=; DiskStorageFormat=Thin; NetType=vDS; Network1=112; DHCP1=FALSE; IPAddress1=xxxxx; SubnetMask1=255.255.255.0; Gateway1=xxxx; Network2=xx; DHCP2=FALSE; IPAddress2=xxxx; SubnetMask2=255.255.255.0; Gateway2=xxxx; pDNS=xxxx; sDNS=xxxx; Notes=Template:Linux-RedHat_84_lab Ver. 06-18-2017; Sysinfo=בדיקות System Linux; Resp=מאיר בר; Prod=Dev/Test/Qa/Train; Goal=בדיקות תקינות של ה template; DMZ=0; alfa=0; cmdb=TRUE}
12-31-2023 14:43:22 Line 625: Deploying vll-test2
12-31-2023 14:43:22 Notes =
System: בדיקות System Linux
Manager: 
Zone: Dev/Test/Qa/Train
12-31-2023 14:43:22 Deploy VM...
12-31-2023 14:43:22 Line 652: Running: New-VM -Name vll-test2
12-31-2023 14:43:29 Line 656: Error count=1
12-31-2023 14:43:29 12/31/2023 2:43:24 PM New-VM VM or template with name 'vll-test2' already exists.
12-31-2023 14:43:29 Error:###Line 670: vll-test2 failed to deploy!
12-31-2023 14:43:29 Error:###Line 671: VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.DuplicateName: 12/31/2023 2:43:24 PM New-VM VM or template with name 'vll-test2' already exists.
at VMware.VimAutomation.ViCore.Impl.V1.Service.Vm.VmValidator.TryValidateUniqieVmName(String vmName, FolderInterop location)
at VMware.VimAutomation.ViCore.Impl.V1.Service.VmServiceImpl.CloneVm(String name, String description, VMHostInterop vmHost, VIContainerInterop resourceContainer, FolderInterop location, StorageResourceInterop storage, Nullable`1 diskStorageFormat, OSCustomizationSpecInterop spec, Boolean runAsync, VirtualMachineInterop vm, AdvancedOption[] options, Boolean linkedClone, Snapshot referenceSnapshot, KeyProviderInterop keyProvider, StoragePolicyInterop storagePolicy, ReplicationGroupInterop replicationGroup, Boolean skipHardDisks, Nullable`1 sevEnabled, Nullable`1 bootDelayMillisecond)
at VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM.DoWork(VIAutomation client, List`1 moList)
12-31-2023 14:43:29 Error:###Line 672:
12-31-2023 14:43:29 Error:###Line 674: 12/31/2023 2:43:24 PM New-VM VM or template with name 'vll-test2' already exists.
Sleeping 10 seconds

the server is deployed ok but i can't understand why i get the exception.

any help will be appreciated.

Thanks, Mordechai

0 Kudos
9 Replies
LucD
Leadership
Leadership

Looks like the New-VM is called more than once.
Then on the 2nd call you would get that error, while the 1st call has effectively created the VM.

But without seeing the code I cannot determine if and where that happens.


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

0 Kudos
pargit
Enthusiast
Enthusiast

i thought about it as well but the script run only one new-vm per line in the csv...

this is the main loop that start the new-vm process per csv line.

https://pastebin.com/8U36AVs7

0 Kudos
LucD
Leadership
Leadership

I noticed that your code checks if the VM already exists, but only in that specific cluster.
If that VM exists in another cluster, your code will not intercept that case.

For debugging this kind of code I would suggest using the Visual Studio Code Debug functionality.
I would start by placing a breakpoint on the line with the New-VM cmdlet.
When the code stops, check if a Get-VM returns anything, then hit F5 to continue, and so on.


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

0 Kudos
pargit
Enthusiast
Enthusiast

Hi,

i'm setting the cluster variable where i set the vcenter server and datastore and it's based on the os of the new vm.

    switch ($VM.Farm) {
    "2" {
                  If ($VM.OSType -eq "Windows") {
        $vcserver = $vcdrp
        #$cluster = "Ramle_Cluster_IT"
                            $cluster = "Ramle_Cluster_Windows"
        $datastore = "NetApp_Ram_DS_Cluster"
                            }
                        Else {
        $vcserver = $vcdrp
        #$cluster = "Ramle_Cluster_Linux"
                            $cluster = "Ramle_Cluster_Linux"
        $datastore = "NetApp_Ram_DS_Cluster"
                            }
    break
       }

as i wrote, the new-vm is running ok (i see the task running in the vc) but after the sleep i get the new-vm error that it's exist. can't understand why it tries again..

0 Kudos
LucD
Leadership
Leadership

Is there perhaps a duplicated line in your input file?


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

0 Kudos
pargit
Enthusiast
Enthusiast

i double check the script again, there is only 1 new-vm command...

in the log i see that at 14:43:22 there was a search for existing vm and nothing there (i even checked before in the vc itself)

12-31-2023 14:43:22 Error:###Line 604: 12/31/2023 2:43:22 PM Get-VM VM with name 'vll-test2' was not found using the specified filter(s).

 

the error is after 2 seconds 2:43:24 is from the "$Error.Exception" variable...

12-31-2023 14:43:29 Error:###Line 671: VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.DuplicateName: 12/31/2023 2:43:24 PM New-VM VM or template with name 'vll-test2' already exists.

so 2 seconds after the new-vm (which runs ok) i get the error of existing vm. it's like the vc get the new-vm command but hangs for few seconds and then "see" the new vm as existing..

thanks

 

0 Kudos
LucD
Leadership
Leadership

Did you check if you have more than 1 VCSA connection open?
What is in $global:defaultVIServers?


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

0 Kudos
pargit
Enthusiast
Enthusiast

i added a log file to write what is in $global:defaultVIServers

also, for your question if there is another vcsa connection open, my script support multi vc connection as every command targets a specific vc name (using the -server argument) so every line in the csv will be pointed to the correct vc.

also, i have naming convention for each vc so the chance that there will be another vm in another vc with the same name is very rare.

0 Kudos
LucD
Leadership
Leadership

I'm out of ideas, but if this were a script of mine I would run it through the debugger in VSC and use breakpoints to see what is happening.


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

0 Kudos