【问题标题】:Replace values in powershell object替换 powershell 对象中的值
【发布时间】:2021-06-14 22:33:39
【问题描述】:

使用 powershell 为我的 VMware 环境设置 HA 数据存储。我只是不知道如何替换某个值。

$Cluster = Get-Cluster $ClusterName | Get-View
$HAInfo = $Cluster.Configuration.DasConfig

$HAinfo 的结果是这样的:

Enabled                    : True
VmMonitoring               : vmMonitoringDisabled
HostMonitoring             : enabled
VmComponentProtecting      : disabled
FailoverLevel              : 1
AdmissionControlPolicy     : VMware.Vim.ClusterFailoverResourcesAdmissionControlPolicy
AdmissionControlEnabled    : True
DefaultVmSettings          : VMware.Vim.ClusterDasVmSettings
Option                     : {das.ignoreRedundantNetWarning}
HeartbeatDatastore         : {Datastore-datastore-2367254, Datastore-datastore-1586741}
HBDatastoreCandidatePolicy : userSelectedDs
LinkedView                 :

现在我对 HeartbeatDatastore 感兴趣,它现在包含:

Type      Value            
----      -----            
Datastore datastore-2367254
Datastore datastore-1586741

我需要用新值替换值。我可以很容易地通过写来做到这一点:

$Hainfo.HeartbeatDatastore[1].value = "newvalue"

但我不能确定它是否包含 0、1 或 2 个值。我的问题是,例如,当它只包含 1 行(数据存储,datastore-2367254)时,我不知道应该如何添加具有新值的新行。

不确定是否需要这些额外信息:

$Hainfo.HeartbeatDatastore | get-member


   TypeName: VMware.Vim.ManagedObjectReference

Name        MemberType Definition                    
----        ---------- ----------                    
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()             
GetType     Method     type GetType()                
ToString    Method     string ToString()             
Type        Property   string Type {get;set;}        
Value       Property   string Value {get;set;}  

请帮忙。

【问题讨论】:

  • 需要更换的数据存储是否始终具有 ID datastore-1586741
  • 很遗憾,由于$hainfo.HeartbeatDatastore是一个固定大小的集合,你必须使用效率低下的+=添加到集合中 --> $Hainfo.HeartbeatDatastore += 'Datastore-datastore-1586741。请注意前面的Datastore-,因为显然该对象类型操纵了一个集合字符串并假定第一个字符直到第一个- 是数据类型。

标签: arrays powershell object


【解决方案1】:

这样解决了:

$HAInfo.HeartbeatDatastore.Clear()
$dsObj1 = New-Object VMWare.Vim.ManagedObjectReference
$dsObj1 = here I fill it with the values I converted
$HAInfo.HeartbeatDatastore += $dsObj1

$dsObj2 = New-Object VMWare.Vim.ManagedObjectReference
$dsObj2 = here I fill it with the values I converted
$HAInfo.HeartbeatDatastore += $dsObj2

【讨论】:

    猜你喜欢
    • 2021-06-14
    • 2021-06-11
    • 2022-01-22
    • 2019-09-17
    • 2022-07-26
    • 2015-04-22
    • 2022-11-17
    • 2019-07-23
    • 1970-01-01
    相关资源
    最近更新 更多