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..
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
ASKER
Ok, I will....just one quick question....why not use 'return'? maybe that's where I'm getting confused...
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
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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...
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
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:
Chris
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
HTHChris
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}
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
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
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
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,DEP ARTMENT,ST ATE
12345,HG98234,Ringo,Star,M anager,Acc ounting,TX
54321,NA23450,Michael,Jack son,Superv isor,Finan ce,CO
43216,BG23540,Hillary,Clin ton,Manage r,Operatio ns,WA
#CSV file2. Active Directory file.
#
EMPLOYEE_NUMBER,FIRST_NAME ,LAST_NAME ,ZIP_CODE
12345,Ringo,Star,12345
54321,Michael,Jackson,6543 2
43216,Hillary,Clinton,2134 5
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
Here are the structures for each of the files.
#CSV file1. HR Reference file.
#
EMPLOYEE_NUMBER,UNIQUE_ID,
12345,HG98234,Ringo,Star,M
54321,NA23450,Michael,Jack
43216,BG23540,Hillary,Clin
#CSV file2. Active Directory file.
#
EMPLOYEE_NUMBER,FIRST_NAME
12345,Ringo,Star,12345
54321,Michael,Jackson,6543
43216,Hillary,Clinton,2134
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
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
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
Open in new window