locked
Merge two arrays with multiple properties each RRS feed

  • Question

  • I've been trying a little bit back and forth, but i need some tips i guess

    I am trying to merge two arrays together:

    $Owner = Get-CimInstance Win32_Process | ForEach { Invoke-CimMethod -InputObject $_ -MethodName GetOwner -ErrorAction SilentlyContinue}
    
    $Processes = Get-CimInstance Win32_Process

    How can i merge those so that Owner corresponds to the service running?

    ProcessName          : taskeng.exe
    Handles                  : 111
    VM                         : 62992384
    WS                        : 9220096
    Path                       : C:\Windows\system32\taskeng.exe
    Owner                   : SOME USER

    I was trying to use Add-Member, and succeed with one process, not sure how to do it for the entire list

    $1 = Get-CimInstance Win32_Process -Filter "Name = 'notepad.exe'"
    Add-Member -InputObject $1 -NotePropertyName Owner -NotePropertyValue SOME_USER
    $1.Owner



    Monday, November 7, 2016 5:46 PM

Answers

  • You need to do it in the loop"

    Get-CimInstance Win32_Process | 
    	ForEach { 
    		$owner = Invoke-CimMethod -InputObject $_ -MethodName GetOwner -ErrorAction SilentlyContinue
    		$_ | Add-Member -MemberType NoteProperty -Name Owner -Value $owner.User -PassThru
    	}
    


    \_(ツ)_/

    • Marked as answer by CesarTabares Thursday, November 10, 2016 1:10 PM
    Monday, November 7, 2016 6:18 PM