Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell-目录-并排比较_Powershell_Powershell 2.0 - Fatal编程技术网

PowerShell-目录-并排比较

PowerShell-目录-并排比较,powershell,powershell-2.0,Powershell,Powershell 2.0,我需要在powershell中并排比较两个目录结构,但是我无法做到这一点。 寻找这样的输出 Filename Dir1 Dir2 File exists in Dir1 File exists in Dir2 ABC.txt c:\data d:\data True False ABD.txt c:\data d:\data False False ADC.txt c:\data

我需要在powershell中并排比较两个目录结构,但是我无法做到这一点。 寻找这样的输出

Filename   Dir1     Dir2      File exists in Dir1 File exists in Dir2
ABC.txt    c:\data  d:\data   True                False  
ABD.txt    c:\data  d:\data   False               False
ADC.txt    c:\data  d:\data   True                True

$SourceFiles = Get-ChildItem 'c:\data' -File 
$destFiles = Get-ChildItem 'd:\Data' -File 
Compare-Object $sourceFiles $destFiles -IncludeEqual -ExcludeDifferent -Property Name| ForEach-Object { 
   [PSCustomObject]@{ 
   Name = $_.Name 
   Path1 = ($SourceFiles | Where-Object Name -eq $_.Name).Directory 
   Path2 = ($destFiles | Where-Object Name -eq $_.Name).Directory } 
}
试试这个:

$DirSourceName='c:\temp'
$DirDestName='c:\temp2'

$SourceFiles = Get-ChildItem $DirSourceName -File 
$destFiles   = Get-ChildItem $DirDestName -File 

Compare-Object $sourceFiles $destFiles   -Property Name -IncludeEqual | %{
    [pscustomobject]@{
        FileName=$_.Name
        Dir1=$DirSourceName
        Dir2=$DirDestName
        "File exists in Dir1"=($_.SideIndicator -eq "==" -or $_.SideIndicator -eq "<=")
        "File exists in Dir2"=($_.SideIndicator -eq "==" -or $_.SideIndicator -eq "=>")
    }

}
$DirSourceName='c:\temp'
$DirDestName='c:\temp2'
$SourceFiles=获取子项$DirSourceName-File
$destFiles=获取子项$DirDestName-File
比较对象$sourceFiles$destFiles-属性名称-IncludeEqual |%{
[pscustomobject]@{
FileName=$\文件名
Dir1=$DirSourceName
Dir2=$DirDestName
文件存在于Dir1“=($\u0.SideIndicator-eq“=”-或$\u0.SideIndicator-eq”)中
}
}

您尝试了什么,以及您尝试的失败原因是什么?理想情况下,您应该提供您尝试过的内容的详细信息,并包括关于失败原因的具体信息,以及错误消息和/或错误输出。不是代码编写服务;最好的问题是那些提供有用信息的问题,这样那些回答问题的人可以引导你设计出自己的正确答案。请看一个好问题。利用
比较对象(dir c:\data)(dir d:\data)-IncludeEqual
?我已经尝试过了,但发现了以下错误。比较对象:无法将参数绑定到参数“ReferenceObject”,因为它为null。第7行字符:16+比较对象$sourceFiles$destFiles-属性名称-IncludeEqual…+~~~~~~~~~~~+CategoryInfo:InvalidData:(:)[Compare Object],ParameterBindingValidationException+FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CompareObject命令检查PowerShell版本并最终更新。检查您的脚本所在的服务器上是否存在dir1和dir2,如果您可以使用“我的脚本”访问它们,则必须更改Dirsourcename和dirdestnamell,而不是该dum。我自己也写了类似的脚本,几乎有类似的错误。脚本有点问题。