Automation

 View Only
  • 1.  Get-VM Not Working Immediately After VM Deployment

    Posted Jul 19, 2019 04:05 PM

    Hi I have been testing a script of mine that uses "Get-VM" via the -Name param (but I have also tested using UUID). I have noticed that on some occasions it takes a randomy amount of time (usually under 5 minutes) for the VM to be detectable by "Get-VM" causing my script to sometimes missing certains VMs.

    Am I missing something, or is there away to wait until the newly deployed VM is detectable? Thanks for the help.



  • 2.  RE: Get-VM Not Working Immediately After VM Deployment

    Posted Jul 19, 2019 04:10 PM

    You will have to give a bit more info, and preferably an example of what you see.
    What environment are you working with, a vCenter, a standalone ESXi node...?

    Btw, how do you do a Get-VM with the UUID?
    Or did you mean Id?



  • 3.  RE: Get-VM Not Working Immediately After VM Deployment

    Posted Jul 19, 2019 05:49 PM

    Hi LucD, thanks for the fast reply!

    I am working with a VCenter yes (I am on version 6.0). Forget about the ID/UUID, I was thinking of something else when I said that. I am using the VM name grabbed from the Event logs to try to get the VM with the -Name parameter.

    Most of the time given a VM Name it properly grabs the VM like its supposed to.

    In the case that it does not, it returns the following error:

    Get-VM : 2019-07-09 2:24:12 PM Get-VM VM with name 'myvm' was not found using the specified filter(s).

    At C:\Users\azuritekevin\Desktop\VM_Script\VM_Deploy.ps1:322 char:22

    +             $newVM = Get-VM -name $vmarg.vm.name

    +                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~

    If I run the command after maybe 4 or 5 minutes it will work though.



  • 4.  RE: Get-VM Not Working Immediately After VM Deployment

    Posted Jul 19, 2019 05:53 PM

    When you are connected (Connect-VIServer) to a vCenter, a New-VM will result in the new VM being registered in the vCenter.

    I'm not sure why the registration of the VM would take longer in some cases.
    It would help if you show how you do the New-VM.

    Are you perhaps using the RunAsync switch?

    You can inspect the events (with Get-VIEvent) to see when the VM creation and registration actually happened.

    And compare that to the time when your Get-VM failed and when it succeeded.



  • 5.  RE: Get-VM Not Working Immediately After VM Deployment

    Posted Jul 19, 2019 06:25 PM

    I am not using RunAsync

    The VMs are being created using the thick client or web client.

    Here is what I see when I inspect the logs.

    SrcTemplate          : VMware.Vim.VmEventArgument

    Template             : True

    Key                  : 126971613

    ChainId              : 126971484

    CreatedTime          : 7/9/2019 2:26:57 PM

    UserName             : AZADOMAIN\azuritekevin

    Datacenter           : VMware.Vim.DatacenterEventArgument

    ComputeResource      : VMware.Vim.ComputeResourceEventArgument

    Host                 : VMware.Vim.HostEventArgument

    Vm                   : VMware.Vim.VmEventArgument

    Ds                   :

    Net                  :

    Dvs                  :

    FullFormattedMessage : Template template_v1 deployed on host esx-23

    ChangeTag            :

    SrcTemplate          : VMware.Vim.VmEventArgument

    Template             : True

    Key                  : 126971487

    ChainId              : 126971484

    CreatedTime          : 7/9/2019 2:20:49 PM

    UserName             : AZADOMAIN\azuritekevin

    Datacenter           : VMware.Vim.DatacenterEventArgument

    ComputeResource      : VMware.Vim.ComputeResourceEventArgument

    Host                 : VMware.Vim.HostEventArgument

    Vm                   : VMware.Vim.VmEventArgument

    Ds                   :

    Net                  :

    Dvs                  :

    FullFormattedMessage : Deploying myvm on host esx-23 in L2 from template template_v1

    ChangeTag            :

    2:24:12  is the time my Get-VM ran, so after the second event. There is a 6:57 min between the first and second event. Maybe this is due to the time it takes for Hardware and OS Customization since this is deployed from a template?



  • 6.  RE: Get-VM Not Working Immediately After VM Deployment

    Posted Jul 19, 2019 06:47 PM

    The VM is deployed from a Template.
    Are you using a OSCustomizationSpec?
    Is the Task in the web client 100% complete before you do the Get-VM?
    Cloning might take a while, depending on the size of the Template and the performance of your storage.

    There should be more than those 2 events.
    A sample deployment from a Template in my lab produces

    19-Jul-19 20:42:31 VmBeingDeployedEvent      

    19-Jul-19 20:42:31 VmUuidAssignedEvent       

    19-Jul-19 20:42:31 VmInstanceUuidAssignedEvent

    19-Jul-19 20:42:59 VmReconfiguredEvent       

    19-Jul-19 20:42:59 VmDeployedEvent

    I generated the above with

    $vmName = 'NewVM'

    $start = (Get-Date).AddMinutes(-5)


    Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) |

    where { $_.Vm.Name -match "$vmName" } |

    Sort-Object -Property CreatedTime |

    Select CreatedTime, @{N = 'Event'; E = { $_.GetType().Name} }



  • 7.  RE: Get-VM Not Working Immediately After VM Deployment
    Best Answer

    Posted Jul 19, 2019 06:49 PM

    It looks like the deployment was only finished at 2:26:57, which is after your Get-VM.

    ---------------------------------------------------------------------------------------------------------

    Was it helpful? Let us know by completing this short survey here.