Powershell 查找数组是否有值,然后从该数组复制id

Powershell 查找数组是否有值,然后从该数组复制id,powershell,Powershell,我正在Okta中编写一个脚本,试图只返回特定的数据。是否可以查看factorType的值是否等于sms,然后获取相应的ID 这是我被退回的一个例子: [ { "id": "ufs2bysphxKODSZKWVCT", "factorType": "question", "provider": "OKTA", "vendorName&

我正在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"
          ]
        }
      }
    }
  }
]
如前所述,使用
convertfromjson
创建一个对象。然后,您可以根据需要进行筛选/选择

$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


您好,很有趣,也许可以使用
convertfromjson
,然后选择匹配的值:
$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