VMware Cloud Community
stacycarter
Enthusiast
Enthusiast

PowerCLI - Get SDRS datastore recommendation to deploy VM via Scorch

In absence of vRA, I'm trying to work on some deployment runbooks for VMs in System Center Orchestrator (Scorch) that can be published in Service Manager. It appears that Scorch does not support datastore clusters/SDRS in the Clone Windows VM activity.

Any ideas using PowerCLI?  Is there a way via PowerCLI to get a recommended datastore name that I can then use/pass along to the Clone Windows VM activity?

Thanks.

15 Replies
LucD
Leadership
Leadership

Do you have the Integration Pack for VMware vSphere installed in SCCM?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
stacycarter
Enthusiast
Enthusiast

Hi LucD​ - We have the vSphere Integration pack for System Center Orchestrator (SCORCH, not SCCM) installed.  Was trying to use Clone Windows VM, which is one of the activities in that integration pack.  It doesn't appear to support datastore clusters/SDRS, just datastores.

Reply
0 Kudos
LucD
Leadership
Leadership

Sorry, I have no knowledge nor experience with SCORCH I'm afraid.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
stacycarter
Enthusiast
Enthusiast

Just to add LucD​ - In the Scorch runbook, I could run a regular PowerShell/PowerCLI script right before the "Clone Windows VM" activity to prune out the recommenced datastore (for whatever VMware cluster is chosen in the runbook logic) and pass it along to that activity.  But not sure if that is possible with PowerCLI or how to do that.

Reply
0 Kudos
LucD
Leadership
Leadership

What do you mean with "...prune out the recommenced datastore...".

Is that the recommended datastore by SDRS?

We could get that via the RecommendDatastores method.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
stacycarter
Enthusiast
Enthusiast

