10 Replies Latest reply on Aug 22, 2011 10:06 AM by mikelleen

    How to specify existing vhd file when creating a new VM using existing vhd on datastore?

    mikelleen Novice

      I am new to PowerCLI and finding it quite powerfule, however in the documentation I am reading, I am not seeing how to use an existing vhd on a datastore when creating a new VM.

       

      Is this simply registering the vhd, or am I missing something?

       

      My current create VM statement is as follows:

      New-VM

       

      –Name $TargetHostName –Host $TargetVMCenterHost -Datastore $TargetVMCenterDatastore -DiskMB 20000 –MemoryMB $TargetHostMemoryMB –NumCPU $TargetHostCPUs –GuestID $TargetHostOS –CD –Description $TargetHostName

       

        • 1. Re: How to specify existing vhd file when creating a new VM using existing vhd on datastore?
          LucD Guru
          User ModeratorsCommunity WarriorsvExpert

          That will create your VM with 1 virtualdisk of 20GB.

          If you want to add more virtualdisks to your VM you can use the New-Harddisk cmdlet after your New-VM cmdlet.

          Something like this

           

          New-HardDisk -VM (Get-VM $TargetHostName) -CapacityKB 10000 -Persistence persistent

           

          or you can use the pipeline ('|') like this

           

          New-VM –Name $TargetHostName –Host $TargetVMCenterHost `
          -Datastore $TargetVMCenterDatastore -DiskMB 20000 `
          –MemoryMB $TargetHostMemoryMB –NumCPU $TargetHostCPUs `
          –GuestID $TargetHostOS –CD –Description $TargetHostName | `
          New-HardDisk -CapacityKB 10000 -Persistence persistent
          1 person found this helpful
          • 2. Re: How to specify existing vmdk file when creating a new VM using existing vhd on datastore?
            mikelleen Novice

            Thanks for the help, I think my initial header is wrong and misleading.

             

            What I want to do is  create a VM that attaches to or is created from an existing COPY of a vmdk file.

             

            I think I need to use the DiskPath parameter in the New-VM command line.

             

            Sorry, about the original post being incorrectly titled.

             

            So am I correct in assuming I need to use the DiskPath parameter of the New-VM command?

             

            If so how do I correctly determine the full path in the datastore for the VM and put that into a variable? I'm looking into this now, but any help would be appreciated.

             

            Thanks from a newbie!

            • 3. Re: How to specify existing vmdk file when creating a new VM using existing vhd on datastore?
              LucD Guru
              vExpertCommunity WarriorsUser Moderators

              Ok, I see.

              You can indeed add/attach an existing virtualdisk to a new VM with the DiskPath parameter.

              Such a diskpath is constructed as follows

              [datastorename] folder\file.vmdk

              The datastorename between square brackets, followed by a blank.

              Then the folder name, which is most of the time the displayname of the VM that created the virtualdisk.

              And finally the filename of the .vmdk file.

               

              To create a new VM from an existing definition, you will need the .vmx file, not the .vmdk file.

              In that case you use the VMFilePath parameter.

              See my VMX Raiders Revisited post for more on that subject.

              1 person found this helpful
              • 4. Re: How to specify existing vmdk file when creating a new VM using existing vhd on datastore?
                mikelleen Novice

                Thanks for the help!

                 

                It looks like I  am getting a path error, my assumption is I am making a simple mistake in my statement:

                 

                PS C:\Documents and Settings\---snip---..VCENTER> Echo $TargetVMDKDiskPath
                TEST-VM-Vol6 Mustard\Mustard.vmdk


                PS C:\Documents and Settings\---snip---..VCENTER> New-VM -Name $TargetHostName -Host $TargetVMCenterHost -Datastore $TargetVMCenterDatastore -MemoryMB $TargetHostMemoryMB -NumCPU $TargetHostCPUs -GuestID $TargetHostOS -DiskPath $TargetVMDKDiskPath -Description $TargetHostName


                New-VM : 8/22/2011 10:00:07 AM    New-VM        The disk path "TEST-VM-Vol6 Mustard\Mustard.vmdk" is invalid.
                At line:1 char:7

                 

                 

                I also tried the following:

                 

                PS C:\Documents and Settings\---snip---.VCENTER> Echo $TargetVMDKDiskPath
                \TEST-VM-Vol6\Mustard\Mustard.vmdk


                PS C:\Documents and Settings\---snip---.VCENTER> New-VM -Name $TargetHostName -Host $TargetVMCenterHost -Datastore $TargetVMCenterDatastore -MemoryMB $TargetHostMemoryMB -NumCPU $TargetHostCPUs -GuestID $TargetHostOS -DiskPath $TargetVMDKDiskPath -Description $TargetHostName


                New-VM : 8/22/2011 10:06:04 AM    New-VM        The disk path "\TEST-VM-Vol6\Mustard\Mustard.vmdk" is invalid.
                At line:1 char:7

                 

                Any ideas on what I am doing wrong in my path?

                 

                Thanks!

                • 5. Re: How to specify existing vmdk file when creating a new VM using existing vhd on datastore?
                  LucD Guru
                  vExpertUser ModeratorsCommunity Warriors

                  It looks as if you don't have the square brackets around the datastorename.

                  • 6. Re: How to specify existing vmdk file when creating a new VM using existing vhd on datastore?
                    mikelleen Novice

                    Thanks for your help, I appreciate you spending time to look this over.

                     

                    I tired the following with the same error:

                     

                    PS C:\Documents and Settings\---snip---.VCENTER> Echo $TargetVMDKDiskPath
                    [TEST-VM-Vol6] Mustard\Mustard.vmdk


                    PS C:\Documents and Settings\---snip---..VCENTER> New-VM -Name $TargetHostName -Host $TargetVMCenterHost -Datastore $TargetVMCenterDatastore -MemoryMB $TargetHostMemoryMB -NumCPU $TargetHostCPUs -GuestID $TargetHostOS -DiskPath $TargetVMDKDiskPath -Description $TargetHostName


                    New-VM : 8/22/2011 10:28:25 AM    New-VM        The disk path "[TEST-VM-Vol6] Mustard\Mustard.vmdk" is invalid.
                    At line:1 char:7
                    + New-VM <<<<  -Name $TargetHostName -Host $TargetVMCenterHost -Datastore $TargetVMCenterDatastore -MemoryMB $TargetHostMemoryMB -NumCPU $TargetHostCPUs -GuestID $TargetHostOS -DiskPath $TargetVMDKDiskPath -Description $TargetHostName

                     

                    I also tired it with the back slashes in the path with the same issus:

                    PS C:\Documents and Settings\--snip---.VCENTER> Echo $TargetVMDKDiskPath
                    \[TEST-VM-Vol6]\Mustard\Mustard.vmdk

                     

                    I'm sure this is something simple in my path statement, just not sure what, I am trying other various combinations, do you see anything that looks wrong?

                     

                    Thanks!

                    • 7. Re: How to specify existing vmdk file when creating a new VM using existing vhd on datastore?
                      LucD Guru
                      vExpertCommunity WarriorsUser Moderators

                      Didn't notice that before, but I think vSphere uses a forward-slash instead of a backward slash in the paths.

                      • 8. Re: How to specify existing vmdk file when creating a new VM using existing vhd on datastore?
                        mikelleen Novice

                        Cool! That worked, thanks!

                         

                        I am looking over your blog referenced in the post above as well.

                         

                        One more quick and simple (I hope) question:

                         

                        What is the difference between creating the VM from the vmdk and vmx?

                         

                        In my example, I am using the vmdk, as well as indicating number of CPU's, memory,etc. However you indicate that using the vmx file will bring over the properties of the copied vm?

                         

                        In my case I am using a copy of the original production datastore that I copied and renamed test-datastorename. Obviously to refresh my test environment.

                         

                        Can you give me a quick note on the benefiets of creating the VM from the vmx instead of the vmdk?

                         

                        Thanks for your help! It is very much appreciated!

                        • 9. Re: How to specify existing vmdk file when creating a new VM using existing vhd on datastore?
                          LucD Guru
                          User ModeratorsvExpertCommunity Warriors

                          To be clear, neither "creates" a VM.

                          With the .vmx file you register an existing VM in the vCenter. The registration takes the required information from the .vmx file and creates a new entry in the vCenter.

                          With a .vmdk file, you don't create a VM. You include that existing virtualdisk in a new VM that you define (#cpu, size memory...).

                          The difference is that with a .vmx file you don't have to give the peticulars of the VM, those are taken from the .vmx file.

                          • 10. Re: How to specify existing vmdk file when creating a new VM using existing vhd on datastore?
                            mikelleen Novice

                            OK thanks! I appreciate your help this is good information for a new user like me.