How to get value from variable in a new member property using Add-Member

I have a cmdlet that returns data based on an identifying value, however the data returned does not contain the identifying value itself, and in order to process it, I need to add that value as a property to the returned data. An example of what I want is this:

@("WSHelper","WUDFHost") | % -PV newprocessname {$_} | % { Get-Process "$newprocessname"}| Add-Member -MemberType NoteProperty -Name NewProcessName -Value $newprocessname -PassThru | Select-Object Name, NewProcessName

However I have found no way to actually get the value of newprocessname as the value of the added property. How would I do this?

Ben,
Welcome to the forum. :wave:t3:

I’m not sure if I really get what you want to achieve.

Is it a secret or can you share what actual cmdlet we’re talking about. There might be a better way. :wink:

How do you get the value if it’s not there anymore?

Hmmm … Are you sure this example is suitable? … you’re starting with strings … PowerShell works with objects and properties …

And regardless of that …

Please do not use aliasses here in the forum since it makes the code much harder to read and please use the proper code formatting

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

I had to iterate through the first part of your code just to figure out what it does. I would say in general, it’s not helpful to use abbreviations/aliases for things when coding. If it’s just a CLI instance you’re doing for yourself, then by all means golf the heck out of it, but for the audience here (me included) it’s helpful to be verbose.
So, it looks like this section:

@("WSHelper","WUDFHost") | % -PV newprocessname {$_}

is possibly redundant? -PV is shorthand for “PipelineVariable” which is expecting a string value, so creating an array, piping it to Foreach-Object and calling the PipelineVariable parameter just to feed it the string object that already came through the pipeline is a lot of work when this would work:

Get-Process -Name "WSHelper","WUDFHost"

If you do a Get-Help against Get-Process you’ll see that the -Name parameter accepts an array for input.

However, I still don’t understand what the end goal is here. If we’re doing Get-Process on the names "WSHelper","WUDFHost" why do I need to add those as values for “NewProcessName” to the object?

2 Likes

Thanks greyOut … I was curious what that funky syntax was all about :slight_smile:

Sorry about the formatting, I thought I had to use the backticks :slight_smile: Part of the problem was that there was no preview when I wrote this. And again there was not when I started this reply. Reason was that the preview section was showing a dialog with some hints and tips and it it was not obvious to me that you can/should close that to see the preview. Your ‘how to format code’ made me aware of the preview section.

Anyway, the actual cmdlet is Get-MailboxAutoReplyConfiguration , the values we start of are the ObjectIds (Guids as strings like in the example I provided). And originally the pipleine was simple, An array of Guids was piped to the command , that was piped to a Select-Object

The problem is that the cmd works fine with the Guid provided, but the returned data does NOT contain that guid , the ‘key’ value returned is the property Identity. But that value is not unique, multiple mailboxes can have the same identity. So to know for which mailbox we requested the data, I wanted to add the original Guid as a member to the data. And that is more difficult than expected.

It is just an example of trying to add a value coming from a variable as a property to an object. The important part is getting the value substituted.

Is the code a secret or can you share it? Please share your actual code. If there are sensitive information in the code obfuscate it. :man_shrugging:t3:

No secret, but it is just as simple as I described:

@({{objectIds}}) | Get-MailboxAutoReplyConfiguration | Select-Object Identity, AutoReplyState, StartTime, EndTime

Where the {objectIds} is replaced with an array of Guids. So the whole idea is that the output at the end of the pipeline also contains the Guid we started the pipeline with.

OK, but with the Select-Object you remove everything except for the specified properties.

How does the output of Get-MailboxAutoReplyConfiguration look like?

And to add what I tried:

@({{objectIds}}) | % -PipleineVariable objectid { $_ } | Get-MailboxAutoReplyConfiguration | Add-Member -MemberType NoteProperty -Name 'ObjectId' -Value 'test' -PassThru | Select-Object Identity, AutoReplyState, StartTime, EndTime, ObjectId

Works, all objects returned have a property called ObjectId with a value of ‘test’.

But what I really want is not the value ‘test’, that should be the Guid in $objectid.


Here all the properties returned, and in this case there is a Guid in Identity, but that does not have to be. Most of the time it is a username, and not unique. And strange enough the MailboxOwnerId is aways the same value as the Identity. So if Identity is a name, MailboxOwnerId is the same name.

And the reason I gave a simpler example when starting the topic is because it is not simple to just test the Get-MailboxAutoReplyConfiguration cmdlet . So if someone manages to make the original example work, it would solve my actual problem as well.

And I solved it:

@("WSHelper","WUDFHost") | foreach{Get-Process $_ | Add-Member -MemberType NoteProperty -Name NewProcessName -Value $_ -PassThru} | Select-Object Name, NewProcessName

1 Like

Great. And thanks for sharing. :+1:t3: :love_you_gesture:t3:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.