LucD​ - Yes, trying to understand how to use this method (which I saw mentioned in William Lam's old blog post too - http://www.virtuallyghetto.com/2012/03/vm-provisioning-on-datastore-clusters.html ).  I basically want to get the top recommended datastore name for a cluster (or datastore cluster) via PowerCLI.  I've never used this method before. 

Reply
0 Kudos
LucD
Leadership
Leadership

You will need to know the name of the DatastoreCluster, the name of the cluster where the VM shall go, and the amount of diskspace the VM will require.

Note that the script only assumes a single disk that is Thin provisioned.
The result can be one or more datastores in the datastorecluster, you should look at the Rating to find the optimal one (according to SDRS).
If no datastore can be selected by SDRS, nothing will be returned.

$clusterName = 'ClusterName'

$dscName = 'DatastoreCluster'

$hdSizeGB = 20

$dsc = Get-DatastoreCluster -Name $dscName

$cluster = Get-Cluster -Name $clusterName

$rp = Get-ResourcePool -Name Resources -Location $cluster

$pod = New-Object VMware.Vim.StorageDrsPodSelectionSpec

$pod.storagePod = $dsc.ExtensionData.MoRef

$storMgr = Get-View StorageResourceManager

$spec = New-Object VMware.Vim.StoragePlacementSpec

$spec.type = [VMware.Vim.StoragePlacementSpecPlacementType]::create

$pod = New-Object VMware.Vim.StorageDrsPodSelectionSpec

$pod.storagePod = $dsc.ExtensionData.MoRef

$spec.PodSelectionSpec = $pod

$configSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

$disk = New-Object VMware.Vim.VirtualDisk

$disk.CapacityInKB = $hdSizeGB * 1MB

$backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo

$backing.ThinProvisioned = $true

$backing.DiskMode = 'persistent'

$backing.EagerlyScrub = $false

$backing.FileName = ''

$disk.Backing = $backing

$vDisk = New-Object VMware.Vim.VirtualDeviceConfigSpec

$vDisk.Device = $disk

$vDisk.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::add

$configSpec.DeviceChange += $vDisk

$spec.ConfigSpec = $configSpec

$spec.ResourcePool = $rp.ExtensionData.MoRef

$result = $storMgr.RecommendDatastores($spec)

if(!$result.DrsFault){

    foreach($rec in $result.Recommendations){

        $rec.Action |

        Select @{N='Datastore';E={Get-View -Id $_.Destination -Property Name | Select -ExpandProperty Name}},

            Type,

            @{N='Rating';E={$rec.Rating}},

            @{N='Reason';E={$rec.Reason}}

    }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

V00Z11
Enthusiast
Enthusiast

Hi Luc,

I tried using this code but I always get back a result, no matter how large the $hdSizeGB value is.

Here is an example from my dev environment: The DatastoreCluster has 1,4 TB of free disk space and consists of currently one datastore.

PS C:\> Get-DatastoreCluster N99-N02-NN

Name                      CapacityGB      FreeSpaceGB     SdrsAutomationLevel

----                      ----------      -----------     -------------------

N99-N02-NN                3.999,750       1.429,349       FullyAutomated

PS C:\> Get-DatastoreCluster N99-N02-NN | Get-Datastore

Name                               FreeSpaceGB      CapacityGB

----                               -----------      ----------

N99-N02-NN01                         1.429,349       3.999,750

I set $hdSizeGB to 2000, so that it would not fit and ran the code, but it is returning a result.

Datastore    Type               Rating Reason

---------    ----               ------ ------

N99-N02-NN01 StoragePlacementV1      1 storagePlacement

Here is the content of a couple of objects:

PS C:\> $result

Recommendations DrsFault Task LinkedView

--------------- -------- ---- ----------

{1324}

-------------------------------------------------------------------------------------

PS C:\> $result.Recommendations

Key            : 1324

Type           : V1

Time           : 07.03.2019 15:47:17

Rating         : 1

Reason         : storagePlacement

ReasonText     : Satisfy storage initial placement requests

WarningText    :

WarningDetails :

Prerequisite   :

Action         : {VMware.Vim.StoragePlacementAction}

Target         : StoragePod-group-p341

-------------------------------------------------------------------------------------

PS C:\> $result.Recommendations.Action

Vm                :

RelocateSpec      : VMware.Vim.VirtualMachineRelocateSpec

Destination       : Datastore-datastore-340

SpaceUtilBefore   : 64,26405

SpaceDemandBefore : 64,75349

SpaceUtilAfter    : 64,26562

SpaceDemandAfter  : 64,75566

IoLatencyBefore   : 0,34394

LinkedView        :

Type              : StoragePlacementV1

Target            :

-------------------------------------------------------------------------------------

PS C:\> $result.Recommendations.Action.RelocateSpec

Service      :

Folder       :

Datastore    : Datastore-datastore-340

DiskMoveType : moveAllDiskBackingsAndAllowSharing

Pool         :

Host         :

Disk         :

Transform    :

DeviceChange :

Profile      :

LinkedView   :

-------------------------------------------------------------------------------------

PS C:\> $spec

Type                      : create

Priority                  :

Vm                        :

PodSelectionSpec          : VMware.Vim.StorageDrsPodSelectionSpec

CloneSpec                 :

CloneName                 :

ConfigSpec                : VMware.Vim.VirtualMachineConfigSpec

RelocateSpec              :

ResourcePool              : ResourcePool-resgroup-311

Host                      :

Folder                    :

DisallowPrerequisiteMoves :

ResourceLeaseDurationSec  :

LinkedView                :

-------------------------------------------------------------------------------------

PS C:\> $spec.ConfigSpec.DeviceChange.Device

CapacityInKB               : 2097152000

CapacityInBytes            :

Shares                     :

StorageIOAllocation        :

DiskObjectId               :

VFlashCacheConfigInfo      :

Iofilter                   :

VDiskId                    :

NativeUnmanagedLinkedClone :

Key                        : 0

DeviceInfo                 :

Backing                    : VMware.Vim.VirtualDiskFlatVer2BackingInfo

Connectable                :

SlotInfo                   :

ControllerKey              :

UnitNumber                 :

-------------------------------------------------------------------------------------

PS C:\> $spec.ConfigSpec.DeviceChange.Device.Backing

DiskMode               : persistent

Split                  :

WriteThrough           :

ThinProvisioned        : False

EagerlyScrub           : False

Uuid                   :

ContentId              :

ChangeId               :

Parent                 :

DeltaDiskFormat        :

DigestEnabled          :

DeltaGrainSize         :

DeltaDiskFormatVariant :

Sharing                :

KeyId                  :

LinkedView             :

FileName               :

Datastore              :

BackingObjectId        :

Do you have any clue why it is coming up with a recommendation instead of setting the DrsFault value and not returning a recommendation?

I tried using the code against a 6.5 and 6.7 vCenter.

Thanks!!

Reply
0 Kudos
LucD
Leadership
Leadership

The disk is defined as Thin, that probably explains why it comes back with that suggestion.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
V00Z11
Enthusiast
Enthusiast

Thank you for your reply!

I tried setting $backing.ThinProvisioned to $false but it is still returning a recommendation.

Do you have any other clues on what might be missing?

Reply
0 Kudos
LucD
Leadership
Leadership

Seems it also needs the PodDiskLocator.

Try like this

$clusterName = 'ClusterName'

$dscName = 'DatastoreCluster'

$hdSizeGB = 100

$dsc = Get-DatastoreCluster -Name $dscName

$cluster = Get-Cluster -Name $clusterName

$rp = Get-ResourcePool -Name Resources -Location $cluster

$esx = Get-VMHost -Location $cluster | Get-Random

$dc = Get-Datacenter -Cluster $cluster

$folder = Get-Folder -Name vm -Location $dc

$pod = New-Object VMware.Vim.StorageDrsPodSelectionSpec

$pod.storagePod = $dsc.ExtensionData.MoRef

$storMgr = Get-View StorageResourceManager

$spec = New-Object VMware.Vim.StoragePlacementSpec

$spec.type = [VMware.Vim.StoragePlacementSpecPlacementType]::create


$pod = New-Object VMware.Vim.StorageDrsPodSelectionSpec

$pod.storagePod = $dsc.ExtensionData.MoRef

$initialVmConfig = New-Object VMware.Vim.VmPodConfigForPlacement

$initialVmConfig.StoragePod = $dsc.ExtensionData.MoRef

$pDisk = New-Object VMware.Vim.PodDiskLocator

$pDisk.DiskBackingInfo = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo

$pDisk.DiskBackingInfo.FileName = ''

$pDisk.DiskBackingInfo.EagerlyScrub = $false

$pDisk.DiskBackingInfo.ThinProvisioned = $false

$pDisk.DiskBackingInfo.DiskMode = 'persistent'

$initialVmConfig.Disk += $pDisk

$pod.InitialVmConfig += $initialVmConfig

$spec.PodSelectionSpec = $pod


$configSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

$disk = New-Object VMware.Vim.VirtualDisk

$disk.CapacityInKB = $hdSizeGB * 1MB

$backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo

$backing.ThinProvisioned = $false

$backing.DiskMode = 'persistent'

$backing.EagerlyScrub = $false

$backing.FileName = ''

$disk.Backing = $backing

$vDisk = New-Object VMware.Vim.VirtualDeviceConfigSpec

$vDisk.Device = $disk

$vDisk.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::add

$vDisk.FileOperation = [VMware.Vim.VirtualDeviceConfigSpecFileOperation]::create

$configSpec.DeviceChange += $vDisk


$spec.ConfigSpec = $configSpec

$spec.ResourcePool = $rp.ExtensionData.MoRef

$spec.Host = $esx.ExtensionData.MoRef

$spec.Folder = $folder.ExtensionData.MoRef


$result = $storMgr.RecommendDatastores($spec)

if (!$result.DrsFault) {

   foreach ($rec in $result.Recommendations) {

   $rec.Action |

  Select @{N = 'Datastore'; E = {Get-View -Id $_.Destination -Property Name | Select -ExpandProperty Name}},

  Type,

   @{N = 'Rating'; E = {$rec.Rating}},

   @{N = 'Reason'; E = {$rec.Reason}}

   }

}

else {

   $result.DrsFault.FaultsByVm.Fault.LocalizedMessage

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

V00Z11
Enthusiast
Enthusiast

You are the master!

It is working perfectly. Thanks alot!!

Reply
0 Kudos
V00Z11
Enthusiast
Enthusiast

The code is working perfectly with a 6.7 vCenter. But when I try using it with a 6.5 vCenter, then I'm getting the following error.

Exception calling "RecommendDatastores" with "1" argument(s): "The object 'vim.StoragePod:group-p6986' has already been deleted or has not been completely created"

At line:1 char:1

+ $result = $storMgr.RecommendDatastores($spec)

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

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

    + FullyQualifiedErrorId : VimException

The DatastoreCluster exists has the correct id.

PS C:\> $dsc.id

StoragePod-group-p6986

PS C:\> Get-DatastoreCluster -Id StoragePod-group-p6986

Name                      CapacityGB      FreeSpaceGB     SdrsAutomationLevel

----                      ----------      -----------     -------------------

AL1-R01-NN                15.999,250      8.270,476       FullyAutomated

Do you have any chance to try it out against a 6.5 vCenter?

Reply
0 Kudos
LucD
Leadership
Leadership

Unfortunately I don't have a 6.5 environment anymore in my lab.

Is there any information in the vpxd.log at the time you get the error message?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
V00Z11
Enthusiast
Enthusiast

When I execute $result = $storMgr.RecommendDatastores($spec) ,multiple of the following events are logged with different VM names.

2019-03-11T06:48:32.228Z info vpxd[7FB3FD62C700] [Originator@6876 sub=sdrsLogger opID=460529c4] HandleHiddenFiles: placeholder Key=27, Name=[AL1-R01-NN01] myvmname/myvmname-000002.vmdk, Type=diskDescriptor, Size=0 bytes

2019-03-11T06:48:33.423Z warning vpxd[7FB3FD62C700] [Originator@6876 sub=sdrsLogger opID=460529c4] RebalDisk: VM myvmname disk 2164339007 datastore AL1-R01-NN01 backingUsed 945 committed 945

There is only one error meesage, which is the following (including the preceding entry).

2019-03-11T06:48:33.854Z info vpxd[7FB3FDCB9700] [Originator@6876 sub=vpxLro opID=lro-2-1ba02ece-2a74] [VpxLRO] -- BEGIN lro-29428646 --  -- AskRefreshSdrsRecLro --

2019-03-11T06:48:34.009Z error vpxd[7FB3FDCB9700] [Originator@6876 sub=MoStoragePod opID=lro-2-1ba02ece-2a74] [StoragePodMo::GetCorrelatedDatastorePairs] Received exception from SMS: vim.fault.NotFound

We are planning to upgrade that vCenter to 6.7 anytime soon. So from my side you don't really have to dig any deeper because it is working with 6.7. I just thought that maybe there is a quick way to look into this.

Thank you again for your help!

Reply
0 Kudos