你先生被搞砸了。您并不孤单,大多数使用 Get-ChildItem 和 FileSystem Provider 组合工作的人都会遇到这种情况。结果是不一致的,并且经常混淆它们为什么会改变。例如对于您的情况,添加 -Exclude 参数会将命令的输出更改为不仅引用文件,而且引用它们的完整路径。这至少可以说是令人沮丧的。我唯一的建议是:A) 在对 FileSystem 提供程序运行 Get-ChildItem 时,尽可能使用 -Filter 参数,并且 B) 在通过 Where 语句运行这些结果提供结果后执行所有其他过滤。使用 -Include 和 -Exclude 从 FileSystem Provider 中提取所有结果,然后过滤输出,因此 PowerShell 正在做同样的工作,但使用 Where 语句既更灵活,也更可靠。试试这个:
$fso = Get-ChildItem -Recurse -path C:\test\A
$fsoBU = Get-ChildItem -Recurse -path C:\test\B | Where{$_.FullName -notlike "C:\test\B\.git*"}
Compare-Object -ReferenceObject $fso -DifferenceObject $fsoBU
我敢打赌,这种改变会产生你想要的结果。
更多详细信息:以上回答了您的问题,但这是我为找到这个问题所做的...
已创建 C:\Temp\Test。将 File1.csv、File2.csv 和 File3.csv 放入其中。复制文件夹,并粘贴它,创建包含所有三个文件的“C:\Temp\Test - Copy”。然后我删除了 C:\Temp\Test - Copy\File2.csv。我运行了这些命令:
$dir1 = gci c:\temp\test -recurse
$dir2 = gci 'c:\temp\test - copy' -recurse
diff $dir1 -dif $dir2
我得到了预期的结果,说$dir1 有 File2.csv 而$dir2 没有。好的,现在过滤掉一些东西......
$dir1 = gci c:\temp\test -recurse -exclude "file1.csv"
diff $dir1 -dif $dir2
什么什么?这是它告诉我的:
InputObject SideIndicator
----------- -------------
File1.csv =>
File3.csv =>
C:\temp\test\File2.csv <=
C:\temp\test\File3.csv <=
现在为什么只列出$dir2 的文件名和$dir1 的完整路径?为什么会发生变化?显然它改变了,因为我添加了-Exclude 参数,但是对象实际上不同吗?让我们看看:
PS:\Temp> $dir1[0].psobject
BaseObject : C:\temp\test\File2.csv
Members : {System.String PSPath=Microsoft.PowerShell.Core\FileSystem::C:\temp\test\File2.csv, System.String PSParentPath=Microsoft.PowerShell.Core\FileSystem::C:\temp\test, System.String
PSChildName=File2.csv, System.Management.Automation.PSDriveInfo PSDrive=C...}
Properties : {System.String PSPath=Microsoft.PowerShell.Core\FileSystem::C:\temp\test\File2.csv, System.String PSParentPath=Microsoft.PowerShell.Core\FileSystem::C:\temp\test, System.String
PSChildName=File2.csv, System.Management.Automation.PSDriveInfo PSDrive=C...}
Methods : {string get_Name(), long get_Length(), string get_DirectoryName(), System.IO.DirectoryInfo get_Directory()...}
ImmediateBaseObject : C:\temp\test\File2.csv
TypeNames : {System.IO.FileInfo, System.IO.FileSystemInfo, System.MarshalByRefObject, System.Object}
PS C:\Temp> $dir2[0].psobject
BaseObject : File1.csv
Members : {System.String PSPath=Microsoft.PowerShell.Core\FileSystem::C:\temp\Test - Copy\File1.csv, System.String PSParentPath=Microsoft.PowerShell.Core\FileSystem::C:\temp\Test - Copy, System.String
PSChildName=File1.csv, System.Management.Automation.PSDriveInfo PSDrive=C...}
Properties : {System.String PSPath=Microsoft.PowerShell.Core\FileSystem::C:\temp\Test - Copy\File1.csv, System.String PSParentPath=Microsoft.PowerShell.Core\FileSystem::C:\temp\Test - Copy, System.String
PSChildName=File1.csv, System.Management.Automation.PSDriveInfo PSDrive=C...}
Methods : {string get_Name(), long get_Length(), string get_DirectoryName(), System.IO.DirectoryInfo get_Directory()...}
ImmediateBaseObject : File1.csv
TypeNames : {System.IO.FileInfo, System.IO.FileSystemInfo, System.MarshalByRefObject, System.Object}
是的,所以 ImmediateBaseObject 列出了我们使用-Exclude 的结果的完整路径,但没有列出其他结果。对我来说,为了应用-Include 或-Exclude,PowerShell 必须引用对象的完整路径,但如果你不使用它,它只会引用对象本身。如何解决这个问题?收集结果后,使用Where 语句过滤结果。
这并不能回答为什么?!?的问题,但它确实解释了我是如何到达这里的,希望能帮助您在未来解决类似的问题。有时更重要的是弄清楚如何使用你所拥有的东西然后弄清楚为什么它会以它的方式工作。