Share via

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 for business | Windows Server | User experience | PowerShell
Windows for business | Windows Server | User experience | Other
0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    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.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.