2 Replies Latest reply on Dec 7, 2017 11:27 PM by LucD

    Getting the folderID of a folder to pass over to New-VM

    COS Master

      So I have my script below that works fine....

      New-VM -Name $DestinationVM -VM $SourceVM -Datastore (Get-VM $SourceVM | Get-Datastore | Select-Object -ExpandProperty name -First 1) -Location (Get-Folder -ID (Get-VM $SourceVM).FolderId) -DiskStorageFormat thin -VMHost (Get-VMHost -VM $SourceVM)


      Clones a VM into the same datastore and folder as the source VM. No problem there.


      Now, what I need is to change it up to put the VM's it copies into a folder called "ColdClones" which is in the same folder the VM's are in.


      I thought the below would give me the FolderID of the folder called "ColdClones"...

      Get-Folder -ID (Get-VM 'ColdClones').FolderId


      .....but all it returns is this....

      Name                   Type
      ----                   ----
      ColdClones            VM


      How can I have the New-VM command put the VM copies into the folder "ColdClones".



        • 1. Re: Getting the folderID of a folder to pass over to New-VM
          mattboren Master

          Hello, -


          You can have New-VM put the new VMs into the "ColdClones" folder much in the same way that your first example works:  by specifying the folder object for the -Location parameter.


          The object that was returned by your short Get-Folder example _is_ a Folder object (if you pipe your short Get-Folder example to Get-Member, you'll see the TypeName of VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl -- a Folder object).  It's just that the default format for the Folder object when returned to the PowerShell console is that it displays just those two properties:  Name and Type


          So, you could use your short Get-Folder example to get the folder of a VM named "ColdClones" (which is apparently a folder also named "ColdClones in your environment), or you could just use a call to Get-Folder with the name of the destination folder, like:


          ## specify the -Location folder value by getting the folder object by name

          New-VM -Name $DestinationVM -VM $SourceVM -Datastore (Get-VM $SourceVM | Get-Datastore | Select-Object -First 1) -Location (Get-Folder -Name ColdClones) -DiskStorageFormat thin -VMHost (Get-VMHost -VM $SourceVM)


          How does that do for you?

          • 2. Re: Getting the folderID of a folder to pass over to New-VM
            LucD Guru
            User ModeratorsvExpertCommunity Warriors

            Matt's solution is ok, provided you only have one VM folder named 'ColdClones'.

            To solve that issue, your original idea was the correct one, use the FolderId of your source VM.


            Since you didn't specify if your $SourceVM contained a string or a VirtualMachine object, I added that first line (saves on some Get-VM).


            Note that I used splatting to make the code more readable (for humans).


            $SourceVM = Get-VM -Name $SourceVM

            $sNew = @{

                Name = $DestinationVM

                VM = $SourceVM

                Datastore = Get-Datastore -RelatedObject $SourceVM | Select-Object -First 1

                Location = Get-Folder -Id $SourceVM.FolderId

                DiskStorageFormat = [VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.VirtualDiskStorageFormat]::Thin

                VMHost = Get-VMHost -VM $SourceVM


            New-VM @sNew

            Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz