简体   繁体   中英

How to combine items from one PowerShell Array and one Powershell Object and produce a 3rd PowerShell Object?

I have one PowerShell Array called $vmSizelist whose member is as follows

TypeName   : System.Management.Automation.PSCustomObject
Name       : Equals
MemberType : Method
Definition : bool Equals(System.Object obj)

It contains the following items as shown below.

Name : VM1
VMSize : Standard_D2s_v3
ResourceGroup : RG1

Name : VM2
VMSize : Standard_D14_v2
ResourceGroup : RG2

I have got another object called $AllVMSize which contains the below list

Name        NumberOfCores MemoryInMB MaxDataDiskCount OSDiskSizeInMB ResourceDiskSizeInMB

Standard_B1ls         1        512                2        1047552                 4096
Standard_B1ms         1       2048                2        1047552                 4096
Standard_B1s          1       1024                2        1047552                 4096
Standard_B2ms         2       8192                4        1047552                16384
Standard_B2s          2       4096                4        1047552                 8192
Standard_D2s_v3       2       8192                4        1047552                16384
Standard_D14_v2      16      114688              64        1047552               819200

The Get-Member shows below

Name                 MemberType Definition
----                 ---------- ----------
Equals               Method     bool Equals(System.Object obj)
GetHashCode          Method     int GetHashCode()
GetType              Method     type GetType()
ToString             Method     string ToString()
MaxDataDiskCount     Property   System.Nullable[int] MaxDataDiskCount {get;set;}
MemoryInMB           Property   int MemoryInMB {get;set;}
Name                 Property   string Name {get;set;}
NumberOfCores        Property   int NumberOfCores {get;set;}
OSDiskSizeInMB       Property   int OSDiskSizeInMB {get;set;}
RequestId            Property   string RequestId {get;set;}
ResourceDiskSizeInMB Property   int ResourceDiskSizeInMB {get;set;}
StatusCode           Property   System.Net.HttpStatusCode StatusCode {get;set;}

I wanted to merge the the member in an array and the above PSObject and wanted to produce a 3rd PSObject/Array-like below:

Name VMSize           ResourceGroup NumberOfCores MemoryInMB
VM1  Standard_D2s_v3  RG1           2             8192
VM2  Standard_D14_v2  RG2           16            114688

This is not too difficult. Just loop over the items in the $vmSizelist and find a matching item in the $AllVMSize list. If found, return a new object with properties combined.

$result = $vmSizelist | ForEach-Object {
    $vmSize = $_.VMSize
    $refVM = $AllVMSize | Where-Object { $_.Name -eq $vmSize }
    if ($refVM) {
        $_ | Select-Object *, @{Name = 'NumberOfCores'; Expression = {$refVM.NumberOfCores}}, 
                              @{Name = 'MemoryInMB'; Expression = {$refVM.MemoryInMB}}
    }  
}

# output in console
$result | Format-Table -AutoSize

# result to CSV
$result | Export-Csv -Path 'X:\TheCombinedProperties.csv' -NoTypeInformation

Output in console:

Name VMSize          ResourceGroup NumberOfCores MemoryInMB
---- ------          ------------- ------------- ----------
VM1  Standard_D2s_v3 RG1           2             8192      
VM2  Standard_D14_v2 RG2           16            114688

Why not create a hash table keyed on resource group name. Then you can reference it easily while adding the properties in a Select-Object command using the $_.VMSize property from the objects in the $VMSizeList array. It would look something like:

$ResouceGroupHash = 
$ResourceGroups |
Group-Object -Property Name -AsHashTable -AsString

$vmSizelist =
$vmSizelist |
Select-Object *,
    @{Name = 'NumberOfCores'; Expression = { $ResouceGroupHash[$_.VMSize].NumberOfCores}}, 
    @{Name = 'MemoryInMB'; Expression = { $ResouceGroupHash[$_.VMSize].MemoryInMB}}

I didn't test this but, it should work.

Another option; I don't know if you want to bother pulling modules and/or scripts for a small project but there are several versions of something like Join-Object running around on the internet. I didn't exhaustively read this , but I suspect it can do something like this for us. That's just 1 example.

Honestly I usually manually code something like above.

Hope this adds to the conversatiom, let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM