从数组中选取一个首选值

时间:2016-03-11 23:17:57

标签: powershell

我有一个存储在

$q

中的数组
first name: Niranjan 
Last name: Raguraman
Type: Employer
Location: India 

first name: Sunil
Last name: Kumar 
Type: Employee
Location: US

现在我将在这里实现一个foreach并遍历值。我希望powershell根据我的偏好返回变量。

foreach ($a in $q.type)
{
"Not sure how to make it return the variable in the order of my preference 
1st preference - Employee
2nd preference - Employer if there is no employee, then return employer as the value 
"
}

希望我明白这个问题

1 个答案:

答案 0 :(得分:0)

尝试:

$employee = @($q | Where-Object { $_.Type -eq 'Employee'})
if($employee.Count -gt 0) { 
    #1st preference - Employee
    $employee
} else {
    #2nd preference - Employer if there is no employee, then return employer as the value 
    $q
}