marc045
Contributor
Contributor

How to set a CustomField on a datastore?

Jump to solution

Hi All,

I have tried:

get-datastore myDS | set-customfield -name myName -value myValue

but no luck.

I also note:

$myDS = get-datastore myDS

$myDS.extensiondata

There is a field called:

"CustomValue"

How do I populate this extensiondata.CustomValue field?

Even if I can't set a customfield via set-customfield, is there a workaround or a hack to store a custom value for a datastore?

regards

marc0

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

I'm afraid the New-CustomField and New-CustomAttribute in the current PowerCLI build do not support datastores.

But you can use the CustomFieldManager to handle custom fields on datastores.

First get the CustomFieldManager object.

$cfMgr = Get-View customfieldsmanager

To create a new field you can do

$cfMgr.AddCustomFieldDef("Test","Datastore",$null,$null)

This method will return the CustomField object. It is important to store this somewhere, because you will need the Key property in the following methods.

You can also get this key by listing the available fields for any datastore

PS C:\> $ds = Get-Datastore -Name MyDS

PS C:\> $ds.ExtensionData.AvailableField


Key                     : 591
Name                    : Test
Type                    : string
ManagedObjectType       : Datastore
FieldDefPrivileges      :
FieldInstancePrivileges :
DynamicType             :

To assign a value to the new field

$cfMgr.SetField($ds.Extensiondata.MoRef,591,"A value")

To retrieve the value of the custom field

$ds.ExtensionData.CustomValue | where {$_.Key -eq 591}


Value                                          Key DynamicType              DynamicProperty
-----                                          --- -----------              ---------------
A value                                        591

Note that these datastore custom fields are not visible in the vSphere client !


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership

I'm afraid the New-CustomField and New-CustomAttribute in the current PowerCLI build do not support datastores.

But you can use the CustomFieldManager to handle custom fields on datastores.

First get the CustomFieldManager object.

$cfMgr = Get-View customfieldsmanager

To create a new field you can do

$cfMgr.AddCustomFieldDef("Test","Datastore",$null,$null)

This method will return the CustomField object. It is important to store this somewhere, because you will need the Key property in the following methods.

You can also get this key by listing the available fields for any datastore

PS C:\> $ds = Get-Datastore -Name MyDS

PS C:\> $ds.ExtensionData.AvailableField


Key                     : 591
Name                    : Test
Type                    : string
ManagedObjectType       : Datastore
FieldDefPrivileges      :
FieldInstancePrivileges :
DynamicType             :

To assign a value to the new field

$cfMgr.SetField($ds.Extensiondata.MoRef,591,"A value")

To retrieve the value of the custom field

$ds.ExtensionData.CustomValue | where {$_.Key -eq 591}


Value                                          Key DynamicType              DynamicProperty
-----                                          --- -----------              ---------------
A value                                        591

Note that these datastore custom fields are not visible in the vSphere client !


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

View solution in original post

0 Kudos
marc045
Contributor
Contributor

Thanks Luc, great stuff.

0 Kudos