question

GoceDimitroski-4791 avatar image
0 Votes"
GoceDimitroski-4791 asked GoceDimitroski-4791 commented

Powershell remote invoke-command vs local from server

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-serverwindows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered GoceDimitroski-4791 commented

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.



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

No my user ac doesn't have full access to the directories. I should of checked that first . SO i will try it with the cred entrie

0 Votes 0 ·