【问题标题】:Enumerating a PSCustomObject枚举 PSCustomObject
【发布时间】:2017-08-15 20:51:24
【问题描述】:

有谁知道从自定义对象“破坏”打开哈希表源的方法。自定义对象上没有 .getenumerrator 但我有这个哈希表:@{0=0.05; 1024=0.050; 51200=0.050; 512000=0.050; 1024000=0.045; 5120000=0.037}。我通过 Azure RateCard REST API 提取此信息,需要对其进行分解,以便我可以访问每一层费率以生成准确的使用成本报告。有什么建议么?示例输出:

MeterId          : d23a5753-ff85-4ddf-af28-8cc5cf2d3882
MeterName        : Standard IO - Page Blob/Disk (GB)
MeterCategory    : Storage
MeterSubCategory : Locally Redundant
Unit             : GB
MeterTags        : {}
MeterRegion      : 
MeterRates       : @{0=0.05; 1024=0.050; 51200=0.050; 512000=0.050; 1024000=0.045; 5120000=0.037}
EffectiveDate    : 2014-02-01T00:00:00Z
IncludedQuantity : 0.0

 TypeName: System.Management.Automation.PSCustomObject

Name             MemberType   Definition                                                                                                                           
----             ----------   ----------                                                                                                                           
Equals           Method       bool Equals(System.Object obj)                                                                                                       
GetHashCode      Method       int GetHashCode()                                                                                                                    
GetType          Method       type GetType()                                                                                                                       
ToString         Method       string ToString()                                                                                                                    
EffectiveDate    NoteProperty System.String EffectiveDate=2014-02-01T00:00:00Z                                                                                     
IncludedQuantity NoteProperty System.Decimal IncludedQuantity=0.0                                                                                                  
MeterCategory    NoteProperty System.String MeterCategory=Storage                                                                                                  
MeterId          NoteProperty System.String MeterId=d23a5753-ff85-4ddf-af28-8cc5cf2d3882                                                                           
MeterName        NoteProperty System.String MeterName=Standard IO - Page Blob/Disk (GB)                                                                            
MeterRates       NoteProperty System.Management.Automation.PSCustomObject MeterRates=@{0=0.05; 1024=0.050; 51200=0.050; 512000=0.050; 1024000=0.045; 5120000=0.037}
MeterRegion      NoteProperty System.String MeterRegion=                                                                                                           
MeterSubCategory NoteProperty System.String MeterSubCategory=Locally Redundant                                                                                     
MeterTags        NoteProperty System.Object[] MeterTags=System.Object[]                                                                                            
Unit             NoteProperty System.String Unit=GB                         

【问题讨论】:

    标签: powershell azure pscustomobject azure-billing-api


    【解决方案1】:

    不知道你说的break open是什么意思,但这应该会给你钥匙:

    $object.MeterRates.keys
    

    这将为您提供值:

    $object.MeterRates.keys | % {$object.MeterRates[$_]}
    

    你追求总价值吗?我猜它可能是这样的:

    ($object.MeterRates.keys | % {$object.MeterRates[$_] * $_} | Measure-Object -Sum).Sum
    

    【讨论】:

    • 钥匙?不确定你指的是什么价值。
    • 键正是你想要的——如果 MeterRates 是一个普通的哈希表。所以我建议您将@DaveSexton 的答案加入书签以供将来参考,因为它会在您学习 PowerShell 时派上用场。 (戴夫:如果您查看他的输入,您会发现这是一个自定义对象,而不是普通的哈希表,因此对于这个特定问题,蒙特雷发布的答案实际上是必要的。)
    【解决方案2】:

    可能有更好的方法来做到这一点,但我的特定版本的 powershell hacking 产生了类似的东西 -

    $a = "@{0=0.05; 1024=0.050; 51200=0.050; 512000=0.050; 1024000=0.045; 5120000=0.037}"
    
    $b = ConvertFrom-StringData `
            -StringData $a.Replace("; ","`n").Replace("@","").Replace("{","").Replace("}","")
    

    这假定整个字符串都可用,用换行符替换分号,丢弃其他位并将其提供给 ConvertFrom-StringData

    【讨论】:

      【解决方案3】:

      在类似问题上找到此代码。解决了我的问题:

      $js | Get-Member -MemberType NoteProperty).Name
      

      【讨论】:

      • 我知道这是旧的,但我不得不投反对票,因为它甚至不是有效的语法
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 2015-08-08
      • 2010-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多