1

I am new to Powershell and I am trying to write a script. In that, I have an array $spCreated.

Its variable type is:

  • Name: Object[]
  • BaseType: System.Array

The $spCreated contains the value below:

[
  {
    "accountEnabled": "True",
    "addIns": [],
    "alternativeNames": [],
    "appDisplayName": "testSP4",
    "appId": "abc1234-abc1234-abc1234-abc1234-abc1234",
    "appOwnerTenantId": "",
    "appRoleAssignmentRequired": false,
    "appRoles": [],
    "applicationTemplateId": null,
    "deletionTimestamp": null,
    "displayName": "testSP4",
    "errorUrl": null,
    "homepage": null,
    "informationalUrls": {
      "marketing": null,
      "privacy": null,
      "support": null,
      "termsOfService": null
    },
    "keyCredentials": [],
    "logoutUrl": null,
    "notificationEmailAddresses": [],
    "oauth2Permissions": [
      {
        "adminConsentDescription": "text",
        "adminConsentDisplayName": "Access testSP4",
        "id": "ab123-ab123-ab123-ab123-ab123",
        "isEnabled": true,
        "type": "User",
        "userConsentDescription": "Allow the application to access testSP4 on your behalf.",
        "userConsentDisplayName": "Access testSP4",
        "value": "user_impersonation"
      }
    ],
    "objectId": "abc1234-abc1234-abc1234-abc1234-abc1234",
    "servicePrincipalType": "Application",
    "signInAudience": "AzureADMyOrg",
    "tags": [],
    "tokenEncryptionKeyId": null
  }
]

How can I get the value of objectId in a string variable?

I have tried the commands:

$z = $spCreated-match 'objectId'
$b = $z.Substring(15, $z.Length-1)

But it returns (with the quotes and comma)

"abc1234-abc1234-abc1234-abc1234-abc1234",

I just want abc1234-abc1234-abc1234-abc1234-abc1234 as a string. Can I please get help on this? Thanks

Jason
  • 27
  • 4

2 Answers2

0

I was able to figure it out with the help of regex:

$c = [regex]::match($b,'\"([^\)]+)\"').Groups[1].Value

The command above returned just the objectId in the string format. Eg: abc1234-abc1234-abc1234-abc1234-abc1234

Reference: Extract text from a string

Thanks all for the help.

Jason
  • 27
  • 4
  • 5
    It's commendable that you want to share a solution for the benefit of future readers, but it's unclear how your solution relates to the question you asked. Given that your string is clearly JSON, a solution using a parser such as [`ConvertFrom-Json`](https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/ConvertFrom-Json), as suggested in the comments, is definitely preferable. – mklement0 Jun 13 '23 at 22:12
  • @mklement0: Yes true. I changed it to ConvertFrom-Json. I was able to extract the text from the above regex too. So I posted it here. – Jason Jun 23 '23 at 14:12
  • I see. If all you need to do is trim the enclosing `"` from a string, `$b.Trim('"')` will do. I've posted an answer that shows both the `ConvertFrom-Json` solution as well as a regex solution that requires just a single regex overall. – mklement0 Jun 23 '23 at 16:51
0

Given that the elements of your input string constitute a JSON document, the simplest and robust approach is to parse that document with ConvertFrom-Json and the access the property of interest, as Martin Brown showed in a comment:

$value = 
  ($spCreated | ConvertFrom-Json).objectId # -> abc1234-abc1234-abc1234-abc1234-abc1234

If you do want to - suboptimally - solve this problem with string parsing and regexes, use a switch statement :

$value = 
  switch -Regex ($spCreated) {
    '"objectId":\s*"(.+?)"' { $Matches.1 }
  } # -> abc1234-abc1234-abc1234-abc1234-abc1234

Note the use of the automatic $Matches variable to reference what the first (.1) (and only) capture group in the regex ((.+?)) captured.


An alternative solution with a simpler regex, which uses the -split operator to extract the part of the line of interest:

$value = 
  switch -Regex ($spCreated) {
    '"objectId":' { ($_ -split '"')[-2] }
  } # -> abc1234-abc1234-abc1234-abc1234-abc1234
mklement0
  • 382,024
  • 64
  • 607
  • 775