Powershell function - return object

I'm looking to put a script together...and the first step is to compare two lists based on a common attribute such as 'employee number'....I'm having difficulty understanding how I can return the result object from the comparison. I get "a positional parameter cannot be found that accepts argument 'return'" error message if I change

This >>> Compare-Object -ReferenceObject $one -DifferenceObject $two -Property $three -IncludeEqual
For this >> return $(Compare-Object -ReferenceObject $one -DifferenceObject $two -Property $three -IncludeEqual)

The reason why I want to return the object is because I need to do other things with it. So in my script I'd call the function like this:

$somevar = compare-lists $one $two $three  >>> to pass $somevar to another function etc..etc..



$one = Import-Csv 'C:\Ref_List.csv'
$two = Import-Csv 'C:\Second_List.csv'
$three = 'EMPLOYEE_NUMBER'

Function Compare-Lists {
	param($one,
		$two,
		$three)
	
	Compare-Object -ReferenceObject $one -DifferenceObject $two -Property $three -IncludeEqual
}

compare-lists $one $two $three

Open in new window

LVL 2
bnditAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AkhaterCommented:
Try the below and use $rtr later on


$one = Import-Csv 'C:\Ref_List.csv'
$two = Import-Csv 'C:\Second_List.csv'
$three = 'EMPLOYEE_NUMBER'
$rtr = @()

Function Compare-Lists {
	param($one,
		$two,
		$three)
	
	$rtr = Compare-Object -ReferenceObject $one -DifferenceObject $two -Property $three -IncludeEqual
}

Open in new window

0
bnditAuthor Commented:
Ok, I will....just one quick question....why not use 'return'? maybe that's where I'm getting confused...
0
bnditAuthor Commented:
I tried it...the function executes without problems but I don't see the first 20 entries...what am I doing wrong?
$one = Import-Csv 'C:\Ref_List.csv'
$two = Import-Csv 'C:\Second_List.csv'
$three = 'EMPLOYEE_NUMBER'
$rtn = @()

# Compares two sorted lists
Function Compare-Lists {
	param($one,
		$two,
		$three)
	
	$rtn = Compare-Object -ReferenceObject $one -DifferenceObject $two -Property $three -IncludeEqual
}

$val = compare-lists $one $two $three
$val | Select-Object EMPLOYEE_NUMBER -First 20

Open in new window

0
Redefine Your Security with AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Check out our on-demand webinar to learn more about how AI can help your organization!

Chris DentPowerShell DeveloperCommented:
Compare-Lists does not return. You assign things to $rtn, which is globally defined. But $val is $Null.

Get rid of $rtn:
Function Compare-Lists {
	param($one,
		$two,
		$three)

	# This will be left in the output pipeline from this function.
	Compare-Object -ReferenceObject $one -DifferenceObject $two -Property $three -IncludeEqual
}

$one = Import-Csv 'C:\Ref_List.csv'
$two = Import-Csv 'C:\Second_List.csv'
$three = 'EMPLOYEE_NUMBER'

# Assign the output from Compare-Lists to a variable
$val = compare-lists $one $two $three
# Do other things with that variable
$val | Select-Object EMPLOYEE_NUMBER -First 20

Open in new window

Then $val will have a value and the select should work.

Chris
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bnditAuthor Commented:
@Chris
I tested your suggestion and it worked....however, I had a question....how different is your suggestion to what I ended up using....using return $ ( ) inside the function...
$one = Import-Csv 'C:\Ref_List.csv'
$two = Import-Csv 'C:\Second_List.csv'
$three = 'EMPLOYEE_NUMBER'

# Compares two sorted lists
Function Compare-Lists {
	param($one,
		$two,
		$three)
	
	return $(Compare-Object -ReferenceObject $one -DifferenceObject $two -Property $three -IncludeEqual)
	
}

$val = compare-lists $one $two $three

$val | Select-Object -First 20

Open in new window

0
Chris DentPowerShell DeveloperCommented:
In this case, not any different at all. Mine only has the advantage that it's less to write :)

That said, return has to be used with a bit of care, although there's nothing wrong with what you have above. Perhaps consider these examples to demonstrate its use:
Function Do-Stuff1 {
  $Test = $True
  If ($Test) {
    Return "Test is set"
  }
  Return "Test is not set"
}

Function Do-Stuff2 {
  $Test = $True
  If ($Test) {
    "Test is set"
  }
  "Test is not set"
}

Do-Stuff1
Do-Stuff2

Open in new window

HTH

Chris
0
bnditAuthor Commented:
@Chris
I see the difference...thanks. One last thing, you see how I'm using the property "EMPLOYEE_NUMBER" value to do the comparison...how could I return a more valuable output with more columns in addition to EMPLOYEE_NUMBER? For example, say that I wanted to return two additional columns from variable $one (FIRST_NAME and LAST_NAME) so that the output would be as follows:

EMPLOYEE_NUMBER,FIRST_NAME,LAST_NAME

The way I manage to do this right now is by using a different function...but wondering if the Compare-Object had an easier way get this output.

I'm looking for something like the Sort-Object where I pass the CSV file and tell it to sort on a given column and the ouput contains all the columns in sorted order.....like this:

Import-CSV C:\file.csv | Sort-Object {[int] $_.EMPLOYEE_NUMBER}
0
Chris DentPowerShell DeveloperCommented:
$val, the result of CompareObject should have an InputObject. That means you should be able to grab additional properties.

If it is not giving you anything else, can you post sample copies of your files? It's for testing only, so feel free to fabricate data, I only need the format of each.

Chris
0
bnditAuthor Commented:
@Chris

Here are the structures for each of the files.

#CSV file1. HR Reference file.
#
EMPLOYEE_NUMBER,UNIQUE_ID,FIRST_NAME,LAST_NAME,TITLE,DEPARTMENT,STATE
12345,HG98234,Ringo,Star,Manager,Accounting,TX
54321,NA23450,Michael,Jackson,Supervisor,Finance,CO
43216,BG23540,Hillary,Clinton,Manager,Operations,WA


#CSV file2. Active Directory file.
#
EMPLOYEE_NUMBER,FIRST_NAME,LAST_NAME,ZIP_CODE
12345,Ringo,Star,12345
54321,Michael,Jackson,65432
43216,Hillary,Clinton,21345

I found Shay's script basically joins the two files, but for some reason it's not working for me..

http://www.powergui.org/thread.jspa?threadID=6773

0
bnditAuthor Commented:
Thanks for the help.

For a supplement to the answer to this question using a hashtable see this other post

http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_26928287.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.