VMware Cloud Community
fujie_fu
Contributor
Contributor

How to set customer property to datastore via powercli?

How to set customer property to datastore via powercli? the query commands: PS D:\powercli> $datastore1.ExtensionData.CustomValue.Get(0) PS D:\powercli> $datastore1.ExtensionData.AvailableField.Get(0)
0 Kudos
5 Replies
LucD
Leadership
Leadership

Currently, the New-CustomAttribute and Set-Annotation cmdlets do not yet support Datastore objects.
But you can use the CustomFieldsManager methods to cerate and set a CA for a Datatsore.

For example

$dsName = 'MyDS'
$caName = 'DatastoreCA'
$caValue = 'MyValue'

$ds = Get-Datastore -Name $dsName
$caMgr = Get-View CustomFieldsManager
$ca = $caMgr.Field.Where{ $_.Name -eq $caName }
if (-not $ca) {
  $ca = $caMgr.AddCustomFieldDef($caName, 'Datastore', $null, $null)
}
$caMgr.SetField($ds.Id, $ca.Key, $caValue)

# Retrieve the CA
$ds.ExtensionData.CustomValue.where{ $_.Key -eq $ca.Key }


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

0 Kudos
fujie_fu
Contributor
Contributor

Got it! well, how to delete a CA for a Datatsore ?
0 Kudos
LucD
Leadership
Leadership

Since a CA is defined on the vCenter level, you have 2 options

- set the Value for the CA for a specific datastore to an empty string.

- remove the CA altogether for all Datastores

You can not remove the CA for 1 specific Datastore.
Which of the 2 options do you have in mind?


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

0 Kudos
fujie_fu
Contributor
Contributor

Thanks very much! i know it. - set the Value for the CA for a specific datastore to an empty string. $caMgr.SetField($ds.Id, $ca.Key, '') - remove the CA altogether for all Datastores $caMgr.RemoveCustomFieldDef(402)
0 Kudos
LucD
Leadership
Leadership

So why the question if you knew it?


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

0 Kudos