VMware vSphere

 View Only
Expand all | Collapse all

Script to move VM from one folder to another folder

  • 1.  Script to move VM from one folder to another folder

    Posted Feb 17, 2016 11:44 AM

    Hi All,

    I have hundreds of VMs which need to be moved to another folder, do we have any script available.

    My requirement is i have many VMs in same vCenter i have to move it to another folder or sub-folder, (like it should be like get the VM names from content and move to destination folder)

    Thanks in Advance.



  • 2.  RE: Script to move VM from one folder to another folder

    Posted Feb 17, 2016 11:47 AM

    Basically, you have the Move-VM cmdlet for that.

    But is there any logic to select the source and destination of these moves ?

    Provided you want to script it.



  • 3.  RE: Script to move VM from one folder to another folder

    Posted Feb 17, 2016 11:53 AM

    Thanks LucD for your quick response as always  :smileyhappy:

    my scenario is i have VMs under folder named "Discovered Virtual Machines" and i have to move those VMs to their respective project related folder like , Production, Testing, Developers.

    like i was looking for script instead of giving each vm name. it should be like.. get the vm names from excel or csv and i give the destination folder like Production , Testing,..etc.. so that those VMs can be moved.



  • 4.  RE: Script to move VM from one folder to another folder



  • 5.  RE: Script to move VM from one folder to another folder

    Posted Feb 17, 2016 12:08 PM

    Thanks Kunal,

    I went through your given links , but the script is for getting the path and moving to another vCenter. may be it will not suits my requirement.

    I have moved host from one datacenter to antoher datacenter within the same vCenter.

    Datacenter01

    HostA

    HostB

    Datacenter02

    When i moved hostA and HostB from Datacenter01 to Datacenter02 the folder structure got misplaced. But i have the list of the Vms and its folder path.

    Now am looking for the script which should move the VMs listed in excel or csv to the selected given folder.



  • 6.  RE: Script to move VM from one folder to another folder

    Posted Feb 17, 2016 12:19 PM

    Assuming the CSV looks like this

    VmName,FolderName

    VM1,folder1\folder2

    VM2,folder1\folder2\folder3

    You could use my Get-FolderByPath function, and do something like this

    Import-Csv vmnames.csv -UseCulture | %{

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

        $folder = Get-FolderByPath -Name $_.FolderName

        Move-VM -VM $vm -Destination $folder

    }



  • 7.  RE: Script to move VM from one folder to another folder

    Posted Feb 17, 2016 12:28 PM

    Thanks LucD,

    This might be a dumb question :smileymischief: , where can i give the destination folder name in above script line.

    Import-Csv vmnames.csv -UseCulture | %{

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

        $folder = Get-FolderByPath -Name $_.FolderName

        Move-VM -VM $vm -Destination $folder

    }



  • 8.  RE: Script to move VM from one folder to another folder

    Posted Feb 17, 2016 12:56 PM

    In the FolderName column in the CSV



  • 9.  RE: Script to move VM from one folder to another folder

    Posted Feb 17, 2016 03:43 PM

    I just entered the details of the destination path of the vm in csv and ran the script i got below given error. .not sure if am missing something here.

    PowerCLI H:\MY DATA\Script Important\VMfoldermove> .\vmmove.ps1

    Get-FolderByPath : The term 'Get-FolderByPath' is not recognized as the name

    of a cmdlet, function, script file, or operable program. Check the spelling of

    the name, or if a path was included, verify that the path is correct and try

    again.

    At H:\MY DATA\Script Important\VMfoldermove\vmmove.ps1:5 char:15

    +     $folder = Get-FolderByPath -Name $_.FolderName

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

        + CategoryInfo          : ObjectNotFound: (Get-FolderByPath:String) [], Co

       mmandNotFoundException

        + FullyQualifiedErrorId : CommandNotFoundException

    Move-VM : Cannot validate argument on parameter 'Destination'. The argument is

    null or empty. Provide an argument that is not null or empty, and then try the

    command again.

    At H:\MY DATA\Script Important\VMfoldermove\vmmove.ps1:7 char:34

    +     Move-VM -VM $vm -Destination $folder

    +                                  ~~~~~~~

        + CategoryInfo          : InvalidData: (:) [Move-VM], ParameterBindingVali

       dationException

        + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom

       ation.ViCore.Cmdlets.Commands.MoveVM

    Get-FolderByPath : The term 'Get-FolderByPath' is not recognized as the name

    of a cmdlet, function, script file, or operable program. Check the spelling of

    the name, or if a path was included, verify that the path is correct and try

    again.

    At H:\MY DATA\Script Important\VMfoldermove\vmmove.ps1:5 char:15

    +     $folder = Get-FolderByPath -Name $_.FolderName

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

        + CategoryInfo          : ObjectNotFound: (Get-FolderByPath:String) [], Co

       mmandNotFoundException

        + FullyQualifiedErrorId : CommandNotFoundException

    Move-VM : Cannot validate argument on parameter 'Destination'. The argument is

    null or empty. Provide an argument that is not null or empty, and then try the

    command again.

    At H:\MY DATA\Script Important\VMfoldermove\vmmove.ps1:7 char:34

    +     Move-VM -VM $vm -Destination $folder

    +                                  ~~~~~~~

        + CategoryInfo          : InvalidData: (:) [Move-VM], ParameterBindingVali

       dationException

        + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom

       ation.ViCore.Cmdlets.Commands.MoveVM



  • 10.  RE: Script to move VM from one folder to another folder

    Posted Feb 17, 2016 04:02 PM

    You will have to copy the code for my Get-FolderByPath function to your .ps1 file (at the top).

    Then you call the function later on in the .ps1 file.



  • 11.  RE: Script to move VM from one folder to another folder

    Posted Feb 18, 2019 10:38 AM

    Hello Luc,

    Apologies for my reply, I am new to scripting and I tried this function but it comes back with error, if you have time perhaps you could advise?

    I have already included the get-folderbypath fucntion in my ps1 file (at the top)

    I am using very simple .csv to try to move just 1 VM for a test

    ============================================================

    vmnames.csv

    VmName,FolderName

    DECADMB4,Decatur Data Center/Decatur Production Cluster ESXi 6.0 - BL460c G6 X5570 CPU Only/Decatur Application Servers

    ==================================================

    Get-FolderByPath : A parameter cannot be found that matches parameter name

    'Name'.

    At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vmmove.ps1:40

    char:32

    +     $folder = Get-FolderByPath -Name $_.FolderName

    +                                ~~~~~

        + CategoryInfo          : InvalidArgument: (:) [Get-FolderByPath], Paramet

       erBindingException

        + FullyQualifiedErrorId : NamedParameterNotFound,Get-FolderByPath

    Move-VM : Cannot validate argument on parameter 'Destination'. The argument is

    null or empty. Provide an argument that is not null or empty, and then try the

    command again.

    At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vmmove.ps1:42

    char:34

    +     Move-VM -VM $vm -Destination $folder

    +                                  ~~~~~~~

        + CategoryInfo          : InvalidData: (:) [Move-VM], ParameterBindingVali

       dationException

        + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom

       ation.ViCore.Cmdlets.Commands.MoveVM



  • 12.  RE: Script to move VM from one folder to another folder

    Posted Feb 18, 2019 11:13 AM

    The parameter for the Get-FolderByPath function is Path, not Name.



  • 13.  RE: Script to move VM from one folder to another folder

    Posted Feb 18, 2019 03:06 PM

    Ok, please accept my apologies, what do I need to edit to make it work? Thank you for replying so quickly!



  • 14.  RE: Script to move VM from one folder to another folder

    Posted Feb 18, 2019 03:25 PM

    The line where you call Get-FolderByPath.
    Like this

    $folder = Get-FolderByPath -Path $_.FolderName



  • 15.  RE: Script to move VM from one folder to another folder

    Posted Feb 18, 2019 09:00 PM

    Thank you so much Luc, this worked, but you knew that ;-) I am learning day by day, really appreciate your support.