我在employee.csv中有员工详细信息,我已经编译了PS对象中的每一行。
现在会有-NotePropertyName "EmployeeTotal_LeaveCount"
,而-NotePropertyValue
应该是最终对象中"YES"
的总出现次数 -
单个PSObject
@{EmployeeID=MACL22656; EmployeeRole=Accountant; EmployeeDepartment=Accounts; EmployeeLeave_Monday=YES; EmployeeLeave_Tuesday=YES; EmployeeLeave_Thursday=YES}
预期的最终对象:
@{EmployeeID=MACL22656; EmployeeRole=Accountant; EmployeeDepartment=Accounts; EmployeeLeave_Monday=YES; EmployeeLeave_Tuesday=YES; EmployeeLeave_Thursday=YES; EmployeeTotal_LeaveCount=3}
到目前为止,我试过
$employee|Add-Member -NotePropertyName "EmployeeTotal_LeaveCount" -NotePropertyValue ($employee|Select-String -CaseSensitive -Pattern 'YES' | Measure-Object).count
但它返回 - 1而不是3 [因为'是'在对象中发生了3次]
请帮助达到预期效果。
答案 0 :(得分:1)
要迭代名称以EmployeeLeave_
开头并且值为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
示例输出:
EmployeeID : MACL22656
EmployeeRole : Accountant
EmployeeDepartment : Accounts
EmployeeLeave_Monday : YES
EmployeeLeave_Tuesday : YES
EmployeeLeave_Thursday : YES
EmployeeTotal_LeaveCount : 3