Custom Object Property

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Paul Goffar Paul Goffar 2 years ago.

  • Author
    Posts
  • #20501
    Profile photo of Paul Goffar
    Paul Goffar
    Participant

    Hello all,

    I'm working on a small function and I'm getting hung-up on some of the output. What I have right now works perfectly for generating results in a nice clean report, my size conversion is all working, etc... but I need to add another column listing computer name as this script will target many machines. I'm thinking this is just a custom object property but since i would calling $ENV:Computername into that property.....let me stop there. Hopefully someone knows what I'm trying to say 🙂

    
    function Get-MyDocSize
    {
    	[CmdletBinding()]
    	param (
    		[Parameter(Position = 0, Mandatory = $false)]
    		[System.String]
    		$path = "C:\Users\*\Documents"
    	)
    	{
    		Get-ChildItem $path |
    		Where-Object { $_.PSisContainer } |
    		ForEach-Object { Write-Progress 'Examining Folder' ($_.FullName); $_ } |
    		ForEach-Object {
    			$result = '' |
    			Select-Object Path, Count, Size;
    			$result.path = $_.FullName;
    			$temp = Dir $_.FullName -recurse -ea SilentlyContinue |
    			Measure-Object length -sum -ea SilentlyContinue;
    			$result.count = $temp.Count; $result.Size = "{0:N2}" -f ($temp.sum / 1MB) + " MB"; $result
    		} |
    		Sort-Object Size -descending |
    		Export-Csv -Path "\\servername\folder\" + "$env:computername" + ".csv" -NoTypeInformation
    		
    		
    	}
    }	

    My current results look like this

    Path	                                                    Count	                 Size
    C:\Users\Public\Documents	                 3	                 9,228.00 MB
    C:\Users\user3\Documents	              2627	                 7,755.00 MB
    C:\Users\user1\Documents	                89	                 1,065.46 MB
    C:\Users\user2\Documents	                 1	                 0.10 MB
    

    My ideal results would look like this...

    
    ComputerName          Path	                                      Count	                 Size
    Computer1                 C:\Users\Public\Documents	      3	                 9,228.00 MB
    Computer1                 C:\Users\user3\Documents	      2627	                 7,755.00 MB
    Computer1                 C:\Users\user1\Documents	      89	                 1,065.46 MB
    Computer1                 C:\Users\user2\Documents	      1	                 0.10 MB
    
    
  • #20502
    Profile photo of Don Jones
    Don Jones
    Keymaster

    $result | Add-Member -MemberType NoteProperty -Name ComputerName -Value 'whatever'

    Although I would tend to not create the custom object as you've done, but instead:

    $properties = @{'ComputerName'='whatever';'Size'=$Size.Calculation;'Temp'='whatever'}
    $obj = New-Object PSObject -Prop $properties
    

    Little more programmatic. But, whatever works for you.

  • #20503
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Using the "pipe something to Select-Object" approach of building a custom object, you'd just add a new property name to your Select-Object call (probably ComputerName), and then set it to $env:computername. Something like this:

    function Get-MyDocSize
    {
        [CmdletBinding()]
        param (
            [Parameter(Position = 0, Mandatory = $false)]
            [System.String]
            $path = "C:\Users\*\Documents"
        )
    
        Get-ChildItem $path |
        Where-Object { $_.PSisContainer } |
        ForEach-Object { Write-Progress 'Examining Folder' ($_.FullName); $_ } |
        ForEach-Object {
            $result = "" |
            Select-Object Path, Count, Size, ComputerName;
            $result.path = $_.FullName;
            $temp = Dir $_.FullName -recurse -ea SilentlyContinue |
            Measure-Object length -sum -ea SilentlyContinue;
            $result.count = $temp.Count;
            $result.Size = "{0:N2}" -f ($temp.sum / 1MB) + " MB";
            $result.ComputerName = $env:computername
            $result
        } |
        Sort-Object Size -descending |
        Export-Csv -Path "\\servername\folder\" + "$env:computername" + ".csv" -NoTypeInformation
    }
    

    One thing to keep in mind is that your "Size" field is now a string, not a number. Sort-Object might not work quite the way you expect in this situation. As far as strings are concerned, "2" is greater than "1000".

  • #20505
    Profile photo of Paul Goffar
    Paul Goffar
    Participant

    You guys are amazing! I try not to flood the forum too much but everytime I get stuck, you're always right there, THANKS!

    Don, I follow you pretty closely and take all of your advice to heart as I'm still feeling out the "best" way to do things.

    Dave, you're always right on the money!

    Thanks again

You must be logged in to reply to this topic.