Created
          December 18, 2024 09:12 
        
      - 
      
- 
        Save 1ARdotNO/804016848cd84188282e64d40b8259a2 to your computer and use it in GitHub Desktop. 
    Script to use graphql api to fetch a list of emails and usernames from github, 
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | param ( | |
| [Parameter(Mandatory = $true)] $orgName | |
| ) | |
| $pat = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($ENV:GITHUB_TOKEN)")) | |
| $query="{ | |
| organization(login: `"$orgName`") { | |
| samlIdentityProvider { | |
| externalIdentities(first: 100) { | |
| nodes { | |
| user { | |
| login | |
| name | |
| } | |
| samlIdentity { | |
| attributes { | |
| name | |
| value | |
| } | |
| emails { | |
| primary | |
| type | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| }" | |
| $params = @{ | |
| 'Uri' = ('https://api.github.com/graphql') | |
| 'Headers' = @{'Authorization' = 'Basic ' + $pat } | |
| 'Method' = 'post' | |
| 'ContentType' = 'application/json' | |
| 'Body' = ( @{query=$query} | ConvertTo-Json ) | |
| } | |
| #add environments | |
| $result=Invoke-RestMethod @params | |
| $result.data.organization.samlIdentityProvider.externalIdentities.nodes | ForEach-Object { | |
| [pscustomobject]@{ | |
| name = ($_.samlidentity.attributes | where {$_.name -like "*displayname*"}).value | |
| username = $_.user.login | |
| email = ($_.samlidentity.attributes | where {$_.name -like "*emailaddress*"}).value | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment