VMware Cloud Community
marotech
Enthusiast
Enthusiast
Jump to solution

Script error when trying to move vms to their dedicated folders

Hello,

I am having issues when using a script to move VMs from the Discovered VMs folder to their dedicated VM folders as they were on the old vCenter. I already recreated the folder structure on the new vCenter.

The csv from where i am importing is on the form:

Name  Path

vm1     DatacenterFolderName\folder1\folder2\vm1

vm2     DatacenterFolderName\folder2\folder3\vm2 etc

The script is the following:

Get-Module -Name VMware* -ListAvailable | Import-Module

If ($globale:DefaultVIServers ) {

Disconnect-VIServer -Server $global:DefaultVIServers -Force

}

$destVI = Read-Host "Please enter name or IP address of the destination Server"

$datacenter = Read-Host "DataCenter name in VC"

$creds = get-credential

connect-viserver -server $destVI -Credential $creds

# move the vm's to correct location

$VMfolder = @()

$VMfolder = import-csv "c:\csv-files\test\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 "vm"

}

else

{

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

}

}

Disconnect-VIServer "*" -Confirm:$False

and the error i am getting is the following:

Move-VM : 10/19/2018 7:50:24 AM Move-VM Server task failed: The request refers to an unexpected or unknown type.

At C:\Users\username\Desktop\CheapDisasterRecovery\import-05-move-vms-folders.ps1:27 char:3

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

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

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

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

Can someone let me know what i am doing wrong or if there is something that needs to be modified to make it work?

The original vCenter is 5.5 and the destination vCenter is 6.5.

Thank you.

42 Replies
LucD
Leadership
Leadership
Jump to solution

Do you still do the move like this?
Because I don't see in the code you included where $folder is initialised.

Shouldn't that be $tgtFolder instead of $folder?

    if($vm){

        Move-VM -VM $vm -Destination $vm.VMHost -InventoryLocation $folder -Confirm:$false

    }


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

frased
Enthusiast
Enthusiast
Jump to solution

Thank you so much for all your help on this!  Here is the final version that worked perfectly.  Created folders and moved VMs... 

$newDatacenter = "blahblah"

$newFolder = "Folder1"

$startFolder = Get-Folder -Name "vm" -Location $newDatacenter

Import-Csv "C:\scripts\vCenter5_vm-folder.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 $vm.VMHost -InventoryLocation $location -Confirm:$false

    }

}

0 Kudos
stealthdave
Contributor
Contributor
Jump to solution

 

@LucD 

Hi LucD,

Thanks for this. 

I just wanted to advise on the issues I had, and see if they affected anyone else.

I had to change the [char]${Separator} = '/' to [char]${Separator} = '\' in the get-folderbypath function, as the $key = Split-Path $guest.Path line converts the / to \, and then errors if the / is not replaced with \.

I also noticed that you have an rogue e in the $global:DefaultVIServers line, you have $globale:DefaultVIServers.

The below, worked for me:

function Get-FolderByPath{
param(

[CmdletBinding()]

[parameter(Mandatory = $true)]

[System.String[]]${Path},

[char]${Separator} = '\'

)


process{

foreach($strPath in $Path){

$root = Get-Folder -Name Datacenters

$strPath.Split($Separator) | %{

$root = Get-Inventory -Name $_ -Location $root -NoRecursion

if((Get-Inventory -Location $root -NoRecursion | Select -ExpandProperty Name) -contains "vm"){

$root = Get-Inventory -Name "vm" -Location $root -NoRecursion

}

}

$root | where {$_ -is [VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl]}|%{

Get-Folder -Name $_.Name -Location $root.Parent -NoRecursion

}

}

}

}


Get-Module -Name VMware* -ListAvailable | Import-Module

If ($global:DefaultVIServers ) {

Disconnect-VIServer -Server $global:DefaultVIServers -Force -Confirm:$false

}

$destVI = Read-Host "Please enter name or IP address of the destination Server"

$creds = Get-Credential

connect-viserver -server $destVI -Credential $creds

# move the vm's to correct location

$VMfolder = Import-Csv "C:\Scripts\AdminTasks\Powercli\Folders\VM_folderpaths_import_migration_test.csv" | Sort-Object -Property Path

foreach($guest in $VMfolder)
{
# This converts the format of the path from folder/folder to folder\folder, and why the seperator must be \ in the function
$key = Split-Path $guest.Path
write-host -ForegroundColor cyan $key

$keyFolder = Get-FolderByPath -Path $key -Separator '\'

$vm = Get-VM -Name $guest.Name

Move-VM -VM $vm -Destination $vm.VMHost -InventoryLocation $keyFolder | out-null
}

Disconnect-VIServer "*" -Confirm:$False

 

0 Kudos