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:
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.
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.
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:
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?
Sorry about the formatting, I thought I had to use the backticks 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.
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.
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.