0

I am trying to run a diff of two files using Compare-Object cmdlet and print the ones missing in the second file (slave.txt) when compared to the first file (master.txt). In this example, I wanted to print user1, user3 which are in master.txt but not present in slave.txt. I have a pre-requisite of storing each values in the file as a variable, so "mobj" and "sobj" cannot be ignored.

I am finding this error when running the script. what could be the issue here?

~]# cat master.txt
user1
user2
user3

~]# cat slave.txt
user2
user4

Code:

$mfile = Get-Content "C:\master.txt"
$sfile = Get-Content "C:\slave.txt" 
foreach ($mobj in $mfile) {

foreach($sobj in $sfile){
        Compare-Object (ls $mobj) (ls $sobj) -Property Name, Length, LastWriteTime -passthru | Where { $_.PSParentPath -eq (gi $mobj).PSPath } 

    }
}

The error reported is:

Compare-Object : Cannot bind argument to parameter 'ReferenceObject' because it is null.
2
  • 2
    Why are you trying to compare the output of ls (which is an alias for Get-ChildItem)? Commented Nov 16, 2015 at 15:07
  • 1
    Please go back to the question you got the Compare-Object line from and carefully review all answers to that question. Commented Nov 16, 2015 at 15:07

2 Answers 2

1

Er, not what your last question meant. Just because that question was marked as a duplicate doesn't mean that's the exact unmodified code you have to use.

Try this:

$mfile = Get-Content "C:\master.txt"
$sfile = Get-Content "C:\slave.txt" 

#In master.txt not in slave.txt
Compare-Object $mfile $sfile | Where-Object { $_.SideIndicator -eq '<=' } | Select-Object -ExpandProperty InputObject

#In slave.txt not in master.txt
Compare-Object $mfile $sfile | Where-Object { $_.SideIndicator -eq '=>' } | Select-Object -ExpandProperty InputObject
Sign up to request clarification or add additional context in comments.

Comments

0

Either start running your code from the directory where the user's folders are, or you need to edit your master and slave files.

You're runnig LS/Dir and without executing in the source directory where the user's profiles are found, or explicitly specifying the full path to their profiles in the source file, you're going to have this error, because LS will throw a non-terminating error(which is interpreted as a null, as no data or objects go to stdout/the pipeline) if it can't find a directory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.