Wilson Mar bio photo

Wilson Mar

Hello. Hire me!

Email me Calendar Skype call 310 320-7878

LinkedIn Twitter Gitter Google+ Instagram Youtube

Github Stackoverflow Pinterest

GitHub reaching to web servies on the web gives scripts data power


Overview

I wanted to make calls to GitHub’s API from a PowerShell script.

While looking for examples, I found several libraries that already do it:

Do you know of others? Please let me know.


Use PowerShellForGitHub module

  1. The module exists within several other PS modules (in a PowerShell CLI Terminal window):

    
    (Find-Module).count
    $response = Find-Module
    $response[0] | format-list # details for first item
    Set-PSRepository -Name PSGallery -InstallationPolicy Trusted 
    

    (Find-Module).count returned 1084 on November 4, 2016. So list commands are not appropriate:

    $response | Sort-Object
    $response.GetEnumerator() | Sort-Object Value -descending
    
  2. List the many fields in metadata for module PowerShellForGitHub:

    $response.GetEnumerator() | ?{ $_.Name -eq "PowerShellForGitHub" } | format-list
    

    The response:

    Name                       : PowerShellForGitHub
    Version                    : 0.1.0
    Type                       : Module
    Description                : PowerShell wrapper for GitHub API
    Author                     : Microsoft Corporation
    CompanyName                : PowerShellTeam
    Copyright                  : (c) 2016 Microsoft Corporation. All rights 
                              reserved.
    PublishedDate              : 7/27/16 8:33:44 PM
    InstalledDate              : 
    UpdatedDate                : 
    LicenseUri                 : https://github.com/PowerShell/PowerShellForGitHub/
                              blob/master/LICENSE
    ProjectUri                 : https://github.com/PowerShell/PowerShellForGitHub
    IconUri                    : 
    Tags                       : {GitHub, API, PowerShell, PSModule}
    Includes                   : {Cmdlet, Workflow, Function, DscResource...}
    PowerShellGetFormatVersion : 
    ReleaseNotes               : 
    Dependencies               : {}
    RepositorySourceLocation   : https://www.powershellgallery.com/api/v2/
    Repository                 : PSGallery
    PackageManagementProvider  : NuGet
    AdditionalMetadata         : {developmentDependency, FileList, 
                              isLatestVersion, IsPrerelease...}
    
  3. This command is necessary to avoid errors when modules are installed:

    Set-PSRepository -Name PSGallery -InstallationPolicy Trusted

    If it worked, no text is returned, just the PowerShell prompt again.

  4. The GitHub README says to rename ApiTokensTemplate.psm1 to ApiTokens.psm1 and update value of $global:gitHubApiToken with GitHub token for your account.

    QUESTION: how do I do that before having the file???

  5. Install the latest module in PS Gallery within the PowerShell CLI:

    Install-Module -Name PowerShellForGitHub

    If Set-PSRepoitory was not run before this, the following would appear:

    Untrusted repository
    You are installing the modules from an untrusted repository. If you trust this 
    repository, change its InstallationPolicy value by running the Set-PSRepository
     cmdlet. Are you sure you want to install the modules from 'PSGallery'?
    [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help 
    (default is "N"):
    

    Rerun after Set-PSRepository:

    BLAH: The response I got is aother error, but the message showed me where the file is installed on my Mac:

    PackageManagement\Install-Package : Could not find a part of the path 
    '/usr/local/microsoft/powershell/6.0.0-alpha.10/Modules/PowerShellForGitHub/0.1.0'.
    Installing package 'PowerShellForGitHub'                                  
    At /usr/local/microsoft/powershell/6.0.0-alpha.10/Modules/PowerShellGet/PSModul e.psm1:1711 char:21                                                             + ...          $null = PackageManagement\Install-Package @PSBoundParameters
           +                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           + CategoryInfo          : NotSpecified: (Microsoft.Power....InstallPackage 
    :InstallPackage) [Install-Package], Exception
           + FullyQualifiedErrorId : System.IO.DirectoryNotFoundException,Microsoft.PowerShell.Commands.CopyItemCommand,Microsoft.PowerShell.PackageManagement.  
      Cmdlets.InstallPackage
    

    Looking into the folder, there are a lot of .dll files. So it’s not applicable to me. Thus, I …

  6. Uninstall

    Uninstall-Module PowerShellForGitHub

    BLAH: This is the error message I received:

    PackageManagement\Uninstall-Package : No match was found for the specified 
    search criteria and module names 'PowerShellForGitHub'.
    At /usr/local/microsoft/powershell/6.0.0-alpha.10/Modules/PowerShellGet/PSModul
    e.psm1:2096 char:21
           + ...        $null = PackageManagement\Uninstall-Package @PSBoundParameters
           +                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : ObjectNotFound: (Microsoft.Power...ninstallPacka 
    ge:UninstallPackage) [Uninstall-Package], Exception
     + FullyQualifiedErrorId : NoMatchFound,Microsoft.PowerShell.PackageManagem 
    ent.Cmdlets.UninstallPackage
    
  7. Since I was running a Mac, I got rid of it and went without using a library.

More on API Microservices

This is one of a series:

More about Git & GitHub

This is one of a series on Git and GitHub:

  1. Git and GitHub videos

  2. Why Git? (file-based backups vs Git clone)
  3. Git Markdown text
  4. Git basics (script)
  5. Git command shortcuts

  6. Git-client based workflows
  7. Git whoops (correct mistakes)
  8. Git rebase
  9. Git interactive merge (imerge)
  10. Git HEAD (Commitish references)
  11. Git commits with a Tag and Signature

  12. Git custom commands
  13. Git utilities
  14. Git hooks

  15. GitHub data security

  16. TFS vs GitHub
  17. GitHub REST API
  18. GitHub GraphQL API
  19. GitHub PowerShell API Programming
  20. GitHub GraphQL PowerShell Module