Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >PowerShell对返回结果展开对象或数组

PowerShell对返回结果展开对象或数组
EN

Stack Overflow用户
提问于 2021-04-08 17:59:56
回答 1查看 51关注 0票数 0

所以我不太精通PowerShell,我不知道如何让它以我想要的方式工作。我试着用谷歌搜索解决方案,但我找不到任何我想要做的事情。我不确定我是不是在寻找正确的东西。不管怎样,我要做的就是展开一个返回的对象。现在,当它只有一个时,它可以很好地工作,但当有多个时,它只输出一个数组。这是针对Veeam Powershell命令的,如果有帮助的话。

有问题的脚本

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
$clients = @()

foreach($tenant in Get-VBRCloudTenant | Select-Object Name) {
    $newTenant = Get-VBRCloudTenant -Name $tenant.Name | Select-Object * -ExcludeProperty SaltedPassword,PasswordSalt

    $clients += @{
        "LastActive" = $newTenant.LastActive;
        "Id" = $newTenant.Id;
        "Description" = $newTenant.Description;
        "Enabled" = $newTenant.Enabled;
        "LastResult" = $newTenant.LastResult;
        "Name" = $newTenant.Name;
        "Storage" = $newTenant.Resources;
    }
}

$clients | ConvertTo-Json

现在,如果我在PowerShell中运行它,我会得到以下输出。Get-VBRCloudTenant -Name "JobName" | Select-Object * -ExcludeProperty SaltedPassword,PasswordSalt | ConvertTo-Json

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
{
    "Enabled":  true,
    "LeaseExpirationEnabled":  false,
    "LeaseExpirationDate":  null,
    "Resources":  [
                      {
                          "Repository":  "Veeam.Backup.Core.CBackupRepository",
                          "RepositoryFriendlyName":  "",
                          "RepositoryQuota":  84934656,
                          "RepositoryQuotaPath":  "",
                          "UsedSpace":  37717110,
                          "PerformanceTierUsedSpace":  0,
                          "CapacityTierUsedSpace":  0,
                          "ArchiveTierUsedSpace":  0,
                          "UsedSpacePercentage":  44.4,
                          "WanAccelerationEnabled":  false,
                          "WanAccelerator":  null,
                          "Id":  "bc978335-a0e1-4c9f-9421-d19e215aefaa"
                      },
                      {
                          "Repository":  "Veeam.Backup.Core.CBackupRepository",
                          "RepositoryFriendlyName":  "",
                          "RepositoryQuota":  15728640,
                          "RepositoryQuotaPath":  "",
                          "UsedSpace":  15458718,
                          "PerformanceTierUsedSpace":  0,
                          "CapacityTierUsedSpace":  0,
                          "ArchiveTierUsedSpace":  0,
                          "UsedSpacePercentage":  98.3,
                          "WanAccelerationEnabled":  false,
                          "WanAccelerator":  null,
                          "Id":  "b31975e2-58bd-40d3-b6b6-61e76e4371ac"
                      },
                      {
                          "Repository":  "Veeam.Backup.Core.CBackupRepository",
                          "RepositoryFriendlyName":  "",
                          "RepositoryQuota":  52428800,
                          "RepositoryQuotaPath":  "",
                          "UsedSpace":  35513719,
                          "PerformanceTierUsedSpace":  0,
                          "CapacityTierUsedSpace":  0,
                          "ArchiveTierUsedSpace":  0,
                          "UsedSpacePercentage":  67.7,
                          "WanAccelerationEnabled":  false,
                          "WanAccelerator":  null,
                          "Id":  "4ba334eb-1316-46cf-a2f7-08a4f726a1ed"
                      }
                  ],
    "VMCount":  0,
    "ReplicaCount":  0,
    "LastActive":  "1/8/2021 1:09 PM",
    "LastResult":  "Success",
    "ResourcesEnabled":  true,
    "ReplicationResourcesEnabled":  false,
    "ThrottlingEnabled":  false,
    "ThrottlingValue":  1,
    "ThrottlingUnit":  1,
    "MaxConcurrentTask":  6,
    "ReplicationResources":  null,
    "WorkstationCount":  0,
    "ServerCount":  0,
    "BackupProtectionEnabled":  false,
    "BackupProtectionPeriod":  1,
    "Type":  0,
    "GatewaySelectionType":  0,
    "GatewayPool":  [

                    ],
    "GatewayFailoverEnabled":  false,
    "NewVMBackupCount":  0,
    "NewWorkstationBackupCount":  0,
    "NewServerBackupCount":  0,
    "RentalVMBackupCount":  0,
    "RentalWorkstationBackupCount":  0,
    "RentalServerBackupCount":  0,
    "RentalReplicaCount":  0,
    "NewReplicaCount":  0,
    "Id":  "10904f6b-1e5d-47dc-8fa6-364e627dd9ce",
    "Name":  "",
    "Description":  ""
}

所以这是预期的,所以我在Get-VBRCloudTenant上做了一个for each循环,我认为它可以工作,但它没有工作,我得到了这个输出。我正在做一个自定义对象,因为我不需要所有其他信息。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[
    {
        "Id":  "f1d3acd6-bd95-4ee7-8728-0243a4506523",
        "Description":  "",
        "Storage":  [
                        "f020393f-afe2-4340-9a9f-23420ce8af70"
                    ],
        "LastResult":  "Success",
        "Name":  "",
        "LastActive":  "2/25/2021 3:42 PM",
        "Enabled":  true
    },
    {
        "Id":  "10904f6b-1e5d-47dc-8fa6-364e627dd9ce",
        "Description":  "",
        "Storage":  [
                        "bc978335-a0e1-4c9f-9421-d19e215aefaa",
                        "b31975e2-58bd-40d3-b6b6-61e76e4371ac",
                        "4ba334eb-1316-46cf-a2f7-08a4f726a1ed"
                    ],
        "LastResult":  "Success",
        "Name":  "",
        "LastActive":  "1/8/2021 1:09 PM",
        "Enabled":  true
    }
]

如果有人能给我指出正确的方向,那就太好了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-08 18:32:44

解决了我的问题。那是ConvertTo-Json。默认情况下,它的最大深度只有2,所以当它到达Resources对象时,它就停止了。我将它的深度增加到3,它完全按照我想要的那样工作。

现在我只需要运行它来获得我想要的东西。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Get-VBRCloudTenant | Select-Object * -ExcludeProperty SaltedPassword,PasswordSalt | ConvertTo-Json -Depth 3
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67009470

复制
相关文章

相似问题

Powershell在pscustomobject中展开结果数组

10

Powershell数组或对象

30

如何在powershell数组中展开对象值

12

PowerShell -展开组-对象

135

从C#发送Powershell并从结果返回数组对象?

14
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文