PowerShell 根据返回结果展开对象或数组
PowerShell expand object or array on returned results
所以我不是很精通 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": ""
}
所以这是按预期工作的,所以我在 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
}
]
如果有人能为我指出正确的方向,那就太好了。
解决了我的问题。这是 ConvertTo-Json
。默认情况下它的最大深度仅为 2,因此它在到达 Resources
对象时立即停止。我将它增加到 3 的深度,它完全按照我的要求工作。
现在我只需要运行这个来得到我想要的。
Get-VBRCloudTenant | Select-Object * -ExcludeProperty SaltedPassword,PasswordSalt | ConvertTo-Json -Depth 3
所以我不是很精通 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": ""
}
所以这是按预期工作的,所以我在 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
}
]
如果有人能为我指出正确的方向,那就太好了。
解决了我的问题。这是 ConvertTo-Json
。默认情况下它的最大深度仅为 2,因此它在到达 Resources
对象时立即停止。我将它增加到 3 的深度,它完全按照我的要求工作。
现在我只需要运行这个来得到我想要的。
Get-VBRCloudTenant | Select-Object * -ExcludeProperty SaltedPassword,PasswordSalt | ConvertTo-Json -Depth 3