Automation

 View Only
  • 1.  move vm to subfilders

    Posted Oct 22, 2019 08:03 AM

    Hi.

    I use vsphere web client 6.7
    There is a list of VMs. There are two entries in the annotation of each VM. 
     
    in inventory I have folders with the name from the first entry in annatation and subfolders from the second entry in annatation.
    help with writing a script to move VMs to subfolders according to annotation 


  • 2.  RE: move vm to subfilders

    Posted Oct 22, 2019 08:11 AM

    What is the layout of these fields in the Annotation?
    How can a script find the 1st and 2nd entry?



  • 3.  RE: move vm to subfilders

    Posted Oct 22, 2019 08:30 AM

    <#$vms = Get-VM

    foreach ($vm in $vms) {

    $Critical = $vm.CustomFields.Item("Critical level")
    $id = $vm.CustomFields.Item("Service ID")

    #get-folder -Name $Critical  
    new-folder -Name $id -Location (Get-Folder $Critical)

    Move-Inventory -Item $vm -Destination $id
    }

    #>



  • 4.  RE: move vm to subfilders

    Posted Oct 22, 2019 08:31 AM

    or

    <#

    $vms = Get-VM

    foreach ($vm in $vms) {

    $Critical = $vm.CustomFields.Item("Critical level")
    $id = $vm.CustomFields.Item("Service ID")

    get-folder -Name $Critical | new-folder -Name $id

    move-vm -VM $vm -Destination $id
    }

    #>



  • 5.  RE: move vm to subfilders

    Posted Oct 22, 2019 10:34 AM

    You could try something like this.
    If a folder doesn't exist, the script will create it.

    foreach ($vm in Get-VM) {

        $Critical = $vm.CustomFields.Item("Critical level")

        $id = $vm.CustomFields.Item("Service ID")

        try{

            $cFolder = Get-Folder -Name $Critical -Type VM -ErrorAction Stop

        }

        catch{

            $cFolder = New-Folder -Name $Critical -Location (Get-Folder -Name vm) -Confirm:$false

        }

        try{

            $iFolder = Get-Folder -Name $id -Type VM -Location $cFolder -ErrorAction Stop

        }

        catch{

            $iFolder = New-Folder -Name $id -InventoryLocation $cFolder -Confirm:$false

        }

        Move-VM -VM $vm -Destination $id -Confirm:$false

    }



  • 6.  RE: move vm to subfilders

    Posted Oct 22, 2019 11:15 AM
    can we specify the full path to the subfolder in the variable and use it when moving?

    # Move-VM -VM $vm -Destination 'full path to subfolder' -Confirm:$false



  • 7.  RE: move vm to subfilders

    Posted Oct 22, 2019 11:20 AM

    Correction, that should be InventoryLocation instead of Destination.

    And no, you have to specify a folder, you can't use the full path



  • 8.  RE: move vm to subfilders

    Posted Oct 23, 2019 06:15 AM
    Tried to use Inventory Location. does not find given instead. In which version of powercli is it present

    # Move-VM -VM $vm -InventoryLocation 'subfolder' -Confirm:$false



  • 9.  RE: move vm to subfilders

    Posted Oct 23, 2019 07:02 AM

    It was added in PowerCLI 6.5.2, see VMware PowerCLI Change Log



  • 10.  RE: move vm to subfilders

    Posted Oct 28, 2019 12:59 PM
    thanks, the script is working