VMware Cloud Community
RajuVCP
Hot Shot
Hot Shot

Script to move VM from one folder to another folder

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.

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
Reply
0 Kudos
14 Replies
LucD
Leadership
Leadership

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.


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

Reply
0 Kudos
RajuVCP
Hot Shot
Hot Shot

Thanks LucD for your quick response as always  Smiley Happy

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.

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
Reply
0 Kudos
kunaludapi
Expert
Expert

I have written 2 scripts for the same purpose

Getting VMs folder Path from VMs and Templates vCenter

Move/Migrate VMs to folder Path on another vCenter - Powercli


--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
Reply
0 Kudos
RajuVCP
Hot Shot
Hot Shot

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.

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
Reply
0 Kudos
LucD
Leadership
Leadership

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

}


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

RajuVCP
Hot Shot
Hot Shot

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

}

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
Reply
0 Kudos
LucD
Leadership
Leadership

In the FolderName column in the CSV


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

Reply
0 Kudos
RajuVCP
Hot Shot
Hot Shot

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

Raju Gunnal VCP 4, VCP 5, VTSP 4, VTSP 5, ITIL V3 http://www.techtosolution.com
Reply
0 Kudos
LucD
Leadership
Leadership

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.


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

Reply
0 Kudos
GarethCox2014
Contributor
Contributor

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

Reply
0 Kudos
LucD
Leadership
Leadership

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


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

Reply
0 Kudos
GarethCox2014
Contributor
Contributor

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

Reply
0 Kudos
LucD
Leadership
Leadership

The line where you call Get-FolderByPath.
Like this

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


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

Reply
0 Kudos
GarethCox2014
Contributor
Contributor

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

Reply
0 Kudos