none
Need help creating custom objects and adding them to an array. RRS feed

  • Question

  • Hi Everyone,

    So I'm dinking around in Powershell today and I'm failing at creating an array of custom objects.  I want to create a HTML report that builds out after every MDT image update. To do that, I need to collect my data on what's contained in each build. Here's my current script in progress -

    function Parse-Dependents($fullname){
        
        #PARAMETER SWITCHING
        if(!($fullname)){
        $dependents = get-dependents
        }
        if($fullname){
        $dependents = get-dependents -fullname $fullname
        }
        
        #SPIN THROUGH ARRAY OF GUIDS FROM GET-DEPENDENTS
        foreach($d in $dependents){
            
            #SPIN THROUGH EACH APP IN APPLICATIONS.XML
            foreach ($app in $apps){ 
                    
                    #IF MATCH THEN ADD OBJECT WITH PROPERTIES
                    if($d -match $app.guid){ 
                    #ADD APPLICATION MATCH TO THE ARRAY
                    $applications = @{
                    'Name' = $app.ShortName;
                    'GUID' = $app.Guid;
                    'Version' = $app.Version;
                    'Last Modified Time' = $app.LastModifiedTime;
                    'Last Modified By' = $app.LastModifiedBy;
                    'Install Directory' = [string]'\\my\path\to\MDT\' + $app.WorkingDirectory.TrimStart(".","\")
                    'CommandLine' = $app.CommandLine;
                    }
                    new-object -typename PSObject -property $applications
                    }
                    
                    }
                
                }
            
        #RETURN MATCHED ARRAY
        return $applications
            
    }

    It all works great until I look at my output and see that I get my expected properties and array, but at the end it's created additional empty entries for each of my initial properties I assigned.  So I get my list of apps like this :

    powershell.ex... OrgChart Plugin  \\my\mdt\server\ XX\user                     9/22/2014 5:... {ffee7497-0c...

    And then below it :

                     CommandLine                                                                                    
                     Name                                                                                           
                     Install Direc...                                                                               
                     Last Modified By                                                                               
                     Version                                                                                        
                     Last Modified...                                                                               
                     GUID                                                                                           

    And these are all listed under the Name property.  I'm still pretty new to PS (8 months or so now), and rarely have to create a custom object, but this seems like a case for doing so to create a custom html report.  Any ideas would be greatly appreciated.

    Ryan

    Tuesday, November 11, 2014 3:31 PM

Answers

  • Here you're (correctly) outputting your finished object:

    new-object -typename PSObject -property $applications

    but then then after that you're also outputting the hash table that was used to create the object:

     return $applications

    which is totally unnecessary, and is producing the extraneous output.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    • Edited by mjolinorModerator Tuesday, November 11, 2014 7:02 PM
    • Marked as answer by MrBrooks Tuesday, November 11, 2014 7:33 PM
    Tuesday, November 11, 2014 7:01 PM
    Moderator

All replies

  • Here you're (correctly) outputting your finished object:

    new-object -typename PSObject -property $applications

    but then then after that you're also outputting the hash table that was used to create the object:

     return $applications

    which is totally unnecessary, and is producing the extraneous output.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    • Edited by mjolinorModerator Tuesday, November 11, 2014 7:02 PM
    • Marked as answer by MrBrooks Tuesday, November 11, 2014 7:33 PM
    Tuesday, November 11, 2014 7:01 PM
    Moderator
  • Thanks.  You're right I am returning twice.  I'm still getting the hang of sending content in and out of functions and I'm just kind of used to using return to tell a function to send that data back. That must be where the extraneous items were coming from, and even more frustrating result of seeing the header properties of Name and Value, and not the expected Name, Install, Last Modified, and etc properties. I appreciate the help.
    Tuesday, November 11, 2014 7:33 PM
  • Whats really strange is that $applications had the correct data but return $applications did not.  I'm guessing there are some extra actions going on with return? Anyways thanks again for the help.
    Tuesday, November 11, 2014 7:56 PM
  • It's not really all that strange.  

    If you look at your script, you're not outputting the hash table until both loops finish.  The values are dependent on the properties of the $app objects enumerated in the inner loop, and only outputting an object if the dependent ($d) matches the $app guid property.

    At the end, you're outputting the hash table without that test, so it's going to have values based on the last $app in the loop, regardless of whether the guid matches the dependents or not.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Tuesday, November 11, 2014 8:08 PM
    Moderator
  • Ahhhh that's where the data is coming from.  I was going back and forth counting each array and scratching my head.  Final result is pretty awesome, it doesn't do much yet but should be able to spit out some really nice HTML MDT image reports (we have a lot of configurations) :) 
    Tuesday, November 11, 2014 8:21 PM