0

I have employee details in employee.csv, which I have compiled each row in PS objects.

Now there will be -NotePropertyName "EmployeeTotal_LeaveCount" and the --NotePropertyValue should be the total occurrence of "YES" in the final object-

Single PSObject

@{EmployeeID=MACL22656; EmployeeRole=Accountant; EmployeeDepartment=Accounts; EmployeeLeave_Monday=YES; EmployeeLeave_Tuesday=YES; EmployeeLeave_Thursday=YES}

Expected Final Object:

@{EmployeeID=MACL22656; EmployeeRole=Accountant; EmployeeDepartment=Accounts; EmployeeLeave_Monday=YES; EmployeeLeave_Tuesday=YES; EmployeeLeave_Thursday=YES; EmployeeTotal_LeaveCount=3}

So far I tried

$employee|Add-Member -NotePropertyName "EmployeeTotal_LeaveCount" -NotePropertyValue ($employee|Select-String -CaseSensitive -Pattern 'YES' | Measure-Object).count

But it returns - 1 instead of 3 [as 'YES' occurred 3 times in Object]

Please help to achieve the expected result.

1 Answer 1

1

To iterate over properties whose name start with EmployeeLeave_ and it's value is YES:

$Employee=[PSCustomObject]@{EmployeeID="MACL22656"
                            EmployeeRole="Accountant"
                            EmployeeDepartment="Accounts"
                            EmployeeLeave_Monday="YES"
                            EmployeeLeave_Tuesday="YES"
                            EmployeeLeave_Thursday="YES"
}

$Employee|Add-Member -NotePropertyName "EmployeeTotal_LeaveCount"  `
    -NotePropertyValue ($Employee.PSObject.Properties|
        where-Object {$_.Name -like "EmployeeLeave_*" -and $_.Value -eq "YES"}).count
$Employee

Sample output:

EmployeeID               : MACL22656
EmployeeRole             : Accountant
EmployeeDepartment       : Accounts
EmployeeLeave_Monday     : YES
EmployeeLeave_Tuesday    : YES
EmployeeLeave_Thursday   : YES
EmployeeTotal_LeaveCount : 3
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.