You can retrieve a datastore compatible with storage policy and create a virtual machine in the datastore.
Prerequisites
- Verify that you are connected to a vCenter Server system.
- Verify that a tag-based storage policy named Tag-Policy exists in the vCenter Server environment.
- Verify that the tag of the Tag-Policy storage policy is associated with one of the available datastores in the vCenter Server environment.
Procedure
- Get the tag-based Tag-Policy storage policy and store it in the $policy variable.
$policy = Get-SpbmStoragePolicy -Name 'Tag-Policy'
- Get the tag used in the Tag-Policy storage policy and store it in the $tag variable.
$tag = ($($($policy.AnyOfRulesets).AllOfRules).AnyOfTags)[0]
- Get a datastore compatible with the $policy storage policy and store it in the $ds variable.
$ds = Get-SpbmCompatibleStorage -StoragePolicy $policy
- Get the virtual machine host that contains the $ds datastore and store it in the $vmhost variable.
$vmHost = Get-VMHost -Datastore $ds
- Create a virtual machine named VM-Tag in the $ds datastore and store the virtual machine object in the $vm variable.
$vm = New-VM -Name 'VM-Tag' -ResourcePool $vmHost -Datastore $ds -NumCPU 2 -MemoryGB 4 -DiskMB 1
- Associate the $policy storage policy with the $vm virtual machine.
Set-SpbmEntityConfiguration $vm -StoragePolicy $policy
- Verify that the $vm virtual machine is compliant with the $policy storage policy.
Get-SpbmEntityConfiguration $vm
The status should be
Compliant.
- Get the
Tag-Assignment
object for the $ds datastore and store it in the $tagAs variable.
$tagAs = Get-TagAssignment -Entity $ds
- Remove the $tag tag association from the $ds datastore.
Remove-TagAssignment -TagAssignment $tagAs
- Check the compliance of the $vm virtual machine with the $policy storage policy.
Get-SpbmEntityConfiguration $vm
The status should be
NonCompliant.