powershell - 查找数组是否有值,然后从该数组中复制 id
问题描述
我正在 Okta 中编写一个脚本,并试图只返回特定数据。是否可以查看factorType的值是否等于sms然后获取相应的ID?
这是我被退回的一个例子:
[
{
"id": "ufs2bysphxKODSZKWVCT",
"factorType": "question",
"provider": "OKTA",
"vendorName": "OKTA",
"status": "ACTIVE",
"created": "2014-04-15T18:10:06.000Z",
"lastUpdated": "2014-04-15T18:10:06.000Z",
"profile": {
"question": "favorite_art_piece",
"questionText": "What is your favorite piece of art?"
},
"_links": {
"questions": {
"href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/questions",
"hints": {
"allow": [
"GET"
]
}
},
"self": {
"href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/ufs2bysphxKODSZKWVCT",
"hints": {
"allow": [
"GET",
"DELETE"
]
}
},
"user": {
"href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL",
"hints": {
"allow": [
"GET"
]
}
}
}
},
{
"id": "ostf2gsyictRQDSGTDZE",
"factorType": "token:software:totp",
"provider": "OKTA",
"status": "PENDING_ACTIVATION",
"created": "2014-06-27T20:27:33.000Z",
"lastUpdated": "2014-06-27T20:27:33.000Z",
"profile": {
"credentialId": "dade.murphy@example.com"
},
"_links": {
"next": {
"name": "activate",
"href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/ostf2gsyictRQDSGTDZE/lifecycle/activate",
"hints": {
"allow": [
"POST"
]
}
},
"self": {
"href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/ostf2gsyictRQDSGTDZE",
"hints": {
"allow": [
"GET"
]
}
},
"user": {
"href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL",
"hints": {
"allow": [
"GET"
]
}
}
},
"_embedded": {
"activation": {
"timeStep": 30,
"sharedSecret": "HE64TMLL2IUZW2ZLB",
"encoding": "base32",
"keyLength": 16
}
}
},
{
"id": "sms2gt8gzgEBPUWBIFHN",
"factorType": "sms",
"provider": "OKTA",
"status": "ACTIVE",
"created": "2014-06-27T20:27:26.000Z",
"lastUpdated": "2014-06-27T20:27:26.000Z",
"profile": {
"phoneNumber": "+1-555-415-1337"
},
"_links": {
"verify": {
"href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/sms2gt8gzgEBPUWBIFHN/verify",
"hints": {
"allow": [
"POST"
]
}
},
"self": {
"href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/sms2gt8gzgEBPUWBIFHN",
"hints": {
"allow": [
"GET",
"DELETE"
]
}
},
"user": {
"href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL",
"hints": {
"allow": [
"GET"
]
}
}
}
}
]
解决方案
正如@IronMan评论的那样,用于ConvertFrom-Json
创建一个对象。然后您可以根据需要过滤/选择。
$str = '[ { "id": "ufs2bysphxKODSZKWVCT", "factorType": "question", "provider": "OKTA", "vendorName": "OKTA", "status": "ACTIVE", "created": "2014-04-15T18:10:06.000Z", "lastUpdated": "2014-04-15T18:10:06.000Z", "profile": { "question": "favorite_art_piece", "questionText": "What is your favorite piece of art?" }, "_links": { "questions": { "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/questions", "hints": { "allow": [ "GET" ] } }, "self": { "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/ufs2bysphxKODSZKWVCT", "hints": { "allow": [ "GET", "DELETE" ] } }, "user": { "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL", "hints": { "allow": [ "GET" ] } } } }, { "id": "ostf2gsyictRQDSGTDZE", "factorType": "token:software:totp", "provider": "OKTA", "status": "PENDING_ACTIVATION", "created": "2014-06-27T20:27:33.000Z", "lastUpdated": "2014-06-27T20:27:33.000Z", "profile": { "credentialId": "dade.murphy@example.com" }, "_links": { "next": { "name": "activate", "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/ostf2gsyictRQDSGTDZE/lifecycle/activate", "hints": { "allow": [ "POST" ] } }, "self": { "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/ostf2gsyictRQDSGTDZE", "hints": { "allow": [ "GET" ] } }, "user": { "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL", "hints": { "allow": [ "GET" ] } } }, "_embedded": { "activation": { "timeStep": 30, "sharedSecret": "HE64TMLL2IUZW2ZLB", "encoding": "base32", "keyLength": 16 } } }, { "id": "sms2gt8gzgEBPUWBIFHN", "factorType": "sms", "provider": "OKTA", "status": "ACTIVE", "created": "2014-06-27T20:27:26.000Z", "lastUpdated": "2014-06-27T20:27:26.000Z", "profile": { "phoneNumber": "+1-555-415-1337" }, "_links": { "verify": { "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/sms2gt8gzgEBPUWBIFHN/verify", "hints": { "allow": [ "POST" ] } }, "self": { "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/sms2gt8gzgEBPUWBIFHN", "hints": { "allow": [ "GET", "DELETE" ] } }, "user": { "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL", "hints": { "allow": [ "GET" ] } } } } ]'
$obj = $str | ConvertFrom-Json
$obj | Where-Object factorType -eq 'sms' | Select-Object -ExpandProperty id
或者
$str = '[ { "id": "ufs2bysphxKODSZKWVCT", "factorType": "question", "provider": "OKTA", "vendorName": "OKTA", "status": "ACTIVE", "created": "2014-04-15T18:10:06.000Z", "lastUpdated": "2014-04-15T18:10:06.000Z", "profile": { "question": "favorite_art_piece", "questionText": "What is your favorite piece of art?" }, "_links": { "questions": { "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/questions", "hints": { "allow": [ "GET" ] } }, "self": { "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/ufs2bysphxKODSZKWVCT", "hints": { "allow": [ "GET", "DELETE" ] } }, "user": { "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL", "hints": { "allow": [ "GET" ] } } } }, { "id": "ostf2gsyictRQDSGTDZE", "factorType": "token:software:totp", "provider": "OKTA", "status": "PENDING_ACTIVATION", "created": "2014-06-27T20:27:33.000Z", "lastUpdated": "2014-06-27T20:27:33.000Z", "profile": { "credentialId": "dade.murphy@example.com" }, "_links": { "next": { "name": "activate", "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/ostf2gsyictRQDSGTDZE/lifecycle/activate", "hints": { "allow": [ "POST" ] } }, "self": { "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/ostf2gsyictRQDSGTDZE", "hints": { "allow": [ "GET" ] } }, "user": { "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL", "hints": { "allow": [ "GET" ] } } }, "_embedded": { "activation": { "timeStep": 30, "sharedSecret": "HE64TMLL2IUZW2ZLB", "encoding": "base32", "keyLength": 16 } } }, { "id": "sms2gt8gzgEBPUWBIFHN", "factorType": "sms", "provider": "OKTA", "status": "ACTIVE", "created": "2014-06-27T20:27:26.000Z", "lastUpdated": "2014-06-27T20:27:26.000Z", "profile": { "phoneNumber": "+1-555-415-1337" }, "_links": { "verify": { "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/sms2gt8gzgEBPUWBIFHN/verify", "hints": { "allow": [ "POST" ] } }, "self": { "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL/factors/sms2gt8gzgEBPUWBIFHN", "hints": { "allow": [ "GET", "DELETE" ] } }, "user": { "href": "https://${yourOktaDomain}/api/v1/users/00u15s1KDETTQMQYABRL", "hints": { "allow": [ "GET" ] } } } } ]'
($str | ConvertFrom-Json) | Where-Object factorType -eq 'sms' | Select-Object -ExpandProperty id
推荐阅读
- java - 如何正确定位 JButtons 和大小 JTextFields?
- reporting-services - 如何使用表达式计算 SSRS 中的 RunningValue %
- java - 按组对列表进行排序的效率如何?
- javascript - 需要基于现有对象在 Javascript 中创建对象,考虑其性能是有效的
- java - Java 添加到 ArrayList
然后转换为 Object[][] - python - Python列表输入2个相同的单词,如何只打印一次
- arrays - 如何从包含二进制数据的字符串中提取模式
- html - 尝试将样式添加到模态表单
- python - 如何使用 Python pathlib 处理文件名中的多个点?
- r - 将输入向量的名称传递给 R 函数