Pipeline Variable Access Property by Variable

I tried for hours to get this to work :slight_smile: any help would be greatly appreciated as it would massively speed up merge operations.

Why is $.$something or $.$($something) a no go??

[pre]$fieldsIneed = import-csv ‘C:\Users\NickCrooks\OneDrive - ABC_CO\XYZ_CO-Data_Editing\SQL\HTABC.rpt’ #Select * from Table 1;
$xyzvend = import-csv ‘someotherfile.rpt’ #select * from table2;
#$fieldsIneed | GM -MemberType NoteProperty | Select Name, Definition | Out-GridView

$fieldsIneed | %{
$p = $_ #this property being evaluated at the moment
if($p -inotin $($xyzvend | GM -MemberType NoteProperty)){$xyzvend | Add-Member -MemberType NoteProperty -Name $p.name -Value ‘’ -force}
}

#$xyzvend | GM -MemberType NoteProperty | ?{$.name -like “EFT*”} #confirm success
$propsIcareabout = $xyzvend | GM -MemberType NoteProperty | ?{$
.name -like “EFT*”} | select name #easy in this case
#$propsIcareabout += ‘some other manual props if needed’

#region improve data gathering

#region merge properties

$propsIcareabout | %{
$p = $_.name
Write-verbose “Clear the Hash” -Verbose
$hash = @{} #setup merger object, you need matching keys and you can make those keys if needed

Write-Host “$($p)” -ForegroundColor Yellow
pause
#region Object 1
#Go to object 1, git yo stuff and git out
$fieldsIneed | ?{$.vendorid -ne ‘’ -and $.vendorid -ne $null -and $(if($.ADRSCODE.gettype() -like “string”) {$.ADRSCODE.trim()}) -eq ‘PRIMARY’} | %{ #assume data cleaning is always needed

$vid = $_.vendorid.trim() #ideally no trimming or formatting needed

#Write-verbose “Found $($p) and $($vid)” -Verbose
#pause
if( -not $hash.ContainsKey($vid)){$hash.Add($vid, $_.$p)}
$hash
#pause

#Note improvement - if the entire property to be merged is null then skip next iteration
#end object 1 data gathering loop
}
#endregion object 1

#region object 2
write-warning “Begin Second Object - Data Write”
$xyzvend | ?{$.vendorid -ne ‘’ -and $.vendorid -ne $null} | %{
$vid = $.vendorid.trim()
write-verbose “$($vid)” -verbose
<#$
."$($p.tostring())"
$.$p
$test = '$
.’+$p
& $test.tostring()
$.psobject.properties.$($p.tostring())
[string]$omfg = $p
[string]$killmenow = $p.tostring()
$
.psobject.properties.$omfg
$.$omfg
#$
.psobject.properties.add($omfg)
#>
$_
#$xyzvend.$p = $crmhash[$($vid)]

write-host “$($p)” -ForegroundColor Green
pause
}
#endregion object 2
}[/pre]

Due to the time crunch here’s what I did (repeating for each field I needed)

[pre]

if($hash -ne $null) {$hash = @{}}
$fieldsIneed | ?{$.vendorid -ne ‘’ -and $.vendorid -ne $null -and $(if($.ADRSCODE.gettype() -like “string”) {$.ADRSCODE.trim()}) -eq ‘PRIMARY’} | %{
$p = $.vendorid
if( -not $hash.ContainsKey($p)){$hash.Add($p, $
.UseMasterID)}
}

#add in Remaining Hours values
$xyzvend | %{
if($.vendorid -ne $null){
$
.“UseMasterID” = $hash[$($_.vendorid)]
#end not null
}
}
[/pre]

It’s simply because the dot notation is expecting a string after the dot, not a variable. You can do what you want by simply providing a string that contains a variable to resolve.

IE

$services = Get-Service

$p = "Name"

$services | ForEach-Object {
    $_."$p"
}