powershell - 在powershell中访问blob json的键值对
问题描述
我有一个 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
但是,在运行此程序时,没有输出。它运行然后退出。有什么方法可以弄清楚什么可能有效?
解决方案
该 cmdletConvertTo-JSON
会将您的结果转换为 json 字符串。它不会将结果作为数组返回。
所以我建议你删除ConvertTo-JSON
第一行命令中的 cmdlet。之后,该命令将结果作为对象数组返回。然后您的脚本将返回正确的结果。
例如(我在一个存储帐户中这样做)
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