VMware Cloud Community
mrbip20
Contributor
Contributor
Jump to solution

Powercli - Attaching existing vmdk to new vm [ help needed ]

Good morning All!

I'm having some troubles in order to attach an existing vmdk file ( which is not being used in any other vm ), into a new vm that doesn't have any disks drives on it, using powercli.

I'm trying to use the New-HardDisk cmdlet but I'm encountering the following errors:

If I try to get the disks from a specific file path. I see the following output ( I do get the disks thou 😞

PowerCLI C:\> Get-HardDisk -Datastore "DATA2-Test"

CapacityGB      Persistence                                                    Filename

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

500.000         Unknown              ...A2-Test] DATA2-W2k16/DATA2-W2k16_1.vmdk

200.000         Unknown              [DATA2-Test] DATA2-W2k16/DATA2-W2k16.vmdk

Get-HardDisk : 6/11/2019 7:01:43 AM    Get-HardDisk        File [DATA2-Test] was not found

At line:1 char:1

+ Get-HardDisk -Datastore DATA2-Test"

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

    + CategoryInfo          : NotSpecified: (:) [Get-HardDisk], FileNotFound

    + FullyQualifiedErrorId : Client20_DatastoreServiceImpl_ExecuteDatastoreSearch_ViError,VMware.VimAutomation.ViCore

   .Cmdlets.Commands.VirtualDevice.GetHardDisk

If I try to get a specific disk, the cmdlet returns it, but it gaves me an error as well:

PowerCLI C:\> $disk0 = Get-HardDisk -Datastore "DATA2-Test" -DatastorePath "[DATA2-Test] DATA2-W2k16/DATA2-W2k16.vmdk"

Get-HardDisk : 6/11/2019 7:13:00 AM    Get-HardDisk        File [DATA2-Test] was not found

At line:1 char:10

+ $disk0 = Get-HardDisk -Datastore "DATA2-Test" -DatastorePath "[DATA2-Test] DAT ...

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

    + CategoryInfo          : NotSpecified: (:) [Get-HardDisk], FileNotFound

    + FullyQualifiedErrorId : Client20_DatastoreServiceImpl_ExecuteDatastoreSearch_ViError,VMware.VimAutomation.ViCore

   .Cmdlets.Commands.VirtualDevice.GetHardDisk

When I check the variable, I do see the disk:

PowerCLI C:\> $disk0

CapacityGB      Persistence                                                    Filename

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

200.000         Unknown              [DATA2-Test] DATA2-W2k16/DATA2-W2k16.vmdk

PowerCLI C:\> $disk0.Filename

[DATA2-Test] DATA2-AH-W2k16/DATA2-W2k16.vmdk

However, even if I ignore the error, I'm having troubles to assign it to a new vm:

PowerCLI C:\> $vm = Get-VM DATA2-AH-W2k16-Test

PowerCLI C:\> New-HardDisk -vm $vm -DiskPath $disk0.Filename

New-HardDisk : 6/11/2019 7:19:04 AM    New-HardDisk        Invalid configuration for device '1'.

At line:1 char:1

+ New-HardDisk -vm $vm -DiskPath $disk0.Filename

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

    + CategoryInfo          : NotSpecified: (:) [New-HardDisk], InvalidDeviceSpec

    + FullyQualifiedErrorId : Client20_VirtualDeviceServiceImpl_AttachHardDisk_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.NewHardDisk

Any comments and help are really appreciated! Not sure if I'm missing a parameter or if I need to add the existing vmdk to the vm in a different way. I'm using vcenter server 6 and VMware PowerCLI 6.5 Release 1 build 4624819

Thanks in advance!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

No, you correct, the SCSI controller will be created.

I just tested with something similar to what you are trying to do.

The VM TestVM2 has no harddisk nor controller.

When I run the following the disk is added.

Note that TestVM1 is powered off.

$vm1 = Get-VM -Name TestVM1

$vm2 = Get-VM -Name TestVM2


$hd = Get-HardDisk -VM $vm1


New-HardDisk -VM $vm2 -DiskPath $hd.Filename

Is the harddisk working correctly for the 1st VM?

Is that first VM powered off?


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

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Are you sure

  • that datastore is available?
  • that file exists on that datastore?

You could have a look with the Web Client.

Goto Storage, select the datastore and then the Files tab.


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

0 Kudos
mrbip20
Contributor
Contributor
Jump to solution

Yep, the datastore exist, and the command actually brings me the file:

PowerCLI C:\> $disk0

CapacityGB      Persistence                                                    Filename

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

200.000         Unknown              [DATA2-Test] DATA2-W2k16/DATA2-W2k16.vmdk

PowerCLI C:\> $disk0.Filename

[DATA2-Test] DATA2-W2k16/DATA2-W2k16.vmdk

But when I use the add disk into a new vm that id doesn't have any disk attached, I get the following error:

PowerCLI C:\> New-HardDisk -vm $vm -DiskPath $disk0.Filename

New-HardDisk : 6/11/2019 7:19:04 AM    New-HardDisk        Invalid configuration for device '1'.

At line:1 char:1

