PowerShell icon indicating copy to clipboard operation
PowerShell copied to clipboard

Join-Object -Type AllInBoth & -RightProperties outputs empty value for JoinProperty

Open mi-hol opened this issue 7 years ago • 1 comments

Question: How can below specified required result be achieved?

Data examples:

file1: hostname;IP Address s1;10.10.1.1 s3;10.10.1.3

file2: Hostname;exist s1;Y missing1;Y

Required result:

file3: hostname;IP Address;exist "s3";"10.10.1.3"; "s1";"10.10.1.1";"Y" "missing1";;"Y" I've tried:

$CSVfieldDelimiter =';'
# Get the files loaded into objects
$Object1 = Import-CSV file1 -Delimiter $CSVfieldDelimiter 
$Object2 = Import-CSV file2 -Delimiter $CSVfieldDelimiter 
# Dot source the file
. .\join-object.ps1

Join-Object -Left $Object1 -Right $Object2 `
			-LeftJoinProperty Hostname `
			-RightJoinProperty Hostname `
			-RightProperties exist `
			-Type AllInBoth |
    Export-CSV $file3 -Delimiter $CSVfieldDelimiter -NoTypeInformation

but result is: file3: hostname;IP Address;exist "s3";"10.10.1.3"; "s1";"10.10.1.1";"Y" ;;"Y"

mi-hol avatar Mar 05 '18 17:03 mi-hol

The problem is the columns selected for storage - missing the RightProperty "hostname". If you use "-Type=AllInBoth" then you will need to include the key (hostname) column from both tables. Make it: "-RightProperties hostname,exist" (without the quotes). Or just leave argument out to get all columns from that table. You already have all of the LeftProperties by default. If you want to clean up extra columns (only have one hostname column) then do a pass of the resulting joined object in memory - to copy the existing value from the secondary hostname column, where it exists, into the first hostname column (but don't copy the blanks) - before you save the data.

LUCScape avatar May 30 '18 01:05 LUCScape