WSMANFault (out of memory due to GCI?)

I’m working on a reporting script right now to determine employee utilization of an enterprise application, Box Sync (like a commercial implementation of Dropbox). It’s crucial that I get this to run with Invoke-Command for parallelization because we have so many endpoints to test against. I don’t have logic in here yet to deal with systems not powered on, but in preliminary testing it’s working great against my local machine but failing with a wsmanfault when I try to run it against another local machine on the domain.


Invoke-Command -ComputerName $ComputerName -AsJob -JobName BoxCheck -ScriptBlock {

    $exepresent = Test-Path "$env:ProgramFiles\Box\Box Sync\BoxSync.exe"
    $exerunning = [bool] (Get-Process | Where-Object {$_.ProcessName -eq "BoxSync"})

    $explorerprocesses = @(Get-WmiObject -Query "Select * FROM Win32_Process WHERE Name='explorer.exe'" -ErrorAction SilentlyContinue)
    $usernames = @()
    if ($explorerprocesses.Count -eq 0){
        $logonstatus = "Nobody logged on" #currently not using this variable
    } else {
        $logonstatus = "User(s) logged on" #currently not using this variable
        foreach ($i in $explorerprocesses){
            $username = $i.GetOwner().User
            $usernames += $username             
     foreach ($user in $usernames) {
        $boxsyncdirectoryexists = Test-Path "$env:SystemDrive\Users\$user\Box Sync"
        $boxsyncdirectorystats = If ($boxsyncdirectoryexists) {
            Get-ChildItem -Path "$env:SystemDrive\Users\$user\Box Sync" -Recurse -Force -ErrorAction SilentlyContinue

        $boxsyncdirectorysize = [math]::Round( ( ( $boxsyncdirectorystats | Measure-Object -Property Length -Sum).Sum / 1MB ),2 )

        $properties = @{ComputerName = $env:COMPUTERNAME
            BoxSyncInstalled = $exepresent
            BoxSyncRunning = $exerunning
            User = $user
            BoxDatainMB = $boxsyncdirectorysize
            SyncDirExist = $boxsyncdirectoryexists}
        $obj = New-Object -TypeName PSObject -Property $properties
        Write-Output $obj

Get-Job 'BoxCheck' #| Wait-Job

The error:

Processing data for a remote command failed with the following error message:  For more information, see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OperationStopped: ($computername:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : JobFailure
    + PSComputerName        : $computername

I’ve already run

set-item WSMan:\localhost\Shell\MaxMemoryPerShellMB 2048

to increase the memory. It was already set at 1024 previously. If I had to guess, it is failing during the Get-ChildItem command. It shouldn’t be a very big file list anyway, even though it’s recursive. People don’t typically dump a lot of files into their synced folder to begin with.
Let’s say it is the GCI command though. I want to be able to handle a large file list. How do I ensure that I always have enough memory to run this? Set an absurdly high memory limit? I don’t need to return the results of the GCI command over the remoting session, it’s only included so that I can calculate the size of the combined files and folders in that directory. I could really use some tips. Thanks!

I should add that I have gotten this to successfully run against other remote computers.

I think I managed to fix it by simply reducing the number of properties I’m working with on the Get-ChildItem command, like so:

Get-ChildItem -Path "$env:SystemDrive\Users\$user\Box Sync" -Recurse -Force -ErrorAction SilentlyContinue | Where-Object {!$_.PSIsContainer} | Select Name,Fullname,Length

I’m not sure I understand why it works, though, because it’s not filtering for the properties, it’s retrieving them all and then discarding most of them. So I would expect it to crash just the same. I would really appreciate if anyone could explain to me why this works the way it does.