在 powershell 中访问 blob json 的键值对

Access key Value pairs of a blob json in powershell

我有一个 JSON ,就像这样(只发布初始片段):

[
  {
    "ICloudBlob":      {
      "StreamWriteSizeInBytes":  4194304,
      "ServiceClient":  {
        "AuthenticationScheme":  1,
        "BufferManager":  null,
        "Credentials":      {
          "SASToken":  null,
          "AccountName":
          "storageaccountappse9a4d",
          "KeyName":  null,
          "IsAnonymous":  false,
          "IsSAS":  false,
          "IsSharedKey":  true,
          "IsToken":  false,
          "SASSignature":  null
        },
        "BaseUri":
        "https://storageaccountappse9a4d.blob.core.windows.net/",
        "StorageUri":  {
          "PrimaryUri":
          "https://storageaccountappse9a4d.blob.core.windows.net/",
          "SecondaryUri":  null
        },
        "DefaultRequestOptions":  {
          "RetryPolicy":  {

          },
          "EncryptionPolicy":  null,
          "RequireEncryption":  null,
          "CustomerProvidedKey":  null,
          "EncryptionScope":  null,
          "AbsorbConditionalErrorsOnRetry":  null,
          "LocationMode":  0,
          "ServerTimeout":  null,
          "MaximumExecutionTime":  null,
          "NetworkTimeout":  null,
          "ParallelOperationThreadCount":  1,
          "SingleBlobUploadThresholdInBytes":  134217728,
          "UseTransactionalMD5":  null,
          "StoreBlobContentMD5":  null,
          "DisableContentMD5Validation":  null,
          "ChecksumOptions":  {
            "DisableContentMD5Validation":  null,
            "StoreContentMD5":  null,
            "UseTransactionalMD5":  null,
            "DisableContentCRC64Validation":  null,
            "UseTransactionalCRC64":  null
          }

我想访问 ParallelOperationThreadCount 参数,为此我使用了以下代码:

$JSON_obj=Get-AzStorageAccount | Get-AzStorageContainer | Get-AzStorageBlob | ConvertTo-JSON -Depth 
50
$ParallelOperationThreadCount=@()
foreach($i in $JSON_obj)
{
$ParallelOperationThreadCount+=$i.ICloudBlob.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount

 }
$ParallelOperationThreadCount

然而,在运行上,没有输出。它运行然后退出。 有什么方法可以找出可行的方法吗?

cmdlet ConvertTo-JSON 会将您的结果转换为 json 字符串。它不会 return 作为数组的结果。

所以我建议您删除第一行命令中的 cmdlet ConvertTo-JSON。这样做之后,命令 return 结果作为对象数组。然后您的脚本将 return 正确的结果。

例如(我在一个存储帐户中这样做)

Connect-AzAccount

$accountName =""
$groupName=""

$JSON_obj=(Get-AzStorageAccount -ResourceGroupName $groupName -Name $accountName  | Get-AzStorageContainer  | Get-AzStorageBlob )

$ParallelOperationThreadCount=@()
foreach($i in $JSON_obj)
{
$ParallelOperationThreadCount+=$i.ICloudBlob.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount

 }
$ParallelOperationThreadCount

此外,我们还可以将脚本简化如下。

$ParallelOperationThreadCount= (Get-AzStorageAccount -ResourceGroupName $groupName -Name $accountName  |
        Get-AzStorageContainer  | Get-AzStorageBlob|
        Select-Object -Property @{Name="ParallelOperationThreadCount";Expression={$_.ICloudBlob.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount}})

$ParallelOperationThreadCount


更新

请参考以下脚本

Connect-AzAccount

$accountName =""
$groupName=""

$JSON_obj=(Get-AzStorageAccount -ResourceGroupName $groupName -Name $accountName  | Get-AzStorageContainer  | Get-AzStorageBlob )

$ParallelOperationThreadCount=@()
foreach($i in $JSON_obj)
{
$ParallelOperationThreadCount+=$i.ICloudBlob.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount

 }
$ParallelOperationThreadCount

$JSON_obj|ConvertTo-JSON -Depth 50 | % { [System.Text.RegularExpressions.Regex]::Unescape($_) } | Out-File -Encoding Ascii - append C:\Users\rakshitas\Documents\json_excel\blob.json