0

所以我对 PowerShell 不是很精通,也不知道如何按照我想要的方式进行这项工作。我已经尝试在谷歌上搜索解决方案,但我找不到任何我想做的事情。不确定我是否只是在寻找正确的东西。无论如何,我想做的是扩展一个返回的对象。现在它只有一个时可以正常工作,但是当有多个时它只会输出一个数组。如果有帮助,这适用于 Veeam Powershell 命令。

有问题的脚本

$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

{
    "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":  ""
}

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

[
    {
        "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
    }
]

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

4

1 回答 1

0

想通了我的问题。这是ConvertTo-Json. 默认情况下,它的最大深度仅为 2,因此它在到达Resources对象时立即停止。我将它增加到 3 的深度,它完全按照我的意愿工作。

现在我只需要运行它来得到我想要的。

Get-VBRCloudTenant | Select-Object * -ExcludeProperty SaltedPassword,PasswordSalt | ConvertTo-Json -Depth 3
于 2021-04-08T18:32:43.850 回答