Automation

 View Only
  • 1.  Move datastore using powercli to a folder

    Posted Jan 18, 2021 04:17 PM

    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



  • 2.  RE: Move datastore using powercli to a folder

    Posted Jan 18, 2021 04:26 PM

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



  • 3.  RE: Move datastore using powercli to a folder

    Posted Jan 18, 2021 04:38 PM

    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



  • 4.  RE: Move datastore using powercli to a folder

    Posted Jan 18, 2021 04:42 PM

    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?



  • 5.  RE: Move datastore using powercli to a folder

    Posted Jan 18, 2021 04:44 PM

    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!



  • 6.  RE: Move datastore using powercli to a folder

    Posted Jan 18, 2021 04:54 PM

    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
                        }
    


  • 7.  RE: Move datastore using powercli to a folder

    Posted Jan 19, 2021 08:15 AM

    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



  • 8.  RE: Move datastore using powercli to a folder
    Best Answer

    Posted Jan 19, 2021 08:26 AM

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



  • 9.  RE: Move datastore using powercli to a folder

    Posted Jan 19, 2021 09:02 AM

    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