Powershell remote invoke-command vs local from server

Goce Dimitroski 41 Reputation points
2021-05-19T21:56:17.553+00:00

Hello,
Why is it when I run this from my pc on a remote server i get one result (user profile sizes)

 Invoke-Command -ComputerName $Server  -ScriptBlock {



        Write-Host " User Profiles in MB" -ForegroundColor black -BackgroundColor Yellow
        $sum = 0
        gci -force 'C:\Users'-ErrorAction SilentlyContinue | ? { $_ -is [io.directoryinfo] } | % {
            $len = 0
            gci -recurse -force $_.fullname -ErrorAction SilentlyContinue | % { $len += $_.length }
            $_.fullname, '{0:N2} GB' -f ($len / 1Gb)
            $sum = $sum + $len
        }
        "Total size of profiles",'{0:N2} GB' -f ($sum / 1Gb)



}

When I run it on the server itself

Write-Host " User Profiles in MB" -ForegroundColor black -BackgroundColor Yellow
$sum = 0
gci -force 'C:\Users'-ErrorAction SilentlyContinue | ? { $_ -is [io.directoryinfo] } | % {
$len = 0
gci -recurse -force $.fullname -ErrorAction SilentlyContinue | % { $len += $.length }
$_.fullname, '{0:N2} GB' -f ($len / 1Gb)
$sum = $sum + $len
}
"Total size of profiles",'{0:N2} GB' -f ($sum / 1Gb)

I get the correct result. Why is that ?

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,110 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,360 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,571 Reputation points Microsoft Vendor
    2021-05-20T07:25:53.483+00:00

    Hi,

    Does the user have permission to access the directories? You can specify the user account using the "-credential" parameter.

    Invoke-Command -ComputerName $server -Credential '' -ScriptBlock { ... }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.