PowerCLI

 View Only
  • 1.  Powershell script to defrag all disks in vApp

    Posted May 29, 2014 02:28 PM

    Hello,

    A request came in suggesting that a script should be written to insure that vApps on a given datacenter remain defragmented. Using Powershell, the script should, once per day (during the night), select a random vApp, mount all it's virtual disks, and defrag them all.

    How can I go about doing this? I've read that using the disk manager that comes with VMWare Workstation (calling vmware-vdiskmanager) could possibly work, but AFAIK the datacenters that I need to take care of don't have it installed. I just need a way to execute an automated defragmentation command on each virtual disk, so manully defragmenting the disks through the guest OS is out of the question, I think.

    Thanks in advance!



  • 2.  RE: Powershell script to defrag all disks in vApp

    Posted May 29, 2014 05:09 PM

    You can use the VirtualDiskManager, something like this.

    The script will defrag all Flat vmdk, that are connected to the VM.

    $vmName = "MyVM"

    $si = Get-View ServiceInstance
    $vdiskMgr = Get-View $si.Content.virtualDiskManager

    $vm = Get-VM -Name $vmName
    $dc = &{
     
    $parent = $vm.ExtensionData
     
    while($parent -isnot [VMware.Vim.Datacenter]){
       
    $parent = Get-View -Id $parent.parent
      }
     
    $parent
    }
    Get-HardDisk -VM $vm -DiskType Flat | %{
     
    $vdiskMgr.DefragmentVirtualDisk($_.Filename,$dc.MoRef)
    }

    The VM needs to be powered off, since the DefragmentVirtualDisk method requires a lock on the file.



  • 3.  RE: Powershell script to defrag all disks in vApp

    Posted May 29, 2014 07:56 PM

    Can you please explain what the following lines of code do? For some unknown reason, only some of my VMs can get through that section of code fine. More specifically, PowerCLI specifies that the line $parent = Get-View -Id $parent.parent has a parameter that is Null.

    Here is the code seciton in question :

    $dc = &{

      $parent = $vm.ExtensionData

      while($parent -isnot [VMware.Vim.Datacenter]){

        $parent = Get-View -Id $parent.parent

      }

      $parent

    }

    Thank you!



  • 4.  RE: Powershell script to defrag all disks in vApp

    Posted May 29, 2014 08:54 PM

    Sure, that code-block finds the datacenter to which the VM belongs.

    It does that by ascending through the chain of parents, ultimately it should arrive at a datacenter.

    Do you know for which VMs this fails ?

    Must be something special about these VMs so that the datacenter to which they belong is not is not found.

    And the datacenter is needed because we use a datastore path to point to the VMDK.



  • 5.  RE: Powershell script to defrag all disks in vApp

    Posted May 30, 2014 02:35 PM

    Just looked into this, it seems like some of my VMs simply have a null Parent field in their extension data:

    http://puu.sh/97DFk/87d6ea291b.png

    I'm not sure what to think of this.



  • 6.  RE: Powershell script to defrag all disks in vApp

    Posted May 30, 2014 05:31 PM

    Very strange, where is that VM located in the vShere hierarchy ?

    Is it somewhere below a Datacenter ?

    Or is this specific for VMs that are part of a vApp ?