我想运行一个PowerShell脚本,它可以由用户提供目录名,然后它将检查目录、子目录和这些目录的所有文件内容,以比较它们是否彼此相同。有8台服务器应该都有相同的文件和内容。下面的代码似乎没有执行我想要的操作。我已经看到了Compare-Object、Get-ChildItem和Get-FileHash的使用,但还没有找到正确的组合,我确信它们实际上正在完成任务。如有任何帮助,我们将不胜感激!
$35 = "\\server1\"
$36 = "\\server2\"
$37 = "\\server3\"
$38 = "\\server4\"
$45 = "\\server5\"
$46 = "\\server6\"
$47 = "\\server7\"
$48 = "\\server8\"
do{
Write-Host "|1 : New |"
Write-Host "|2 : Repeat|"
Write-Host "|3 : Exit |"
$choice = Read-Host -Prompt "Please make a selection"
switch ($choice){
1{
$App = Read-Host -Prompt "Input Directory Application"
}
2{
#rerun
}
3{
exit; }
}
$c35 = $35 + "$App" +"\*"
$c36 = $36 + "$App" +"\*"
$c37 = $37 + "$App" +"\*"
$c38 = $38 + "$App" +"\*"
$c45 = $45 + "$App" +"\*"
$c46 = $46 + "$App" +"\*"
$c47 = $47 + "$App" +"\*"
$c48 = $48 + "$App" +"\*"
Write-Host "Comparing Server1 -> Server2"
if((Get-ChildItem $c35 -Recurse | Get-FileHash | Select-Object Hash,Path).hash -eq (Get-ChildItem $c36 -Recurse | Get-FileHash | Select-Object Hash,Path).hash){"Identical"}else{"NOT Identical"}
Write-Host "Comparing Server1 -> Server3"
if((Get-ChildItem $c35 -Recurse | Get-FileHash | Select-Object Hash,Path).hash -eq (Get-ChildItem $c37 -Recurse | Get-FileHash | Select-Object Hash,Path).hash){"Identical"}else{"NOT Identical"}
Write-Host "Comparing Server1 -> Server4"
if((Get-ChildItem $c35 -Recurse | Get-FileHash | Select-Object Hash,Path).hash -eq (Get-ChildItem $c38 -Recurse | Get-FileHash | Select-Object Hash,Path).hash){"Identical"}else{"NOT Identical"}
Write-Host "Comparing Server1 -> Server5"
if((Get-ChildItem $c35 -Recurse | Get-FileHash | Select-Object Hash,Path).hash -eq (Get-ChildItem $c45 -Recurse | Get-FileHash | Select-Object Hash,Path).hash){"Identical"}else{"NOT Identical"}
Write-Host "Comparing Server1 -> Server6"
if((Get-ChildItem $c35 -Recurse | Get-FileHash | Select-Object Hash,Path).hash -eq (Get-ChildItem $c46 -Recurse | Get-FileHash | Select-Object Hash,Path).hash){"Identical"}else{"NOT Identical"}
Write-Host "Comparing Server1 -> Server7"
if((Get-ChildItem $c35 -Recurse | Get-FileHash | Select-Object Hash,Path).hash -eq (Get-ChildItem $c47 -Recurse | Get-FileHash | Select-Object Hash,Path).hash){"Identical"}else{"NOT Identical"}
Write-Host "Comparing Server1 -> Server8"
if((Get-ChildItem $c35 -Recurse | Get-FileHash | Select-Object Hash,Path).hash -eq (Get-ChildItem $c48 -Recurse | Get-FileHash | Select-Object Hash,Path).hash){"Identical"}else{"NOT Identical"}
} until ($choice -eq 3)
1条答案
按热度按时间unftdfkk1#
下面是一个示例函数,它试图有效地比较一个Reference目录和多个Difference目录。它通过首先比较最容易获得的信息和在第一个差异处停止来做到这一点。
使用示例:
输出示例:
DiffCause
列提供了该函数认为目录不同的原因。注:
Select-Object -First 1
是一个巧妙的技巧,可以在我们得到第一个结果后停止搜索。它之所以高效,是因为它不会首先处理所有输入并丢弃除第一个项目之外的所有内容,而是在找到第一个项目后取消管道。Group-Object RelativePath -AsHashTable
创建文件信息的hashtable,以便可以通过RelativePath
属性快速查找。Get-FileHash
使用的默认SHA-256算法更快,但它不安全。有人可以很容易地操纵一个不同的文件,使其具有与原始文件相同的MD5哈希。不过,在一个受信任的环境中,这并不重要。如果需要更安全的比较,请移除-Algorithm MD5
。