VMware Cloud Community
Natarajvd
Contributor
Contributor
Jump to solution

Script for Move vm from one folder to other folder

I am looking Script for moving VM from one folder to other folder even Duplicated folder name exist on VC .The below script works fine in Unique folder structure.

$VMfolder = @()

$VMfolder = import-csv "C:\Users\Nataraj\Desktop\04-$($datacenter)-vms-with-FolderPath.csv" | Sort-Object -Property Path

foreach($guest in $VMfolder){

     $key = @()

     $key =  Split-Path $guest.Path | split-path -leaf

     if ($key -eq $datacenter) {

          Write-Host "Root folder $guest.path"

          ##

          Move-VM (Get-VM $guest.Name) -Destination (Get-folder $key)

          }

          else

          {

          Move-VM (Get-VM $guest.Name) -Destination "VM"

          }

}

0 Kudos
44 Replies
MinoDC
Enthusiast
Enthusiast
Jump to solution

sorry sorry sorry Luc...:smileycry:

I've ran the command on test environment with single datacenter...and it works fine:

Move-VM -VM "vSphere Management Assistant (vMA)" -Destination ESXi-BL01 -InventoryLocation 'MGMT'

As it worked on this environment, I thought, unfortunately, that it also worked in production , instead I've tried to run same command in production environment that has multi datacenter:

and receive the error:

Move-VM : 23/01/2019 11:44:06 Move-VM The specified parameter 'InventoryLocation' expects a single value, but your name criteria 'MGMT' corresponds to multiple values.

At line:1 char:1

+ Move-VM -VM 'vSphere Management Assistant (vMA)' -Destination esxcedn ...

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

    + CategoryInfo          : InvalidResult: (System.Collecti...dObjectInterop]:List`1) [Move-VM], VimException

    + FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_MoreResultsThanExpected,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM

Move-VM : 23/01/2019 10:44:06 Move-VM FolderContainer parameter: Could not find any of the objects specified by name.

At line:1 char:1

+ Move-VM -VM 'vSphere Management Assistant (vMA)' -Destination esxcedn ...

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

    + CategoryInfo          : ObjectNotFound: (VMware.VimAutom...ventoryLocation:RuntimePropertyInfo) [Move-VM], ObnRecordProcessingFailedException

    + FullyQualifiedErrorId : Core_ObnSelector_SetNewParameterValue_ObjectNotFoundCritical,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM

then I've tried with this:

Move-VM -VM "vSphere Management Assistant (vMA)" -Destination ESXi-BL01 -InventoryLocation 'CED PRODUCTION\MGMT'

and receive this error

Move-VM : 23/01/2019 11:45:19 Move-VM Could not find FolderContainer with name 'CED PRODUCTION\MGMT'.

At line:1 char:1

+ Move-VM -VM 'vSphere Management Assistant (vMA)' -Destination esxcedn ...

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

    + CategoryInfo          : ObjectNotFound: (CED NAZIONALE/CISIA:String) [Move-VM], VimException

    + FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM

Move-VM : 23/01/2019 10:45:19 Move-VM FolderContainer parameter: Could not find any of the objects specified by name.

At line:1 char:1

+ Move-VM -VM 'vSphere Management Assistant (vMA)' -Destination esxcedn ...

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

    + CategoryInfo          : ObjectNotFound: (VMware.VimAutom...ventoryLocation:RuntimePropertyInfo) [Move-VM], ObnRecordProcessingFailedException

    + FullyQualifiedErrorId : Core_ObnSelector_SetNewParameterValue_ObjectNotFoundCritical,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM

sorry again for the wrong reply

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are using OBN to resolve the InventoryLocation and Destination.
Can you try the same but the actual objects instead of the strings?

Move-VM -VM "vSphere Management Assistant (vMA)" -Destination (Get-VMHost -Name ESXi-BL01) -InventoryLocation (Get-Folder -Name'MGMT')


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

MinoDC
Enthusiast
Enthusiast
Jump to solution

Hi LucD.

It's works if you have unique folder in all datacenter.

Alternatively , if you have the same folder name in multi datacenter, it works in this way (with Location parameter):

Move-VM -VM "vSphere Management Assistant (vMA)" -Destination (Get-VMHost -Name ESXi-BL01) -InventoryLocation (Get-Folder -Name 'MGMT' -Location 'CED PRODUCTION')

How can I insert all info in script ?

And...

How can create a right script where export in csv right information ?

THANKS.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Well, those scripts aren't mine... :smileygrin:

But in your environment, I would add the Datacenter to the export.
A outer ForEach loop over all the datacenters.

You can add the -Location $dc to the Get-Inventory, Get-VM and Get-Folder cmdlets.

If you need help on that let me know.

Or as an alternative, migrate datacenter by datacenter.


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

0 Kudos
MinoDC
Enthusiast
Enthusiast
Jump to solution

Then I'll try with a better script.

If I have problems I will write.

Thank you so much for helping me to find the Move-VM problem.

0 Kudos