VMware Cloud Community
erickmiller
Enthusiast
Enthusiast

Move-Datastore / easier method?

Hi,

It doesn't appear there is a Move-Datastore cmdlet, which would be great to have to move a Datastore into a folder.

Is there a way to do this?

Onyx 2.0 results in:

$list = New-Object VMware.Vim.ManagedObjectReference[] (1)

$list[0] = New-Object VMware.Vim.ManagedObjectReference

$list[0].type = "Datastore"

$list[0].value = "datastore-532"

$_this = Get-View -Id 'Folder-group-s65272'

$_this.MoveIntoFolder_Task($list)

Not sure if there is an easier way, or is the only way to reference the MoRef?

Thanks!

Eric K. Miller, Genesis Hosting Solutions, LLC

- Lease part of our ESX cluster!

Eric K. Miller, Genesis Hosting Solutions, LLC http://www.genesishosting.com/ - Lease part of our ESX cluster!
0 Kudos
3 Replies
ykalchev
VMware Employee
VMware Employee

Hi,

PowerCLI 4.1 still does not support datastore folder operations but you can use Get-View cmdlet in order to get folder moref and call MoveIntoFolder method. Here is a sample function how to do this:

function Move-Datastore($datastore, [string] $folderName) {
   $folder = Get-View -ViewType Folder -Filter @{"name", $folderName} -Property Name
   $folder.MoveIntoFolder($datastore.Id)
}

$ds = Get-Datastore storage1 
Move-Datastore $ds "datastoreFolder1"

Regards,

Yasen Kalchev

PowerCLI Dev Team

Message was edited by: ykalchev

P.S. You can even simplify the script using ExtensionData property:

$ds = Get-Datastore storage1 
$folder = Get-Folder datastoreFolder1
$folder.ExtensionData.MoveIntoFolder($ds.Id)

Yasen Kalchev, vSM Dev Team
0 Kudos
erickmiller
Enthusiast
Enthusiast

Hi Yasen,

Thank you SO much for the quick reply! Very useful information.

The only problem I've seen is when there are folders named the same name in different areas of the Inventory, so for instance, if there is a folder called "Genesis" in the Hosts and Clusters view ("hosts" folder in PowerShell) as well as in VMs and Templates, and possibly in Datastores and Networking, it's a real pain to deal with this in the current scripts.

So far, the only way I know how to deal with it is to loop through the multiple objects returned... something like this, if we were looking for the Networking folder:

Get-View -ViewType Folder -Filter @{"Name"=" } }

The way we're going to solve this is to avoid having duplicate names anywhere in the Inventory (adding " Folder", " Datastore", and " Network" to the end of the folder names, but I thought there may be a better approach when this isn't possible.

Thanks again!

Eric K. Miller, Genesis Hosting Solutions, LLC

- Lease part of our ESX cluster!

Eric K. Miller, Genesis Hosting Solutions, LLC http://www.genesishosting.com/ - Lease part of our ESX cluster!
0 Kudos
ykalchev
VMware Employee
VMware Employee

Hi,

You can add childType filtering in the Get-View filter param in order to optimize the query:

 Get-View -ViewType Folder -Filter @{"Name"="<folder name here>";"ChildType"="network"}

A better way to filter only folders under specific view is to use Get-Folder -Location as you've already tried in the post :

You can use Get-View & Get-VIObjectByVIView cmdltes to get "network" folder and pass it to Get-Folder -Location parameter:

$networkRootFolder = Get-View -ViewType Folder -Filter @{"Name"="^network$"} | Get-VIObjectByVIView
$networkGenesisFolder = Get-Folder "Genesis"  -Location $networkRootFolder 

Regards,

Yasen Kalchev

PowerCLI Dev Team

Yasen Kalchev, vSM Dev Team
0 Kudos