PowerCLI

 View Only
Expand all | Collapse all

changing vm storage drs automation level

  • 1.  changing vm storage drs automation level

    Posted Jan 07, 2013 06:00 AM

    As per http://communities.vmware.com/message/2172311#2172311

    I'm using LucD's method, to extract a list of vm's that have vm's turned on

    function Get-StoragePod{
    <#
    .SYNOPSIS  Find a DatastoreCluster
    .DESCRIPTION The function will return a StoragePod object.
      This is the server-side object used by vSphere for a  DatastoreCluster.
    .NOTES  Author:  Luc Dekens
    .PARAMETER Name  The name of the DatastoreCluster
    .EXAMPLE
      PS> Get-StoragePod
    .EXAMPLE
      PS> Get-StoragePod -Name "SDC*"
    #>
      param(
      [CmdletBinding()]
      [parameter(Position = 0, ValueFromPipeline = $true)]
      [string]$Name = "*"  )
      begin{
       function Get-StoragePodInternal{
         param(
         [CmdletBinding()]
         [parameter(Mandatory = $true,ValueFromPipeline = $true)]
         [VMware.Vim.Folder]$Folder     )
         $Folder.ChildEntity | %{
           if($_.Type -eq "StoragePod"){
             Get-View -Id $_       }
           elseif($_.Type -eq "Folder"){
             Get-View -Id $_ | Get-StoragePodInternal
           }
         }
        }
      }
      process{
        Get-Folder -Name datastore | %{
          Get-StoragePodInternal -Folder $_.ExtensionData
        } | where {$_.Name -like $Name}
      }
    }
    $dsc = Get-StoragePod -Name $datastorecluster
    $vmlist = $dsc.PodStorageDrsEntry.StorageDrsConfig.VmConfig | where {$_.Enabled -notmatch "False"} | Select @{N="VM";E={(Get-View $_.VM).Name}},Enabled,IntraVmAffinity | where {$_.VM -like "*_replica"}

    This successfully gets a list of vm's that have sdrs enabled that shouldnt.

    I'm struggling with how to disable sdrs.  I'm new to powershell

    from what i can gather i need to update dsc.podstoragedrsentry.storagedrsconfig.vmconfig.enabled to False.

    but i'm unsure how to do this, or could point me in the right direction?



  • 2.  RE: changing vm storage drs automation level
    Best Answer

    Posted Jan 07, 2013 08:49 AM

    You will have to use the ConfigureStorageDrsForPod_Task method to change the settings for SDRS.

    In the StorageDrsConfigSpec object you can disable this.

    Combined with your Where-clause this could be something like this

    $storMgr = Get-View StorageResourceManager
    
    $pod
    = Get-DatastoreCluster -Name $datastorecluster
    $spec
    = New-Object VMware.Vim.StorageDrsConfigSpec $pod.ExtensionData.PodStorageDrsEntry.StorageDrsConfig.VmConfig | where {(Get-View $_.VM | Select -ExpandProperty Name) -like "*_replica"} | %{   $entry = New-Object VMware.Vim.StorageDrsVmConfigSpec
      $entry.Operation = "edit"
      $entry.Info = $_
     
    $entry.Info.Enabled = $false
      $spec.vmConfigSpec += $entry
    } $storMgr.ConfigureStorageDrsForPod($pod.ExtensionData.MoRef,$spec,$true)

    Note that the Get-DatastoreCluster cmdlet can be used to replace my function



  • 3.  RE: changing vm storage drs automation level

    Posted Jan 08, 2013 05:47 AM

    Cheers Mate!

    That's just what i needed, need to learn a bit more powershell to fully follow how to replicate how you did this but thanks :^)



  • 4.  RE: changing vm storage drs automation level

    Posted Jan 08, 2013 08:23 AM

    As a matter of fact this is PowerShell/PowerCLI using one the SDK API to change the settings.

    Not all vSphere API are available through PowerCLI cmdlets, but it is quite easy to call the SDK API directly from within PowerCLI/PowerShell.



  • 5.  RE: changing vm storage drs automation level

    Posted Aug 08, 2013 11:53 AM

    Hi!

    I am getting below errors while running this script, though sometimes it do the changes in VM SDRS automation but throwing this error.

    Exception calling "ConfigureStorageDrsForPod" with "3" argument(s): "The object has already been deleted or has not been completely created"

    At line:1 char:1

    + $storMgr.ConfigureStorageDrsForPod($pod.ExtensionData.MoRef,$spec,$true)

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

        + FullyQualifiedErrorId : VimException



  • 6.  RE: changing vm storage drs automation level

    Posted Sep 16, 2013 07:13 PM

    Hi rajan.  I am getting the same errors occasionally though, as you say, sometimes the rule gets correctly created.  Have you found any workaround or root cause?



  • 7.  RE: changing vm storage drs automation level

    Posted Nov 20, 2013 12:20 PM

    Hi,

    Sorry to update this thread, but I need to put all my automation level to 'default'.

    When VM are in Default mode, seems that everything is empty, example:

    Vm              : VirtualMachine-vm-13297
    Enabled         :
    Behavior        :
    IntraVmAffinity :

    IntraVmAntiAffinity :

    LinkedView      :
    DynamicType     :
    DynamicProperty :

    So I Try to do the same thing with a StorageDrsVmConfigSpec:

    $storMgr = Get-View StorageResourceManager

    $spec = New-Object VMware.Vim.StorageDrsConfigSpec

    $dsc = Get-Datastorecluster DS1

    $dsc.ExtensionData.PodStorageDrsEntry.StorageDrsConfig.VmConfig |%{

    $entry = New-Object VMware.Vim.StorageDrsVmConfigSpec

    $entry.Operation = "edit"

    $entry.Info = $_

    $entry.Info.Enabled = ""

    $entry.Info.Behavior = ""

    $entry.Info.IntraVMAffinity = ""

    $spec.vmConfigSpec += $entry

    }

    $storMgr.ConfigureStorageDrsForPod($dsc.ExtensionData.MoRef,$spec,$true)

    But ... no luck:

    Exception calling "ConfigureStorageDrsForPod" with "3" argument(s): "A specified parameter was not correct.

    spec"


    Any idea ? :-)



  • 8.  RE: changing vm storage drs automation level

    Posted Nov 20, 2013 03:17 PM

    Couple things here- The reason you're getting an error is because you're setting the Info properties to empty strings, which is different than null.  Empty string is an invalid value.

    But I was unable to get it to say "Default" with the "Edit" operation.  I could set it to "Fully Automated" by (info.behavior="automated" and info.enabled=$true), but that isn't exactly what you wanted.  Setting them to $null didn't work as I expected.

    But it seems that the "Add" operation instead of "Edit" will overwrite the existing configuration for a given VM, allowing you to get back to defaults.

    I think this should get you what you want:

    $storMgr = Get-View StorageResourceManager              
    $spec = New-Object VMware.Vim.StorageDrsConfigSpec              
    $dsc = Get-Datastorecluster DS1             
    get-vm -Datastore $dsc |%{              
        $vmEntry = New-Object VMware.Vim.StorageDrsVmConfigSpec            
        $vmEntry.Operation = "add"            
        $vmEntry.Info = New-Object VMware.Vim.StorageDrsVmConfigInfo            
        $vmEntry.Info.Vm = $_.ExtensionData.MoRef            
        $spec.vmConfigSpec += $vmEntry             
    }              
    $storMgr.ConfigureStorageDrsForPod($dsc.ExtensionData.MoRef,$spec,$true)


  • 9.  RE: changing vm storage drs automation level

    Posted Nov 20, 2013 03:23 PM

    It's exactly what I want and it works perfectly ! :smileyhappy:

    Thanks !



  • 10.  RE: changing vm storage drs automation level

    Posted May 07, 2013 06:51 AM

    This script seemed to be working as expected, but recently I noticed that New VM's that are added to the datastore cluster won't appear when you run $pod.ExtensionData.PodStorageDrsEntry.StorageDrsConfig.VmConfig

    I haven't been able to get them detected and working, Does anyone know if there is anything that needs to be done after adding a vm to have it appear from powercli when looking at members of a SDRS cluster?



  • 11.  RE: changing vm storage drs automation level

    Posted May 07, 2013 11:11 AM

    I suspect that the new information is not present.

    Remember that the objects you get in PowerCLI are read-only copies.

    You can force a refresh with

    $pod.ExtensionData.UpdateViewData()

    Does that help ?



  • 12.  RE: changing vm storage drs automation level

    Posted May 08, 2013 01:58 AM

    Thanks for the quick response LucD

    No luck,

    Ran $pod.ExtensionData.UpdateViewData() then  $pod.ExtensionData.PodStorageDrsEntry.StorageDrsConfig.VmConfig

    the new VM won't show in powercli, however shows fine in GUI

    Any idea on how often it refreshes?



  • 13.  RE: changing vm storage drs automation level

    Posted May 08, 2013 04:55 AM

    The PowerCLI object does not refresh, unless you do an UpdateViewData.

    Did you try fetching the datastorecluster object again

    $pod = Get-DatastoreCluster -Name $datastorecluster



  • 14.  RE: changing vm storage drs automation level

    Posted May 14, 2013 07:46 AM

    Hi LucD

    I tried $pod.ExtensionData.PodStorageDrsEntry.StorageDrsConfig.VmConfig

    then

    $pod.ExtensionData.UpdateViewData()

    then

    $pod = Get-DatastoreCluster -Name $datastorecluster

    The VM still doesnt appear when I do a $pod.ExtensionData.PodStorageDrsEntry.StorageDrsConfig.VmConfig

    VM was added more than a week ago, so it doesnt seem to be an internal refresh timer.



  • 15.  RE: changing vm storage drs automation level

    Posted May 14, 2013 09:29 AM

    I did some more tests, and I can confirm the behavior you are seeing.

    When a new VM is created on the datastorecluster it is not automatically added to the VmConfig array as a StorageDrsVmConfigInfo entry.

    The fact that you see it in the vSphere client is due to the defaults (see for example the description of the enabled property).

    I'm not sure if this is intentional or a "feature".

    My guess is that this is intentional, as long as you don't divert from the defaults, there is no need to create an entry.

    Perhaps some SDRS wizard can shine his light on this ?



  • 16.  RE: changing vm storage drs automation level

    Posted May 23, 2013 04:42 AM

    Just checking tho, there is no way to update this from the VM side?



  • 17.  RE: changing vm storage drs automation level

    Posted Oct 02, 2017 04:59 PM

    Not sure if this was ever figured out, but you can build out the same objects using Get-Datastore | Get-VM instead of grabbing the list of VMs from the datastore cluster settings which appears to have issues. The altered code is below:

    $pod | Get-VM | Where-Object {$_.Name -like "*volume*"} |

    %{

      $_.Name

      $entry = New-Object VMware.Vim.StorageDrsVmConfigSpec

      $entry.Info = New-Object VMware.Vim.StorageDrsVmConfigInfo

      $entry.Operation = "edit"

      $entry.Info.VM = $_.ExtensionData.MoRef

      $entry.Info.Enabled = $false

      $spec.vmConfigSpec += $entry

    }



  • 18.  RE: changing vm storage drs automation level

    Posted May 09, 2013 07:12 PM

    I'm having the same problem where newly added VMs to the datastore cluster failed to show up in ~StorageDrsConfig.VmConfig.  One way I've found to get them to show up is to enable/disable storage DRS from vSphere Client on the datastore cluster.  For some reason, after doing that from the vSphere GUI, the VMs show up then in ~StorageDrsConfig.VmConfig.  I tried using Set-DatastoreCluster to change the sdrsautomationlevel to manual / disabled, but no luck in getting the VMs to show up.  So far, only after editing the datastore cluster settings from vSphere client.  Not sure if it might be an issue with the vCenter Server not passing the information along or what.  I'd be curious to know what versions you're using.  I'm using ESXi 5.1U1, vCenter Server 5.1U1, and PowerCLI 5.1 release 1.



  • 19.  RE: changing vm storage drs automation level

    Posted May 09, 2013 08:41 PM

    The changes you make in vCenter are not automatically done in your PowerCLI variables.

    You will have to get the object again, or refresh the ExtensionData property with the UpdateViewData method.



  • 20.  RE: changing vm storage drs automation level

    Posted May 09, 2013 09:13 PM

    Sorry, forgot to mention that I re-fetched the datastore cluster object after adding VMs to it, and also after changing the sdrsautomationlevel with Set-DatastoreCluster. Unfortunately, it still had nothing in ~.StorageDrsConfig.VmConfig.  Only after making the changes in vCenter and getting the object again after making the changes did the VMs finally show up in ~.StorageDrsConfig.VmConfig.



  • 21.  RE: changing vm storage drs automation level

    Posted May 09, 2013 09:16 PM

    The ExtensionData property is handled a bit differently, compared to regular PowerShell variables and their properties.

    Did you also try with the UpdateViewData method as I suggested ?

    It might be a caching problem.



  • 22.  RE: changing vm storage drs automation level

    Posted May 09, 2013 09:20 PM

    Yeah, I tried that too to no avail :smileysad:



  • 23.  RE: changing vm storage drs automation level

    Posted May 09, 2013 09:37 PM

    I'll check if I can reproduce that