locked
Unable to merge 1 to many relating data into a PSObject RRS feed

  • Question

  • Hi,

    $mp is ONE

    $mpref is MANY (Contains lots of rows for $mp)

    $mp = Get-SCManagementPack -Name Microsoft.Windows.Server.2012.Discovery 
    
    $system = New-Object -TypeName psobject
    
    
    
    $mpref = $mp.References
    
    if($mpref)
    {
        foreach($mpdep in $mpref)
        {
            Add-Member -InputObject $system -MemberType NoteProperty -Name DisplayName -Value $mp.DisplayName
            Add-Member -InputObject $system -MemberType NoteProperty -Name Description -Value $mp.Description
            Add-Member -InputObject $system -MemberType NoteProperty -Name Name -Value $mp.Name
            Add-Member -InputObject $system -MemberType NoteProperty -Name Version -Value $mp.Version
            Add-Member -InputObject $system -MemberType NoteProperty -Name DependencyName -Value $mpdep.Values.Name
            Add-Member -InputObject $system -MemberType NoteProperty -Name DependencyVersion -Value $mpdep.Values.Version
        }
    }$system


    Error:

     

    Add-Member : Cannot add a member with the name "DependencyVersion" because a member with that name already exists. To overwrite the member anyway, add the Force parameter to your 
    command.
    At line:25 char:9
    +         Add-Member -InputObject $system -MemberType NoteProperty -Nam ...
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (@{DisplayName=W...ndencyVersion=}:PSObject) [Add-Member], InvalidOperationException
        + FullyQualifiedErrorId : MemberAlreadyExists,Microsoft.PowerShell.Commands.AddMemberCommand


    Output:

    DisplayName       : Windows Server 2012 Operating System (Discovery)
    Description       : Microsoft Windows Server 2012 Operating System Management Pack: This management pack discovers Windows Server 2012.
    Name              : Microsoft.Windows.Server.2012.Discovery
    Version           : 6.0.7323.0
    DependencyName    : 
    DependencyVersion : 

    Thanks,

    Rajiv

    Sunday, March 10, 2019 12:18 PM

