VMware Cloud Community
mikelleen
Contributor
Contributor
Jump to solution

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

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

Tags (3)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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


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

View solution in original post

Reply
0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

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


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

mikelleen
Contributor
Contributor
Jump to solution

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!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


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

mikelleen
Contributor
Contributor
Jump to solution

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!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
mikelleen
Contributor
Contributor
Jump to solution

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!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
mikelleen
Contributor
Contributor
Jump to solution

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!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


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

Reply
0 Kudos
mikelleen
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos