Not related to your question but maybe to speed up your array appending 
operation, take a look at 
https://blogs.technet.microsoft.com/ashleymcglone/2017/07/12/slow-code-top-5-ways-to-make-your-powershell-scripts-run-faster/

Section Appending to arrays

Thanks


Carl Webster
Citrix Technology Professional
http://www.CarlWebster.com
The Accidental Citrix Admin

-----Original Message-----
From: [email protected] [mailto:[email protected]] On 
Behalf Of Kurt Buff
Sent: Friday, July 21, 2017 1:04 PM
To: [email protected]
Subject: Re: [powershell] Largest 32 items in multiple subdirectories

I've restructured this a bit. It now looks like this, but I've run into a 
problem, and I know what it is, but not how to solve it.

The script:
   $CurrentVersions = Get-ChildItem K:\Engdrops -directory -filter 
CurrentVersions -recurse | select -expand fullname
   $CurrentVersionsOut = @()
   foreach ($CurrentVersion in $CurrentVersions)
      {
         $CurrentVersionLargest32 = [math]::Round((Get-ChildItem 
$CurrentVersion -recurse `
            | Sort-Object length -descending `
            | select-object -first 32 -expand length | measure-object –sum).sum 
/ 1mb)
         $CurrentVersionsOut += [pscustomobject]@{SourceContentPath = `
            $CurrentVersion; SourceConflictAndDeletedQuotaInMB =
$CurrentVersionLargest32 }
      }
   $CurrentVersionsOut | export-csv -notype c:\temp\CacheSizes.csv

It works great, unless there are fewer than 32 items in the directory, at which 
point the script emits the following error.

   select-object : Property "length" cannot be found.
   At line:3 char:123
   + ...  -descending | select-object -first 32 -expand length | measure-object 
–sum).sum ...
   +                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       + CategoryInfo          : InvalidArgument:
(601-1273V4.01c:PSObject) [Select-Object], PSArgumentException
       + FullyQualifiedErrorId :
ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand


So, it looks like I need to do some error trapping, and I'd like to set a 
4096mb default for small or empty directories.

Thoughts?

Kurt

On Thu, Jul 20, 2017 at 8:47 PM, Devin Rich <[email protected]> wrote:
> Ooops, didn't see you were using -Property from measure-object.
>
> Why does spaces in your input object cause it to fail... that's a weird one.
> ...
>
> Thanks,
> Devin Rich
> Systems Administrator
>
> On Thu, Jul 20, 2017 at 9:44 PM, Devin Rich 
> <[email protected]>
> wrote:
>>
>> Ok, couple other things :)
>>
>> ...  select-object -first 32 | measure-object -property length –sum ..
>> Should be:
>> ...  select-object -first 32 -expand Length | measure-object 
>> -property length –sum ...
>>
>>
>> Next, a lesson on objects. If you have a FileInfo object (like what 
>> you get from Get-ChildItem), you can access its properties (such as 
>> length, fullname, psparentpath, others all shown via Get-Member, 
>> etc.). If you run your Get-ChildItem | Select -Expand FullName, you no 
>> longer have an object.
>> You have an array of strings. They no longer have properties to be accessed.
>> You can only use $CurrentVersion which is the full path from earlier. 
>> I'd say keep the Select -Expand FullName from your first line and 
>> then get rid of the .FullName from accessing the output.
>>
>>
>>
>> Thanks,
>> Devin Rich
>> Systems Administrator
>>
>> On Thu, Jul 20, 2017 at 9:30 PM, Devin Rich 
>> <[email protected]>
>> wrote:
>>>
>>> The $CurrentVersion before your () is wrong. And you don't need the ().
>>> Try this for line 1 insde of the Foreach:
>>> $CurrentVersionLargest32 = Get-ChildItem $CurrentVersion -recurse | 
>>> Sort-Object length -descending | select-object -first 32 | 
>>> measure-object -property length –sum
>>>
>>> Of course, you can also do: [int]$CurrentVersionLargest32 = 
>>> (Get-ChildItem $CurrentVersion -recurse | Sort-Object length 
>>> -descending | select-object -first 32 | measure-object -property 
>>> length –sum).sum / 1GB
>>>
>>> This forces it to round to nearest GB. :)
>>>
>>> Good luck!
>>>
>>> Thanks,
>>> Devin Rich
>>> Systems Administrator
>>>
>>> On Thu, Jul 20, 2017 at 9:04 PM, Kurt Buff <[email protected]> wrote:
>>>>
>>>> I have a large directory, with multiple (80+) subdirectories, each 
>>>> of which has its own subdirectory for which I'm trying to find the 
>>>> largest 32 items, and sum them. I'm using this article:
>>>>
>>>> https://blogs.technet.microsoft.com/askds/2011/07/13/how-to-determi
>>>> ne-the-minimum-staging-area-dfsr-needs-for-a-replicated-folder/
>>>> to determine the optimum cache size for DFRS for the 
>>>> sub-sub-directories that will be replicated.
>>>>
>>>> This works just fine for a single directory:
>>>>    $CurrentVersion = "K:\Engineering\x1\y1\CurrentVersions"
>>>>    $CurrentVersionLargest32 = Get-ChildItem $CurrentVersion 
>>>> -recurse | Sort-Object length -descending | select-object -first 32 
>>>> | measure-object -property length –sum
>>>>
>>>> This fails:
>>>>
>>>>    $CurrentVersions = Get-ChildItem K:\Engineering -directory 
>>>> -filter CurrentVersions -recurse | select -expand fullname
>>>>    $CurrentVersionsOut = @()
>>>>    foreach ($CurrentVersion in $CurrentVersions)
>>>>       {
>>>>          $CurrentVersionLargest32 = $CurrentVersion.(Get-ChildItem 
>>>> $CurrentVersion -recurse | Sort-Object length -descending | 
>>>> select-object -first 32 | measure-object -property length –sum)
>>>>          $CurrentVersionCacheSize = 
>>>> [math]::Round($CurrentVersionLargest32.sum /1gb)
>>>>          $CurrentVersionsOut += [pscustomobject]@{SourceContentPath 
>>>> = $CurrentVersion; SourceConflictAndDeletedQuotaInMB = 
>>>> $CurrentVersionCacheSize }
>>>>       }
>>>>    $CurrentVersionsOut | export-csv -notype c:\temp\CacheSizes.csv
>>>>
>>>> Output looks like this:
>>>>    "SourceContentPath","SourceConflictAndDeletedQuotaInMB"
>>>>    "K:\Engineering\x1\y1\CurrentVersions","0"
>>>>    "K:\Engineering\x2\y2\CurrentVersions","0"
>>>>
>>>> So, to me, after much testing, it looks like I've got something 
>>>> wrong with the first line inside the foreach loop, but I've banged 
>>>> my head against that for several hours, and just can't seem to figure it 
>>>> out.
>>>>
>>>> Pointer in the correct direction would be much appreciated.
>>>>
>>>> Kurt
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>> The information contained in this message is privileged, 
>>> confidential, and protected from disclosure. If you are not the 
>>> intended recipient, you are hereby notified that any review, 
>>> printing, dissemination, distribution, copying or other use of this 
>>> communication is strictly prohibited. If you have received this 
>>> communication in error, please notify us immediately by replying to the 
>>> message and deleting it from your computer.
>>
>>
>
>
> The information contained in this message is privileged, confidential, 
> and protected from disclosure. If you are not the intended recipient, 
> you are hereby notified that any review, printing, dissemination, 
> distribution, copying or other use of this communication is strictly 
> prohibited. If you have received this communication in error, please 
> notify us immediately by replying to the message and deleting it from your 
> computer.





Reply via email to