Function outputs a different object type

Welcome Forums General PowerShell Q&A Function outputs a different object type

Viewing 5 reply threads
  • Author
    Posts
    • #212961
      Participant
      Topics: 1
      Replies: 2
      Points: -1
      Rank: Member

      I have a function that creates an authentication header for OAuth 2 authentication. The header itself is quite simple: it just has one key pair.

      Inside the function, the header is a hashtable. When the hashtable is read by the calling expression, it has become an object. By converting them both to JSON I can see that an empty string has been inserted before the array, and so the object has become an array object.

      You can see this clearly in the output:

      [
      "",
      {
      "Authorization": "BearerĀ  xyzabc123"
      }
      ]

      Inside the function, the object just has the part inside the {} and is a hashtable. How did it get the extra “”?

      As an extra problem, this happens if I request a token interactively. In EXACTLY the same function, if I request the token non-interactively, the object is returned as a hashtable and the receiving expression works fine.

    • #215169
      Senior Moderator
      Topics: 8
      Replies: 1215
      Points: 4,335
      Helping Hand
      Rank: Community Hero

      seems like there is an empty input which makes the hashtable an array with first input as empty and second one the token detail.

    • #215202
      Participant
      Topics: 1
      Replies: 2
      Points: -1
      Rank: Member

      That’s exactly right. I had a Read-Host "Press Enter", which created an empty string in the pipeline.

    • #215523
      Senior Moderator
      Topics: 8
      Replies: 1215
      Points: 4,335
      Helping Hand
      Rank: Community Hero

      Does that solve your problem ? Can I close this thread ?

    • #215544
      Participant
      Topics: 12
      Replies: 523
      Points: 1,214
      Helping Hand
      Rank: Community Hero

      @AirPowerShell98 please share the code portion that creates this JSON

    • #215583
      Participant
      Topics: 1
      Replies: 2
      Points: -1
      Rank: Member

      Yes, please close. It was the “Read-Host” that created a blank string in the pipeline. I added | Out-Null and everything works as expected.

Viewing 5 reply threads
  • You must be logged in to reply to this topic.