+ New-HardDisk -vm $vm -DiskPath $disk0.Filename

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

    + CategoryInfo          : NotSpecified: (:) [New-HardDisk], InvalidDeviceSpec

    + FullyQualifiedErrorId : Client20_VirtualDeviceServiceImpl_AttachHardDisk_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.NewHardDisk

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you have the correct controller on that 2nd VM?


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

0 Kudos
mrbip20
Contributor
Contributor
Jump to solution

Nope! the vm is a newly created one with no disks attached, and the vmdk is not being used by any vm ( it's a copy ).

If I have to create a new scsi ( I thought that it was created by default according to what I read on vmware's kb ), I'm not quite sure the sintaxis, since when I checked the new-ScsiController, I didn't fully understand how needs to be used. Do I have to create it as part of the new-hardDrive sintaxis?

Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, you correct, the SCSI controller will be created.

I just tested with something similar to what you are trying to do.

The VM TestVM2 has no harddisk nor controller.

When I run the following the disk is added.

Note that TestVM1 is powered off.

$vm1 = Get-VM -Name TestVM1

$vm2 = Get-VM -Name TestVM2


$hd = Get-HardDisk -VM $vm1


New-HardDisk -VM $vm2 -DiskPath $hd.Filename

Is the harddisk working correctly for the 1st VM?

Is that first VM powered off?


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

0 Kudos
mrbip20
Contributor
Contributor
Jump to solution

Thanks for the reply LucD. The hard drive is a vmdk copied from another location, I don't have any other vm that is currently using it, so I use the Get-HardDisk with the -DatastorePath parameter, which brings me the vmdk into a variable ( with an error, but the variable is assigned 😞

PowerCLI C:\> Get-HardDisk -Datastore "NADTC2-Test" -DatastorePath "[NADTC2-Test] NADTC2-AH-W2k16/NADTC2-AH-W2k16.vmdk"

CapacityGB      Persistence                                                    Filename

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

200.000         Unknown              [DATA2-Test] DATA2-W2k16/DATA2-W2k16.vmdk

Get-HardDisk : 6/11/2019 7:12:19 AM    Get-HardDisk        File [DATA2-Test] was not found

At line:1 char:1

+ Get-HardDisk -Datastore "NADTC2-Test" -DatastorePath "[NADTC2-Test] NADTC2-AH-W2 ...

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

    + CategoryInfo          : NotSpecified: (:) [Get-HardDisk], FileNotFound

    + FullyQualifiedErrorId : Client20_DatastoreServiceImpl_ExecuteDatastoreSearch_ViError,VMware.VimAutomation.ViCore

   .Cmdlets.Commands.VirtualDevice.GetHardDisk

PowerCLI C:\> $disk0 = Get-HardDisk -Datastore "DATA2-Test" -DatastorePath "[DATA2-Test] DATA2-W2k16/DATA2-W2k16.vmdk"

Get-HardDisk : 6/11/2019 7:13:00 AM    Get-HardDisk        File [DATA2-Test] was not found

At line:1 char:10

+ $disk0 = Get-HardDisk -Datastore "DATA2-Test" -DatastorePath "[DATA2-Test] DAT ...

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

    + CategoryInfo          : NotSpecified: (:) [Get-HardDisk], FileNotFound

    + FullyQualifiedErrorId : Client20_DatastoreServiceImpl_ExecuteDatastoreSearch_ViError,VMware.VimAutomation.ViCore

   .Cmdlets.Commands.VirtualDevice.GetHardDisk

So even with the error that follows the assignation, when I check the variable, I do see the disk ( showing me the correct capacity 😞

PowerCLI C:\> $disk0

CapacityGB      Persistence                                                    Filename

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

200.000         Unknown              [DATA2-Test] DATA2-W2k16/DATA2-W2k16.vmdk

On the get-HardDisk vmware's webpage I see the following comment:
If the hard disk is not attached to any virtual machines, templates, or snapshots, you can search for it in datastores or retrieve it by providing a datastore path to the file where the virtual hard disk is stored. In this case, you might not be able to derive disk type info, and the value of the DiskType property of the hard disk is Unknown.

So I'm guessing that, despite of the error, the disk is being found just fine.

I don't know if there is any properties that I need to set up since I'm bringing it in this way. So I'm not entirely sure what I am missing.

PowerCLI C:\> New-HardDisk -vm $vm -DiskPath $disk0.Filename

New-HardDisk : 6/11/2019 7:19:04 AM    New-HardDisk        Invalid configuration for device '1'.

At line:1 char:1

+ New-HardDisk -vm $vm -DiskPath $disk0.Filename

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

    + CategoryInfo          : NotSpecified: (:) [New-HardDisk], InvalidDeviceSpec

    + FullyQualifiedErrorId : Client20_VirtualDeviceServiceImpl_AttachHardDisk_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.NewHardDisk

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could it be that the copy made the harddisk unusable?

You can't just copy a VMDK as a regular file.
A VMDK are in fact 2 files.

See also KB900


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

0 Kudos
mrbip20
Contributor
Contributor
Jump to solution

Well, I've just realized that the datastore where the vmdk is located was not presented to all the hosts within the cluster, and I was trying to mount the disk into a powered off vm located into one of the Esxi that didn't see the lun. I'm able to mount it now that I vmotioned the vm to a different host using the syntax that you sent me.

Many thanks!

0 Kudos