Link to home
Create AccountLog in
Avatar of bndit
bndit

asked on

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

Avatar of Akhater
Akhater
Flag of Lebanon image

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

Avatar of bndit
bndit

ASKER

Ok, I will....just one quick question....why not use 'return'? maybe that's where I'm getting confused...
Avatar of bndit

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of bndit

ASKER

@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

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
Avatar of bndit

ASKER

@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}
$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
Avatar of bndit

ASKER

@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

Avatar of bndit

ASKER

Thanks for the help.

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

https://www.experts-exchange.com/questions/26928287/Compare-Object-giving-two-different-results-why.html