Automation

 View Only
  • 1.  set DRS options for VMs

    Posted Sep 30, 2009 08:48 AM

    hi,

    i read the post Modify HA virtual machine options for guests and want to set drs options in similiar way (because set-vm is buggy).

    what i did is:

    function Set-DRSAutomationLevel

    {

    param($cluster, $vm, $DRSAutomationLevel)

    $cluster_view = Get-View -ViewType ClusterComputeResource -Filter @{"Name" = $cluster}

    $vm_view = Get-View -ViewType VirtualMachine -Filter @{"Name" = $vm}

    $operation = "add"

    if($cluster_view.ConfigurationEx.DrsVmConfig -ne $null)

    {

    foreach($guest in $cluster_view.ConfigurationEx.DrsVmConfig)

    {

    if($guest.Key.Value -eq $vm_view.MoRef.Value)

    {

    $operation = "edit"

    }

    }

    }

    $spec = New-Object VMware.Vim.ClusterConfigSpec

    $VMspec = New-Object VMware.Vim.ClusterDrsVmConfigSpec

    $VMspec.operation = $operation

    $VMspec.Info = New-Object VMware.Vim.ClusterDrsVmConfigInfo

    if ($DRSAutomationLevel -like "disabled")

    {

    $VMspec.Info.Enabled = $false

    }

    #elseif ($DRSAutomationLevel -like "default")

    #{

    #TBD

    #}

    else

    {

    $VMspec.Info.Enabled = $true

    $VMspec.Info.Behavior = $DRSAutomationLevel

    }

    $VMspec.Info.Key = $vm_view.MoRef

    $spec.DrsVmConfigSpec = @($VMspec)

    $task = Get-View ($cluster_view.ReconfigureCluster_Task($spec, $true))

    while($task.Info.Status -eq "running" -or $task.Info.Status -eq "queued")

    {

    $task.UpdateView()

    }

    }

    everything is working fine (thanks LucD), but i don't no how to set drs level to default (cluster setting).

    my understanding is that i need to remove custom setting of VM (if exists already) , but i don't no how to do that.

    any help appreciated,

    thanks



  • 2.  RE: set DRS options for VMs

    Posted Sep 30, 2009 08:54 AM

    This post should help: http://communities.vmware.com/thread/197308. Basically, you need to set the Behavior option to default.



  • 3.  RE: set DRS options for VMs

    Posted Sep 30, 2009 09:03 AM

    valid options for behavior are : fullyAutomated, partiallAutomated, manual

    there is no "default".

    if no custom option is specified, then vm uses default cluster setting. so you (well, actually I :smileyblush: ) need to delete this custom setting.

    the question is how...



  • 4.  RE: set DRS options for VMs
    Best Answer

    Broadcom Employee
    Posted Sep 30, 2009 02:15 PM

    Hi,

    To removed VM custom setting you need just to delete the entry for that vm setting ClusterDasVmConfigSpec.operation to "remove" and passing VM's MoRef as removeKey:

    function Set-DRSAutomationLevel{
    	param($cluster, $vm, $DRSAutomationLevel)
    	$cluster_view = Get-View -ViewType ClusterComputeResource -Filter @{"Name" = $cluster}
    	$vm_view = Get-View -ViewType VirtualMachine -Filter @{"Name" = $vm}
    	$operation = "add"
    	
    	if($cluster_view.ConfigurationEx.DrsVmConfig -ne $null) {
    		foreach($guest in $cluster_view.ConfigurationEx.DrsVmConfig) {
    			if($guest.Key.Value -eq $vm_view.MoRef.Value){
    				if ($DRSAutomationLevel -like "default") {
    					# remove the custom DRS setting for that VM
    					$operation = "remove"
    				} else {
    					$operation = "edit"
    				}
    			}
    		}
    	}
    	
    	if (($DRSAutomationLevel -like "default") -and ($operation -like "add")) {
    		# if there is no VM specific DRS setting - do nothing
    	}else {
    		$spec = New-Object VMware.Vim.ClusterConfigSpec
    		$VMspec = New-Object VMware.Vim.ClusterDrsVmConfigSpec
    		$VMspec.operation = $operation
    		$VMspec.Info = New-Object VMware.Vim.ClusterDrsVmConfigInfo
    		if ($DRSAutomationLevel -like "disabled"){
    			$VMspec.Info.Enabled = $false
    			$VMspec.Info.Key = $vm_view.MoRef
    		}elseif ($DRSAutomationLevel -like "default"){
    			$VMspec.removeKey = $vm_view.MoRef
    		}else {
    			$VMspec.Info.Enabled = $true
    			$VMspec.Info.Behavior = $DRSAutomationLevel
    		}
    	
    		$VMspec.Info.Key = $vm_view.MoRef
    		$spec.DrsVmConfigSpec = @($VMspec)
    		$cluster_view.ReconfigureCluster($spec, $true)
    	}
    }
    
    

    In addition "view" object have "blocking" task methods that will wait for task completion for you (i.e ReconfigureCluster instead of ReconfigureCluster_Task)

    Regards,

    Yasen



  • 5.  RE: set DRS options for VMs

    Posted Sep 30, 2009 03:35 PM

    thanks a lot! works just fine!