Wilson Mar bio photo

Wilson Mar

Hello. Hire me!

Email me Calendar Skype call 310 320-7878

LinkedIn Twitter Gitter Google+ 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: