Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

powershell - Getting single element returned from select-object into a json array of strings

I have a PowerShell script that grabs users from Google and creates requester accounts in the Freshservice ticket system. I'm using the PowerShell PSGSuite module and Invoke-RestMethod to post to Freshservice via REST API. The issue is that I need one of the resulting json attributes (secondary_emails) to be formatted as an array. I can't figure out how to do that.

Here's the code:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::TLS12
$APIKey = 'myapikey'
$Bytes = [System.Text.Encoding]::Unicode.GetBytes($APIKey)
$EncodedAPIKey = [Convert]::ToBase64String($Bytes)
$HTTPHeaders = @{}
$HTTPHeaders.Add('Authorization', ("Basic {0}" -f $EncodedAPIKey))
$HTTPHeaders.Add('Content-Type', 'application/json')
$AddURL = 'https://vusd.freshservice.com/api/v2/requesters'

$Users = Get-GSUserList -SearchBase '/Staff Accounts/Test' -SearchScope Subtree -MaxResults 500
foreach ($User in $Users) {
    $body = $user | Select-Object `
    @{Name="first_name"       ;Expression={($_.Name.GivenName)}}, `
    @{Name="last_name"        ;Expression={($_.Name.FamilyName)}}, `
    @{Name="primary_email"    ;Expression={($_.primaryemail)}}, `
    @{Name="secondary_emails" ;Expression={($_.primaryemail.split("@")[0]) + '@domain.org'}}, `
    @{Name="time_zone"        ;Expression={('Pacific Time (US & Canada)')}}, `
    @{Name="language"         ;Expression={('en')}} | ConvertTo-json
    #$body
    #Invoke-RestMethod -Method POST -body $body -Uri $AddURL -Headers $HTTPHeaders
}

The resulting output for each user coming from Google is this:

{
    "first_name":  "Api",
    "last_name":  "test 5",
    "primary_email":  "[email protected]",
    "secondary_emails":  "[email protected]",
    "time_zone":  "Pacific Time (US u0026 Canada)",
    "language":  "en"
}

I need the resulting output to be like this as the Freshservice API requires the secondary_emails attribute to be an array of strings (even though I'm only populating it with one. Posting the block below works fine to create a requester up at Freshservice:

{
    "first_name":  "Api",
    "last_name":  "Test5",
    "primary_email":  "[email protected]",
    "secondary_emails":  [
                             "[email protected]"
                         ],
    "time_zone":  "Pacific Time (US u0026 Canada)",
    "language":  "en"
}

https://api.freshservice.com/v2/#requesters

I'm willing to rewrite the whole section that grabs users if there's a better way to do all this. I'm also willing to pipe the body directly into invoke-restmethod if anyone has any ideas how to do that. Couldn't get that to work either.

Thanks!

question from:https://stackoverflow.com/questions/65949394/getting-single-element-returned-from-select-object-into-a-json-array-of-strings

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Rewrote the foreach section like this and it's all good:

foreach ($user in $users) {
    $body = @{'first_name'       = $user.Name.givenname                                 ;
              'last_name'        = $user.Name.familyname                                ;
              'primary_email'    = $user.primaryemail                                   ;
              'secondary_emails' = @($user.primaryemail.split("@")[0] + '@domain.org')  ;
              'time_zone'        = 'Pacific Time (US & Canada)'                         ;
              'language'         = 'en'
            } | ConvertTo-Json

Thanks iRon


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...