Wilson Mar bio photo

Wilson Mar

Hello!

Calendar YouTube Github

LinkedIn

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

US (English)   Norsk (Norwegian)   Español (Spanish)   Français (French)   Deutsch (German)   Italiano   Português   Estonian   اَلْعَرَبِيَّةُ (Egypt Arabic)   Napali   中文 (简体) Chinese (Simplified)   日本語 Japanese   한국어 Korean

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 whoops (correct mistakes)
  6. Git messages (in commits)

  7. Git command shortcuts
  8. Git custom commands

  9. Git-client based workflows

  10. Git HEAD (Commitish references)

  11. Git interactive merge (imerge)
  12. Git patch
  13. Git rebase

  14. Git utilities
  15. Git-signing

  16. Git hooks
  17. GitHub data security
  18. TFS vs GitHub

  19. GitHub actions for automation JavaScript
  20. GitHub REST API
  21. GitHub GraphQL API
  22. GitHub PowerShell API Programming
  23. GitHub GraphQL PowerShell Module