VMware Cloud Community
mmoretDMP
Contributor
Contributor
Jump to solution

Move datastore using powercli to a folder

Hi all,

Here is the situation:
1 VCenter
Multiple Datacenters
Multiple clusters in each DC
Folders for all customers.

I would like to automate the placement of a customer datastore in it's own folder.
However, we use that folder name on each level (VM, DataStore, Network) and sometimes a customer uses multiple clusters, so the Foldername can appear sometimes 8 or 10 times.
I looked at the great functions: Get-FolderPath and GetFolderByPath, but i cannot get it to work.

Error I receive:
Move-Datastore : 18-1-2021 17:15:50 Move-Datastore The specified parameter 'Destination' expects a single value, but your name criteria 'CUST0001' corresponds to multip
le values.

Info:
Get-Folder -Name CUST0001

Name Type
---- ----
CUST0001 Network
CUST0001 Network
CUST0001 Datastore
CUST0001 Datastore
CUST0001 VM
CUST0001 VM

Any help would be greatly appreciated!

TIA - Martijn

Labels (3)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The function should return the Folder object, which can capture in variable.
Then use that variable on the parameter for your cmdlet.


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

View solution in original post

8 Replies
LucD
Leadership
Leadership
Jump to solution

You should be able to find the folder by using Get-FolderByPath.
How did you call the function?
What is not working?


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

mmoretDMP
Contributor
Contributor
Jump to solution

Thanks for your reply.
If i use it on the DC it returns:

Get-FolderByPath -Path "DC001"

Name Type
---- ----
vm VM

If I go deeper, it returns only the VM path:

Get-FolderByPath -Path "DC001/CUST0001"

Name Type
---- ----
CUST0001 VM

If I use the get-folder and pipe, it returns:

get-folder -Type Datastore -Name CUST0001 | Get-FolderPath

Name Path Type
---- ---- ----
CUST0001 DC001\datastore\CUST0001 yellow
CUST0001 DC002\datastore\CUST0001 yellow

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is exactly what the function is supposed to do.
If you look at the code you'll notice it descends the 'vm' path, or in other words the VM & Templates folder.

What would you like it to return?


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

0 Kudos
mmoretDMP
Contributor
Contributor
Jump to solution

Ok, thanks.

My goal is to move a datastore into the datastore folder for this customer using powercli (hence the title of my topic).

Thanks!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you want to go  down the datastore path, you can just change 'vm' to 'datastore'.
Like this

                    if ((Get-Inventory -Location $root -NoRecursion | select -ExpandProperty Name) -contains "datastore") {
                        $root = Get-Inventory -Name "datastore" -Location $root -Server $vc -NoRecursion
                    }


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

0 Kudos
mmoretDMP
Contributor
Contributor
Jump to solution

Again, thanks for your quick replies, but I need some more help I think.
I changed the code of the function, but how do I get the unique folder to move the datastore in?

Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The function should return the Folder object, which can capture in variable.
Then use that variable on the parameter for your cmdlet.


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

mmoretDMP
Contributor
Contributor
Jump to solution

Thanks for your pateince and quick replies!

The following code works like a charm. 

$tds = Get-FolderByPath -Path dc001/CUST001
Move-Datastore -Datastore CUST0001_vol01 -Destination $tds

For others, the function was also changed to return datastore folders:
Around line 38.

if((Get-Inventory -Location $root -NoRecursion | Select -ExpandProperty Name) -contains "datastore"){
$root = Get-Inventory -Name "datastore" -Location $root -Server $vc -NoRecursion

 

0 Kudos