VMware Cloud Community
marcustan
Contributor
Contributor
Jump to solution

Moving VM to another folder with full folder path using script

Hi All,

I'm trying to move virtual machines from one folder to another folder using scripts, however there were some errors that I encountered that I couldn't get around.

The script I used was written by LucD, please refer below:

$newDatacenter = "newDC"

$newFolder = "Temp"

$startFolder = New-Folder -Name $newFolder -Location (Get-Folder -Name vm -Location (Get-Datacenter -Name $newDatacenter))

Import-Csv "c:\temp\VM-with-FolderPath.csv" -UseCulture | %{

    $location = $startFolder

    $_.BlueFolderPath.TrimStart('/').Split('/') | %{

        $tgtFolder = Get-Folder -Name $_ -Location $location -ErrorAction SilentlyContinue

        if(!$tgtFolder){

            $location = New-Folder -Name $_ -Location $location

        }

        else{

            $location = $tgtFolder

        }

    }

  

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

    if($vm){

        Move-VM -VM $vm -Destination $location -Confirm:$false

    }

}

I've already exported the CSV file with another script, importing the CSV has no errors.

The error I got from the above script can be summarized as shown below:

PS C:\> move-vm -vm vra-iaas -destination Temp

WARNING: Specifying Folder in the Destination parameter is deprecated and the parameter will stop accepting Folder

objects at a future release. Please use the InventoryLocation parameter instead.

move-vm : 10/29/2018 4:08:22 PM Move-VM         The operation for the entity "vra-iaas" failed with the following message:

"The request refers to an unexpected or unknown type."

At line:1 char:1

+ move-vm -vm vra-iaas -destination Temp

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

    + CategoryInfo          : NotSpecified: (:) [Move-VM], InvalidType

    + FullyQualifiedErrorId : Client20_TaskServiceImpl_CheckServerSideTaskUpdates_OperationFailed,VMware.VimAutomation

   .ViCore.Cmdlets.Commands.MoveVM

Temp is actually a folder I want to move the virtual machines in to. Did I specify the destination wrongly?

Are there any other commands/scripts that can be used?

Thanks in advance for your help.

Best regards,

Marcus

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

We had a similar thread recently on exactly the same, see Move-VM or Move-Inventory won't move VM to another folder - "The request refers to an unexpected or ...

You will have to use Destination and InventoryLocation.


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

View solution in original post

5 Replies
LucD
Leadership
Leadership
Jump to solution

Try using the InventoryLocation parameter on the Move-VM cmdlet for the destination folder.


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

0 Kudos
marcustan
Contributor
Contributor
Jump to solution

I tried using that command, there were no errors, but the VM did not move to the specified folder.

Just to clarify, i'm currently using vSphere 6.5 and VMware PowerCLI 11.

When I manually drag the VM from one folder to another folder on vCenter, it shows MOVE ENTITIES.

If I use the Move-VM cmdlet, it shows RELOCATE VIRTUAL MACHINE on vCenter.

May I know how you specify the folder directory in the command below?

move-vm -vm vra-iaas -InventoryLocation Temp

Thank you.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

We had a similar thread recently on exactly the same, see Move-VM or Move-Inventory won't move VM to another folder - "The request refers to an unexpected or ...

You will have to use Destination and InventoryLocation.


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

marotech
Enthusiast
Enthusiast
Jump to solution

Hello,

Luc helped me in achieving this in the following thread Script error when trying to move vms to their dedicated folders

If you use and adapt to your environment the script from the accepted solution, it should work without any issues. The environment is the same as yours, vCenter 6.5 and Powercli 11.

marcustan
Contributor
Contributor
Jump to solution

Thanks LucD and marotech! Let me run the script and see if it works out. Really appreciate both your help, cheers!

0 Kudos