powershelldhcpscope-id

Powershell - Select-Objects from previous commands


I can't seem to understand how to get a previous-command-object displayed on the final table in powershell. I understand I'm not explaining myself that well, so I'll go with the examples :)

get-dhcpserverv4scope -computername server01

This will return me something like this:

ScopeID,SubnetMask,Name

10.0.10.0,255.255.255.0,Scope1

10.5.0.0,255.255.248.0,Scope2

Now, when I run this instead I can't get ScopeID to be displayed with the second command results:

get-dhcpserverv4scope -computername server01 | ForEach-Object {get-dhcpserverv4optionvalue -computername server01 -scopeid $_.scopeid} | where {$_.OptionID -like "6"} | Select-Object Name, OptionID, Value

I obviously get to select the Objects from the get-dhcpserverv4optionvalue. How can I get also the ScopeID included in the latest Select-Object? Is it actually possible?

Thanks


Solution

  • You can use Add-Member to add a property to the each object returned by dhcpserverv4optionvalue:

    (get-dhcpserverv4optionvalue -computername server01 -scopeid $_.scopeid) | 
    Add-Member -MemberType NoteProperty -Name "ScopeID " -Value $_.ScopeID -PassThru
    

    The full oneliner will be:

    get-dhcpserverv4scope -computername server01 | ForEach-Object {(get-dhcpserverv4optionvalue -computername server01 -scopeid $_.scopeid) | Add-Member -MemberType NoteProperty -Name "ScopeID " -Value $_.ScopeID -PassThru} | where {$_.OptionID -like "6"} | Select-Object Name, OptionID, Value