Reputation: 15
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
Upvotes: 0
Views: 816
Reputation: 1868
Not tested, but this should work:
get-dhcpserverv4scope -computername server01 | ForEach-Object {
$ScopeID = $_.ScopeID
get-dhcpserverv4optionvalue -computername server01 -scopeid $_.scopeid
} | where {
$_.OptionID -like "6"
} | Select-Object Name, OptionID, Value, @{L='ScopeID';E={$ScopeID}}
I am basically saving the ScopeID value within the ForEach statement and then using at the end by creating a custom property at the end.
Upvotes: 1
Reputation: 1723
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
Upvotes: 1