none
Combining results of Invoke-RestMethod (PSCustomObjects) RRS feed

  • Question

  • I have a script that makes three separate calls to CloudFlare's API and puts the results into separate variables.  Unfortunately, I cannot figure out a way to combine these variables into a single variable.

      $dmarc = Invoke-RestMethod -Method GET "https://api.cloudflare.com/client/v4/zones/$id/dns_records?per_page=100&name=_dmarc.$name" `
          -ContentType "application/json" `
          -Headers $headers | Select -ExpandProperty Result | Select Zone_Name,Name,Content
      $spf = Invoke-RestMethod -Method GET "https://api.cloudflare.com/client/v4/zones/$id/dns_records?per_page=100&name=$name&type=TXT" `
          -ContentType "application/json" `
          -Headers $headers | Select -ExpandProperty Result | Select Zone_Name,Name,Content
      $dkim = Invoke-RestMethod -Method GET "https://api.cloudflare.com/client/v4/zones/$id/dns_records?per_page=100&name=stlouisco._domainkey.$name" `
          -ContentType "application/json" `
          -Headers $headers | Select -ExpandProperty Result | Select Zone_Name,Name,Content

    Running Get-Member shows the below:

       TypeName: Selected.System.Management.Automation.PSCustomObject
    
    Name        MemberType   Definition
    ----        ----------   ----------
    Equals      Method       bool Equals(System.Object obj)
    GetHashCode Method       int GetHashCode()
    GetType     Method       type GetType()
    ToString    Method       string ToString()
    content     NoteProperty string content=v=spf1 
    name        NoteProperty string name=.com
    zone_name   NoteProperty string zone_name=.com

    I've tried the following with similar error results

    $result += $dmarc+$spf+$dkim
    $result = $dmarc+$spf+$dkim
    $result.add($dmarc+$spf+$dkim)

    The error for all three is 

    Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

    I'm confident that the answer is something simple....I just can't figure out what it is I am doing wrong.  

    Wednesday, September 5, 2018 6:27 PM

Answers

  • Why not just


    $result = $dmarc,$spf,$dkim

    ?


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by WWalker0307 Wednesday, September 5, 2018 6:58 PM
    Wednesday, September 5, 2018 6:35 PM
    Moderator

All replies

  • Why not just


    $result = $dmarc,$spf,$dkim

    ?


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by WWalker0307 Wednesday, September 5, 2018 6:58 PM
    Wednesday, September 5, 2018 6:35 PM
    Moderator
  • This is how to combine all results into one object:

    $splat = @{
        ContentType = 'application/json'
        Headers = $headers
        Method = 'GET' 
    }
    $dmarc = Invoke-RestMethod @splat -Uri "https://api.cloudflare.com/client/v4/zones/$id/dns_records?per_page=100&name=_dmarc.$name"
    $spf = Invoke-RestMethod @splat -Uri "https://api.cloudflare.com/client/v4/zones/$id/dns_records?per_page=100&name=$name&type=TXT"
    $dkim = Invoke-RestMethod @splat -Uri "https://api.cloudflare.com/client/v4/zones/$id/dns_records?per_page=100&name=stlouisco._domainkey.$name"
    
    [pscustomobject]@{
        DmarcZoneName = $dmarc.zone_name
        DmarcName = $dmarc.Name
        DmarcContent = $dmarc.Content
        SPFZoneName = $spf.zone_name
        SPFName = $spf.Name
        SPFContent = $spf.Content
        DkimZoneName = $dkim.zone_name
        DkimName = $dkim.Name
        DkimContent = $dkim.Content
    }
    


    \_(ツ)_/

    Wednesday, September 5, 2018 6:45 PM
  • Because I didn't think to try that, lol.

    I also should have elaborated better in my question.  This all sits inside a foreach that loops through all our domains.  Using your method worked for me.


    • Edited by WWalker0307 Wednesday, September 5, 2018 7:09 PM
    Wednesday, September 5, 2018 6:58 PM
  • I'm not as advanced in using PS as you are, I like how you put everything in the splat var to clean things up.  I tried this out, the problem I came across, as I mentioned in my reply to Bill_Stewart, this is all wrapped inside a foreach that loops through all the domains we have in CF.  I modified it to [pscustomobject]$results += { and it threw the same error at me as I mention in the original post.
    Wednesday, September 5, 2018 7:08 PM