• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1252
  • Last Modified:

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

0
bndit
Asked:
bndit
  • 6
  • 3
1 Solution
 
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now