I have confiuration files on two servers. The configurations need to be compared across multiple directories and sub-directories.
$FirstPath = "C:\Users\me\Desktop\server001"
$SecondPath = "C:\Users\me\Desktop\server002"
$FirstComp = Get-ChildItem -Recurse –Path $FirstPath -Filter *.config -Name
$SecondComp = Get-ChildItem -Recurse –Path $SecondPath -Filter *.config -Name
Compare-Object -ReferenceObject (Get-Content -Path $FirstComp) -DifferenceObject (Get-Content -Path $SecondComp)
If i specify a single file in the -Path
statment it works correctly. If I enter $FirstComp
or $SecondComp
they return the file structure and show the information as expected. I have tried it using a ForEach-Object
and a few other ways, but no luck.
Strangely, I get an error message that references the system32 directory that I am not trying to reference, Or that the variable is Null.
I am also hoping to eventually be able to do some error handling if the -Difference-Object is missing.
Get-Content : Cannot find path 'C:\WINDOWS\system32\Web\Wcf\Web.config' because it does not
exist.
At line:1 char:77
... (Get-Content $FirstComp) -DifferenceObject (Get-Content $SecondComp)
~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ObjectNotFound: (C:\WINDOWS\syst...\Wcf\Web.config:String) [Get-Content], ItemNotFoundException
FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Compare-Object : Cannot bind argument to parameter 'ReferenceObject' because it is null.
At line:1 char:33
Compare-Object -ReferenceObject (Get-Content $FirstComp) -DifferenceO ...
~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidData: (:) [Compare-Object], ParameterBindingValidationException
FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CompareObjectCommand
Any help or guidance would be greatly appreciated.
CodePudding user response:
You should compare your files by one by one, not within array.
$FirstPath = "C:\Users\me\Desktop\server001"
$SecondPath = "C:\Users\me\Desktop\server002"
$FirstComp = Get-ChildItem -Recurse –Path $FirstPath -Filter *.config -Name
$SecondComp = Get-ChildItem -Recurse –Path $SecondPath -Filter *.config -Name
foreach ( $Config1 in $FirstComp ){
$SubPath1 = $Config1.fullname.replace($FirstPath, '')
foreach ( $Config2 in $SecondComp ){
$SubPath2 = $Config1.fullname.replace($SecondPath, '')
if ( $SubPath1 -eq $SubPath2 ){
Compare-Object -ReferenceObject ( Get-Content -Path $Config1 ) -DifferenceObject ( Get-Content -Path $Config2 )
}
}
}