PowerCLI

 View Only
Expand all | Collapse all

Adding existing disks to a VM via PowerCLi

  • 1.  Adding existing disks to a VM via PowerCLi

    Posted Mar 06, 2019 11:22 AM

    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 :smileyhappy:

    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



  • 2.  RE: Adding existing disks to a VM via PowerCLi

    Posted Mar 06, 2019 11:25 AM

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

    Did you try with that format?



  • 3.  RE: Adding existing disks to a VM via PowerCLi

    Posted Mar 06, 2019 01:56 PM

    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.



  • 4.  RE: Adding existing disks to a VM via PowerCLi
    Best Answer

    Posted Mar 06, 2019 02:04 PM

    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.



  • 5.  RE: Adding existing disks to a VM via PowerCLi

    Posted Mar 06, 2019 02:52 PM

    Thanks for the steer in the right direction..  making progress ! Thank you ! :smileyhappy:

    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!



  • 6.  RE: Adding existing disks to a VM via PowerCLi

    Posted Mar 06, 2019 03:07 PM

    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

    }



  • 7.  RE: Adding existing disks to a VM via PowerCLi

    Posted Mar 07, 2019 04:42 PM

    Excellent, thanks a million LucD !!

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

    thank you



  • 8.  RE: Adding existing disks to a VM via PowerCLi

    Posted Nov 25, 2019 02:37 PM

    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



  • 9.  RE: Adding existing disks to a VM via PowerCLi

    Posted Nov 25, 2019 02:47 PM

    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).



  • 10.  RE: Adding existing disks to a VM via PowerCLi

    Posted Nov 25, 2019 03:14 PM

    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



  • 11.  RE: Adding existing disks to a VM via PowerCLi

    Posted Nov 25, 2019 03:26 PM

    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"