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.
I don't see any obvious differences.
Did you test run the last version several times?
Does it always work?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I ran your debug version against the production list so to speak and half of the list worked. From what i checked, the other half of the list contains folders that are present in multiple paths, and those are still erroring out.
The error is mostly the same (this time is inventorylocation related parameter):
Move-VM : Cannot convert 'System.Object[]' to the type
'VMware.VimAutomation.ViCore.Types.V1.Inventory.FolderContainer' required by parameter
'InventoryLocation'. Specified method is not supported.
At
C:\Users\username\Desktop\CheapDisasterRecovery\import-05-move-vms-folders-debugging.ps1:61
char:68
+ ... Move-VM -VM $vm -Destination $vm.VMHost -InventoryLocation $folder
+ ~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Move-VM], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.C
ommands.MoveVM
Your script will not work when you have two subfolders with the same name.
DC\Folder1\TestFolder\vm2
DC\Folder2\TestFolder\vm2
To avoid that I have added a modified version of my Get-FolderByPath function.
Can you try with this version.
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 ($globale:DefaultVIServers ) {
Disconnect-VIServer -Server $global:DefaultVIServers -Force
}
$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:\csv-files\test\04-$datacenter-vms-with-FolderPath.csv" | Sort-Object -Property Path
foreach($guest in $VMfolder){
$key = Split-Path $guest.Path
$keyFolder = Get-FolderByPath -Path $key -Separator '\'
$vm = Get-VM -Name $guest.Name
Move-VM -VM $vm -Destination $vm.VMHost -InventoryLocation $keyFolder
}
Disconnect-VIServer "*" -Confirm:$False
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sure, i will run this first thing when i get back to the office and let you know how it goes.
Thank you for your patience and help Luc, much appreciated.
Hello Luc,
I ran the script and it worked flawlessly. Man, I cannot thank you enough for your help with this issue!
Really appreciate it!
New to scripting... I have an exported .csv in vCenter 5.5 and moves hosts to vCenter 6.5 now. It that ran perfect on the export. But I cannot get the below to run in my vCenter6. Here is the output...
.csv contents...
Name | BlueFolderPath |
Demo16 | /Demos |
Demo1 | /Demos |
Demo17 | /Demos |
Demo27 | /Demos |
Demo15 | /Demos |
I keep getting errors on my script...
New-Folder : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViCore.Types.V1.Inventory.VIContainer' required by parameter 'Location'. Specified method is not supported.
At line:11 char:55
+ $location = New-Folder -Name $_ -Location $location
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Folder], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewFolder
Here is my script...
$newDatacenter = "Boston"
$newFolder = "Folder1"
$startFolder = Get-Folder -Name "vm"
Import-Csv "C:\scripts\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 $folder -Confirm:$false
}
}
Did you check if there is perhaps an empty line the CSV file?
Can you runt the following on that CSV file?
Write-Host "$($_.Length) $($_.Name) $($_.BlueFolderPath)"
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I do not see an empty line. The results returned the following...
Demo16 /Demos
Demo1 /Demos
Demo17 /Demos
Demo27 /Demos
Demo15 /Demos
Is that error repeated for every line in the CSV, or do you only see it once?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Every line...
Demo16 PoweredOn 2 4.000
New-Folder : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViCore.Types.V1.Inventory.VIContainer' required by parameter 'Location'. Specified method is not supported.
At line:11 char:55
+ $location = New-Folder -Name $_ -Location $location
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Folder], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewFolder
Demo1 PoweredOn 2 8.000
New-Folder : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViCore.Types.V1.Inventory.VIContainer' required by parameter 'Location'. Specified method is not supported.
At line:11 char:55
+ $location = New-Folder -Name $_ -Location $location
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Folder], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewFolder
Demo17 PoweredOn 2 6.000
New-Folder : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViCore.Types.V1.Inventory.VIContainer' required by parameter 'Location'. Specified method is not supported.
At line:11 char:55
+ $location = New-Folder -Name $_ -Location $location
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Folder], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewFolder
Demo27 PoweredOff 2 4.000
New-Folder : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViCore.Types.V1.Inventory.VIContainer' required by parameter 'Location'. Specified method is not supported.
At line:11 char:55
+ $location = New-Folder -Name $_ -Location $location
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Folder], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewFolder
Demo15 PoweredOn 2 10.000
New-Folder : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViCore.Types.V1.Inventory.VIContainer' required by parameter 'Location'. Specified method is not supported.
At line:11 char:55
+ $location = New-Folder -Name $_ -Location $location
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Folder], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewFolder
In the vCenter recent tasks it shows this...
The Move-VM error is a result from the target folder not being there (due to the other error).
The 1st error seems to indicate that the value on the Name parameter of the New-Folder cmdlet is an array instead of a string.
I don't really see how that can happen when loop through the output of the Import-Csv.
Can you just do a
Import-Csv "C:\scripts\vm-folder.csv" -UseCulture
And check if the column headers are also shown?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I do see column headers...
Name BlueFolderPath |
---- -------------- |
OZ-BDC-Demo-27 /Demos |
OZ-BDC-Demo-25 /Demos |
OZ-BDC-Demo-24 /Demos |
OZ-BDC-Demo-23 /Demos |
OZ-BDC-Demo-22 /Demos |
OZ-BDC-Demo-21 /Demos |
OZ-BDC-Demo-18 /Demos |
OZ-BDC-Demo-19 /Demos |
OZ-BDC-Demo-16 /Demos |
But wait a minute, I'm looking at the wrong parameter, it's the Location parameter that complain about getting an array.
Do you by any chance have multiple Datacenters defined, or are you connected to more than vSPhere server?
Does the following return 1 folder or more?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I'm confused as to why this is my result...
PS C:\scripts> Get-Folder -Name vm
Name Type
---- ----
vm VM
vm VM
vm VM
vm VM
vm VM
vm VM
Can you check the following
Get-Datacenter
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I show 1 vCenter connected to my session. The correct one.
But it is showing all 6 of my DataCenters in Get-Datacenter
And that is causing the issue.
There is a hidden folder named 'vm' under each Datacenter.
Change the $startFolder line like this
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
So that worked and it creates the folders! It began creating the folders.
Problem now is I am not getting any script errors... but the VMs aren't moving. The only error now is in vCenter...
Where are these VMs located?
Are these folders accessible from the cluster on which the VMs are running?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Update... when I run the script from the ./script it actually runs!
My next problem is when it gets to a folder that is more than one level deep it errors with the same error.
/Clients/Bridge-Backups |