Powershell 使用远程连接返回不同的信息 [英] Powershell returns different information using remote connection
问题描述
在我使用 PowerShell 的目标计算机上运行命令$FolderSize =(Get-ChildItem "C:\Users\JDoe" -force -Recurse -ErrorAction SilentlyContinue | Measure-Object length -sum).sum
On my target computer in PowerShell I run command
$FolderSize =(Get-ChildItem "C:\Users\JDoe" -force -Recurse -ErrorAction SilentlyContinue | Measure-Object length -sum).sum
我得到的值为 0.76 gb,它准确对应于磁盘上文件夹的压缩大小.但是,当我尝试使用
and I get a value of 0.76 gb, which accurately corresponds to the compressed size of the folder on disk. However, when I try to run the command on a remote computer using
$folderSize = Invoke-Command -ComputerName "computername";{(Get-ChildItem -Path "C:\Users\JDoe" -Recurse -force -ErrorAction SilentlyContinue | Measure-Object -Property Length -sum).sum}
我得到了一个不同的、更大的数字,17 GB.
$folderSize = Invoke-Command -ComputerName "computername" {(Get-ChildItem -Path "C:\Users\JDoe" -Recurse -force -ErrorAction SilentlyContinue | Measure-Object -Property Length -sum).sum}
I get a different, MUCH larger number, 17 gb.
我尝试在 pssession 中运行第一个命令,但仍然得到 17gb 的结果.我也试过使用
I tried running the first command in a pssession but still get the 17gb result. I also tried using
psexec \\\computername powershell "(Get-ChildItem "C:\Users\JDoe" -force -Recurse -ErrorAction SilentlyContinue | Measure-Object length -sum).sum"
但是仍然得到更大的数字.
psexec \\\computername powershell "(Get-ChildItem "C:\Users\JDoe" -force -Recurse -ErrorAction SilentlyContinue | Measure-Object length -sum).sum"
but still get the larger number.
我不明白为什么我在本地检查时远程获得的结果与文件夹的实际大小不同.至少所有远程结果都是一致的,这告诉我它们都在测量相同的东西.
I don't understand why the results obtained remotely are different than the actual size of the folder when I examine it locally. At least all the remote results are consistent, which tells me they are all measuring the same thing.
推荐答案
这是由于 AppData\Local
中名为 Application Data
的结点指向 >AppData\Local
This is due to a junction in AppData\Local
named Application Data
that points back to AppData\Local
您似乎可以远程访问此连接点(即使从使用 \\COMPUTER01\C$\Users\JDoe\AppData\Local\Application Data
的资源管理器),这就是 为什么 你得到不同的大小,因为它递归地计算相同的东西直到 MAX_PATH 限制.
It appears that you can access this junction remotely (even from explorer using \\COMPUTER01\C$\Users\JDoe\AppData\Local\Application Data
) so this is why you're getting different sizes, as it's recursively counting the same stuff up to the MAX_PATH limit.
在远程和本地比较以下命令的输出:Get-ChildItem 'C:\Users\JDoe\AppData\Local\Application Data' -Force
Compare the following command's outputs on remote vs local:
Get-ChildItem 'C:\Users\JDoe\AppData\Local\Application Data' -Force
本地
PS C:\> Get-ChildItem 'C:\Users\JDoe\AppData\Local\Application Data' -Force
Get-ChildItem : Access to the path 'C:\Users\JDoe\AppData\Local\Application Data' is denied.
At line:1 char:1
+ Get-ChildItem 'C:\Users\JDoe\AppData\Local\Application Data' -Forc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\users\JDoe\A...plication Data\:String) [Get-ChildItem], UnauthorizedAccessException
+ FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
远程
PS C:\> Invoke-Command -ComputerName COMPUTER01 -ScriptBlock { Get-ChildItem 'C:\Users\JDoe\AppData\Local\Application Data' -Force }
Directory: C:\Users\JDoe\AppData\Local\Application Data
Mode LastWriteTime Length Name PSComputerName
---- ------------- ------ ---- --------------
d--hsl 4/16/2020 4:46 PM Application Data COMPUTER01
d----- 10/31/2019 9:43 AM ConnectedDevicesPlatform COMPUTER01
d----- 10/31/2019 9:52 AM ElevatedDiagnostics COMPUTER01
d----- 10/31/2019 9:43 AM Google COMPUTER01
d--hsl 4/16/2020 4:46 PM History COMPUTER01
d----- 4/16/2020 4:50 PM Microsoft COMPUTER01
d----- 9/16/2019 8:14 PM Microsoft Help COMPUTER01
d----- 10/31/2019 9:43 AM MicrosoftEdge COMPUTER01
d----- 10/31/2019 9:53 AM OpenShell COMPUTER01
d----- 4/16/2020 4:47 PM Packages COMPUTER01
d----- 10/31/2019 9:43 AM PlaceholderTileLogoFolder COMPUTER01
d----- 10/31/2019 9:43 AM Publishers COMPUTER01
d----- 3/18/2019 11:52 PM Temp COMPUTER01
d--hsl 4/16/2020 4:46 PM Temporary Internet Files COMPUTER01
d----- 10/31/2019 9:43 AM VirtualStore COMPUTER01
您需要使用this中的递归函数,与Get-ChildItem
分开递归回答.
You will need to recurse separately from Get-ChildItem
by using a recursive function like the one in this answer.
这篇关于Powershell 使用远程连接返回不同的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!