Pipeline Variable Access Property by Variable

Welcome Forums General PowerShell Q&A Pipeline Variable Access Property by Variable

Viewing 2 reply threads
  • Author
    Posts
    • #209625
      Participant
      Topics: 8
      Replies: 15
      Points: 76
      Rank: Member

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

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

      $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
      }
      • This topic was modified 2 months, 3 weeks ago by Nicholas J Crooks.
      • This topic was modified 2 months, 3 weeks ago by Nicholas J Crooks.
      • This topic was modified 2 months, 3 weeks ago by Nicholas J Crooks.
      • This topic was modified 2 months, 3 weeks ago by Nicholas J Crooks. Reason: Forgot to include the method I am using to capture the missing/destination properties
    • #209643
      Participant
      Topics: 8
      Replies: 15
      Points: 76
      Rank: Member

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

      
      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
      }
      }
      
    • #209718
      Participant
      Topics: 6
      Replies: 667
      Points: 97
      Helping Hand
      Rank: Member

      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"
      }
Viewing 2 reply threads
  • You must be logged in to reply to this topic.