VMware Cloud Community
deasyp
Enthusiast
Enthusiast
Jump to solution

Adding existing disks to a VM via PowerCLi

Hi there,

I'm working on a process to add existing vmdk hard-drive files to a VM (doing a migration from HyperV to vSphere using commvault).

I've discovered that when the VM is migrated, the hard-disks are pointing to snapshot files, which requires having to remove the disk references (easy done via powercli).

The trouble is that I need to re-add the existing disks to the VM (in order to be correctly mapped). I've been trying to add the disks using powercli, but it's having none of it.

I'm using the following command example..

$x = get-vm -Name my_vmname

New-HardDisk -DiskPath "[datastore]/my_vmname/my_vmname.vmdk" -VM $x

when I do this, I get :

New-HardDisk : 06/03/2019 10:57:13 New-HardDisk No matching datastore found.

At line:1 char:1

+ New-HardDisk -DiskPath "[datastore]/my_vmname/myvmname.vmdk"  ...

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

    + CategoryInfo          : ObjectNotFound: (:) [New-HardDisk], VimException

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

(I'm using PowerCli 6.5 Release1 build 4624819)

I can map this via the web-console without issue, but it's seriously slow in comparison to the command line.. especially with a large number of folders in the LUN (especially when they're not in any alphabetical order!)

Would really welcome any suggestions on how to do this.. to save my sanity Smiley Happy

Alternatively is there a way to change the file path of a harddisk.. so that I could drop the '-000001' from the filename, thus saving the need to remove disks, then re-add.

Anyone done this before ??

Thanks in advance

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There has to be a blank between the ] and the first character of the VM displayname (which is also the foldername on the datastore).

That warning is to inform you that in the near future you can only pass a single VM on the VM parameter.
Now you can still pass multiple VMs on the VM parameter.

In your case you can ignore the warning.


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

View solution in original post

0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

A DiskPath is normally of the format "[datastore] my_vmname/my_vmname.vmdk".

Did you try with that format?


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

0 Kudos
deasyp
Enthusiast
Enthusiast
Jump to solution

Hi LucD, thanks for the assistance..

Unless I'm going blind.. I think we're both on the same page, the only difference that I can see between my command and yours is the '/' character after the [datastore]

My command above is: "[datastore]/my_vmname/my_vmname.vmdk"

Yours is: "[datastore] my_vmname/my_vmname.vmdk"

I've tried yours without the '/' character and I get the same.. yet when I map the setting via the web interface the path shows the '/'

I do get a response back, when running 'new-harddisk' which says:

WARNING: Parameter 'VM' is obsolete. Passing multiple values to this parameter is obsolete.

Is there a preferred way of doing this disk addition, should I be taking another approach such as going a 'get-vm' and piping the output into 'new-harddisk' ?? (I'm a bit confused)

Any assistance would be very much welcomed.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There has to be a blank between the ] and the first character of the VM displayname (which is also the foldername on the datastore).

That warning is to inform you that in the near future you can only pass a single VM on the VM parameter.
Now you can still pass multiple VMs on the VM parameter.

In your case you can ignore the warning.


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

0 Kudos
deasyp
Enthusiast
Enthusiast
Jump to solution

Thanks for the steer in the right direction..  making progress ! Thank you ! Smiley Happy

As you say it was down to the blank between the ']' and the VMname.. I was taking the path "]/vmname" from the path being displayed when I looked at the web interface!

     I'd been looking at this for so long, that I missed the missing '/' character from the 'get-harddisk' command output.

I don't suppose you know (off the top of your head) if there's a way to pipe the filenames from the output of 'get-harddisk -VM vmname | select Filename' (or something) to use this as a way to map the new disks using the new-harddisk command ??

I'm going to try and figure out a way to trim the '-000001' from the filename (output from get-harddisk, prior to removing the disks from the settings) and then re-add the disks using 'new-harddisk'

Unless there's a cleaner way to do it, using 'set-harddisk', to rename the file ? (thus saving the need for the  'get' > 'delete' > 'new' process for each disk)

Thanks for your time & resolution assistance!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Getting rid of the snapshot suffix is not too hard with the -replace operator and a RegEx expression.

I would go for 2 scripts, and store the results in a CSV file.

Scrip 1, the export

Get-VM | Get-HardDisk |

Select @{N='VM';E={$_.Parent.Name}},Name,FileName |

Export-Csv -Path .\disknames.csv -UseCulture -NoTypeInformation

Script 2, the import with the renamed filename

Import-Csv -Path .\disknames.csv -UseCulture |

ForEach-Object -Process {

   $newFileName = $_.FileName -replace "-\d{6}\.","."

   $vm = Get-VM -Name $_.VM

   New-HardDisk -VM $vm -DiskPath $newFileName

}


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

deasyp
Enthusiast
Enthusiast
Jump to solution

Excellent, thanks a million LucD !!

I was really struggling to get my head around the rename, just needed to re-frame the approach Smiley Happy

thank you

0 Kudos
padulka
Hot Shot
Hot Shot
Jump to solution

Hi LucD, but is this script applicable only to add a second disk without use external csv file? I would like to add on my VM a second existing hard disk Regards

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you can provide the path to that VMDK  in another way than through a CSV, yes, that is possible.

The concept stays the same New-HardDisk and the DiskPath parameter (see Example 6).


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

0 Kudos
padulka
Hot Shot
Hot Shot
Jump to solution

Yes I' m just using the example 6 but I have to use a variable so my script:

$PVID = "1234"

$Myhost = "HOSTRM$PVID.lab.it"

$MyVM = "VM$PVID"+"RM"

$pathdisk = "[/datastore1\ -\ lab$PVID] /Repository/VMDK/newdisk.vmdk"

New-HardDisk -VM $MyVM -DiskPath $pathdisk

I receive the following errors:

New-HardDisk : 25/11/2019 16:01:56 New-HardDisk No matching datastore found.

At line:1 char:1

+ New-HardDisk -VM "VM1234RM" -DiskPath $dspath

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

    + CategoryInfo          : ObjectNotFound: (:) [New-HardDisk], VimException

    + FullyQualifiedErrorId : Client20_VirtualDeviceServiceImpl_NewVirtualHardDisk_DatastoreNotFound,VMware.VimAutomation.Vi

   Core.Cmdlets.Commands.VirtualDevice.NewHardDisk

If I will use your script syntax all works fine

Get-VM -Name $MyVM | Get-HardDisk | Select @{N='VM';E={$_.Parent.Name}},Name,FileName | Export-Csv -Path .\disknames.csv -UseCulture -NoTypeInformation

Before import I modify the csv file as follow:

"VM";"Name";"Filename"

"VM9992RM";"Hard disk 2";"[datastore1 - lab1234] Repository/VMDK/newdisk.vmdk"

$importcsv = Import-Csv -Path .\disknames.csv -UseCulture

$newFileName = $importcsv.FileName

New-HardDisk -VM "MyVM"  -$importcsv.FileName

Regards

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Why do you have all those slashes in

$pathdisk = "[/datastore1\ -\ lab$PVID] /Repository/VMDK/newdisk.vmdk"

Give it a try with just

$pathdisk = "[datastore1 - lab$PVID] Repository/VMDK/newdisk.vmdk"


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