枚举PSCustomObject

时间:2015-11-25 21:43:51

标签: powershell azure pscustomobject azure-billing-api

有没有人知道从自定义对象中“打破”哈希表源的方法。自定义对象上没有.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                         

3 个答案:

答案 0 :(得分:1)

不确定你的意思是什么是打开,但这应该给你钥匙:

$object.MeterRates.keys

这将为您提供值:

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

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

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

答案 1 :(得分:0)

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

$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

答案 2 :(得分:0)

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

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