All replies

  • For starters...

    Review this link:

    https://www.itprotoday.com/powershell/powershell-basics-custom-objects

    You are creating *one* object, then using that single object in your loop.  That's not correct.

    You need to create an object *each time* in your loop.



    • Edited by Marco.Shaw Sunday, March 10, 2019 1:04 PM
    Sunday, March 10, 2019 1:04 PM
  • Actually this has nothing to do with data relations.  The issue is because you are adding everything to a single object.

    $mp = Get-SCManagementPack -Name Microsoft.Windows.Server.2012.Discovery
    $system = foreach ($mpdep in $mp.References) {
        [pscustomobject]@{
            DisplayName = $mp.DisplayName
            Description = $mp.Description
            Name = $mp.Name
            Version = $mp.Version
            DependencyName = $mpdep.Values.Name
            DependencyVersion = $mpdep.Values.Version
        }
    }
    
    $system


    \_(ツ)_/

    Sunday, March 10, 2019 1:18 PM
  • Thanks, but its not like I wanted. Below is the output:

    DisplayName       : Windows Server 2012 Operating System (Discovery)
    Description       : Microsoft Windows Server 2012 Operating System Management Pack: This management pack discovers Windows Server 2012.
    Name              : Microsoft.Windows.Server.2012.Discovery
    Version           : 6.0.7323.0
    DependencyName    : 
    DependencyVersion : 

    Output of $mp.References

    Key           Value                                                                                                                    
    ---           -----                                                                                                                    
    SCDW          ManagementPack:[Name=Microsoft.SystemCenter.DataWarehouse.Library, KeyToken=31bf3856ad364e35, Version=7.3.13142.0]       
    Windows       ManagementPack:[Name=Microsoft.Windows.Library, KeyToken=31bf3856ad364e35, Version=7.5.8501.0]                           
    SystemPerf    ManagementPack:[Name=System.Performance.Library, KeyToken=31bf3856ad364e35, Version=7.0.8438.6]                          
    WindowsServer ManagementPack:[Name=Microsoft.Windows.Server.Library, KeyToken=31bf3856ad364e35, Version=10.0.21.0]                     
    ReportLibrary ManagementPack:[Name=Microsoft.SystemCenter.DataWarehouse.Report.Library, KeyToken=31bf3856ad364e35, Version=7.3.13142.0]
    System        ManagementPack:[Name=System.Library, KeyToken=31bf3856ad364e35, Version=7.5.8501.0]                                      
    SC            ManagementPack:[Name=Microsoft.SystemCenter.Library, KeyToken=31bf3856ad364e35, Version=7.0.8438.6]                      
    SystemHealth  ManagementPack:[Name=System.Health.Library, KeyToken=31bf3856ad364e35, Version=7.0.8438.6] 

    Is it possible to get the output in a single tabular format?

    Rajiv


    • Edited by Rajiv IR Sunday, March 10, 2019 3:50 PM
    Sunday, March 10, 2019 3:49 PM
  • Normalization is usually a 'good thing', but if he wants to preserve that one-to-many relationship he might try this:

    $mp = Get-SCManagementPack -Name Microsoft.Windows.Server.2012.Discovery
    $hash=@{}
    $mp.References | foreach {
        $hash.$($_.Values.Name) = $_.Values.Version
    }
    [pscustomobject]@{
        DisplayName = $mp.DisplayName
        Description = $mp.Description
        Name = $mp.Name
        Version = $mp.Version
        Dependencies = $hash
    }


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Sunday, March 10, 2019 3:51 PM
  • Yes that is another approach but it may be a bit of a challenge for anyone without PowerShell skills.


    \_(ツ)_/

    Sunday, March 10, 2019 3:57 PM
  • Thanks, but its not like I wanted. Below is the output:

    DisplayName       : Windows Server 2012 Operating System (Discovery)
    Description       : Microsoft Windows Server 2012 Operating System Management Pack: This management pack discovers Windows Server 2012.
    Name              : Microsoft.Windows.Server.2012.Discovery
    Version           : 6.0.7323.0
    DependencyName    : 
    DependencyVersion : 

    Output of $mp.References

    Key           Value                                                                                                                    
    ---           -----                                                                                                                    
    SCDW          ManagementPack:[Name=Microsoft.SystemCenter.DataWarehouse.Library, KeyToken=31bf3856ad364e35, Version=7.3.13142.0]       
    Windows       ManagementPack:[Name=Microsoft.Windows.Library, KeyToken=31bf3856ad364e35, Version=7.5.8501.0]                           
    SystemPerf    ManagementPack:[Name=System.Performance.Library, KeyToken=31bf3856ad364e35, Version=7.0.8438.6]                          
    WindowsServer ManagementPack:[Name=Microsoft.Windows.Server.Library, KeyToken=31bf3856ad364e35, Version=10.0.21.0]                     
    ReportLibrary ManagementPack:[Name=Microsoft.SystemCenter.DataWarehouse.Report.Library, KeyToken=31bf3856ad364e35, Version=7.3.13142.0]
    System        ManagementPack:[Name=System.Library, KeyToken=31bf3856ad364e35, Version=7.5.8501.0]                                      
    SC            ManagementPack:[Name=Microsoft.SystemCenter.Library, KeyToken=31bf3856ad364e35, Version=7.0.8438.6]                      
    SystemHealth  ManagementPack:[Name=System.Health.Library, KeyToken=31bf3856ad364e35, Version=7.0.8438.6] 

    Is it possible to get the output in a single tabular format?

    Rajiv


    You have to specifiy the coorect paths to the properties that you want.  We can't help without having access to your system or one like yours.

    The SC forum would be a better place to get information but learning PowerShell would be the best next step.


    \_(ツ)_/

    Sunday, March 10, 2019 3:59 PM
  • Yep, I should have been more specific in respect to paths. I will try to figure it out.

    Thanks

    Rajiv


    • Edited by Rajiv IR Sunday, March 10, 2019 4:19 PM
    Sunday, March 10, 2019 4:14 PM
  • I should have been more specific in respect to paths. I thought forums was one of the best learning grounds as I am a NewBee. I will try by myself... 

    Thanks

    Rajiv

    Technical forums are only useful if you know the underlying technology.  Without the basics you will not be able to ask a question that is understandable.

    <<<<<<<<<<<<<<<<<--------------->>>>>>>>>>>>>>>>>>>>>>>

    Answers in technical forums are not solutions.  We can only show you methods and point to approaches.  Solutions are your job.

    Here is a post I did for another user who realized the need for learning the basics.

    In  deference to the likely frustration of the OP I can add the following links.  They will give a simple context in a few minutes that can help someone new to programming with PowerShell.  They do not teach the basics but do create the context that will make learning the basics easier and, I think, more fun.

    In an interview published 2017 September 13, Jeffrey Snoverexplained the motivation for the project:<sup id="cite_ref-11">[11]</sup>

    I'd been driving a bunch of managing changes, and then I originally took the UNIX tools and made them available on Windows, and then it just didn't work. Right? Because there's a core architectural difference between Windows and Linux. On Linux, everything's an ASCII text file, so anything that can manipulate that is a managing tool.  AWK, grep, sed? Happy days!

    I brought those tools available on Windows, and then they didn't help manage Windows because in Windows, everything's an API that returns structured data. So, that didn't help. [...] I came up with this idea of PowerShell, and I said, "Hey, we can do this better."

    From: https://en.wikipedia.org/wiki/PowerShell

    Also: https://docs.microsoft.com/en-us/powershell/scripting/overview?view=powershell-6

    The above will explain what PowerShell is and, more important , why.

    PowerShell is not like any other scripting systems and has some very new and highly sophisticated features.  These features make PowerShell very easy to use once you understand that they exist and how they are implemented.

    Good luck and happy PosH'ing.

    There is one very good video tutorial by the PowerShell Team.  It is scheduled to be removed so get it now.  It can be downloaded and viewed offline but the online version is better presented and organized.

    Getting Started with Microsoft PowerShell


    \_(ツ)_/

    Sunday, March 10, 2019 4:20 PM