Jim842
Jim842

Reputation: 23

Replace value in list with calculated property powershell

I have two lists in powershell that have the following structure:

Variable = $serviceusage2

ServiceName ActivatedUsers
Serivce1 24
Service2 4
Service3 10

Variable $spdata

PlanID SerivcePlanName ServicePlanDisplayName
GUID1 Service1 Nice Name for Service 1
GUID2 Service2 Nice Name for Service 2
GUID3 Service3 Nice Name for Service 3

The ServiceName and ServicePlan name match in each list.

I'm trying to build out a single list that will add a new column using the ServicePlanDisplayName based on match of the service name using the claculated properties of select-object

I've tried:

$serviceuage2 | Select-Object Servicename, @{ n= 'FriendlyName'; e= {$_ | ForEach-Object { @($SPdata.Servicename[$_.Servicename]).ServicePlanDisplayName}}},ActivatedUsers

and some variations of that, but I'm either getting no response or an error saying the value of the parameter is null.

Select-Object : The value of a parameter was null; one of the following types was expected: {System.String,
System.Management.Automation.ScriptBlock}.
At line:1 char:17
+ ... viceuage2 | Select-Object $_.Servicename, @{ n = 'FriendlyName';e= {$ ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], NotSupportedException
    + FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Commands.SelectObjectCommand

Upvotes: 1

Views: 89

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 60848

Assuming $spdata is an array of objects, first convert it into a hash table, for this Group-Object -AsHashtable helps greatly:

$spdata = $spdata | Group-Object SerivcePlanName -AsHashtable

Once you have your lookup table you can do the calculated property this way:

$serviceusage2 | Select-Object Servicename, @{ N='FriendlyName'; E={ $spdata[$_.Servicename].ServicePlanDisplayName }}, ActivatedUsers

Upvotes: 0

Related Questions