GitHubToolsBD.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
function Get-FilesFromRepo {
  <#
  .SYNOPSIS
    This downloads files from a GitHub Repository
  .DESCRIPTION
    This command will copy files from a GitHub Repo and store them in a
    folder of your choice.
  .EXAMPLE
    Get-FilesFromRepo -GitHubUserName BOB -Repository Webproject -Destination c:\RepoCopy
    This gets the files in the webproject repo from the GitHub user BOB and copies them
    to the the c:\RepoCopy folder.
  .EXAMPLE
    Get-FilesFromRepo -GitHubUserName BOB -Destination c:\RepoCopy
    This will present the user with a menu of Repositories for the GitHub user BOB and
    once a repo is selected it will copy file in the repo to the the c:\RepoCopy folder.
  .PARAMETER GitHubUserName
    This is the GitHub user site that will be used to copy the files from
  .PARAMETER Repository
    This is the users repository that willl be used to copy the files from, if this
    is not specified a menu will show asking to choose the repo from a list.
  .PARAMETER PathInRepo
    If the repo has sub directories you can specify the one you want
  .PARAMETER Destination
    This is the path that will be used to save the files on the local machine
  .PARAMETER FilesToRetrieve
    This a list of the files to retrieve, if this is left blank then all of the
    files will be downloaded to the destination
  .PARAMETER ViewFiles
    This will display the files that can be retrieved only and will not download any of the files
  .NOTES
    General notes
      Written By: Brent Denny
      Written on: 28 Aug 2019
      Modified: 06 Feb 2022
  #>

  [cmdletbinding()]
  Param(
    [Parameter(Mandatory=$true)]
    [string]$GitHubUserName,
    [string]$Repository,
    [string]$PathInRepo = '',
    [string]$Destination = "$env:TEMP\Git\$GitHubUserName",
    [string[]]$FilesToRetrieve = @(),
    [switch]$ViewFiles
  )
  [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  if ($Repository -eq '') {
    $AllRepos = (Invoke-RestMethod -Uri "https://api.github.com/users/$GitHubUserName/repos").URL
    $RepoNames = Split-Path -Path $AllRepos -Leaf 
    Write-Host 'Which Repo'
    $count = 0
    Foreach ($Repo in $RepoNames) {
      $count++
      Write-Host "$count - $Repo"
    }
    do {
      $GoodChoice = $true
      $choice = Read-Host -Prompt "Choose which repo"
      try {
        0 + $choice > $null
        $choice = $choice -as [int]
        if ($choice -gt $RepoNames.Count -or $choice -lt 1) {throw}
      }
      catch {
        $GoodChoice = $false
        continue
      }
      $choice = $choice - 1
      $Repository = $RepoNames[$choice]
    } while ($GoodChoice -eq $false)
  } #END if
  $URI = "https://api.github.com/repos/$GitHubUserName/$Repository/contents/$PathInRepo"
  $WebData = Invoke-WebRequest -Uri $($URI)
  $WebContent = $WebData.Content | ConvertFrom-Json
  $AllDownloadURLS = ($WebContent | Where-Object {$_.type -eq "file"}).download_url 
  $AllFilesInRepo = $AllDownloadURLS | ForEach-Object {Split-Path $_ -Leaf} 
  if ($FilesToRetrieve.Count -eq 0) {$FilesToRetrieve = $AllFilesInRepo}
  if ($ViewFiles -eq $true) {
    $AllFilesInRepo -replace '\%20',' '
    break
  }
  if ((Test-Path $Destination) -eq $false) {
    try {New-Item -Path $Destination -ItemType Directory -ErrorAction Stop > $null} 
    catch {
      Write-Warning "Could not create path '$Destination'!"
      break
    }
  } #END if
  foreach ($DownloadURL in $AllDownloadURLS) {
    $DownloadURLFile = Split-Path $DownloadURL -Leaf
    if ($DownloadURLFile -in ($FilesToRetrieve -replace '\s','%20')) {
      $DestinationFilePath = Join-Path $Destination ($DownloadURLFile -replace '\%20',' ')
      try {Invoke-WebRequest -Uri $DownloadURL -OutFile $DestinationFilePath -ErrorAction Stop -Verbose}
      catch {Write-Warning "Unable to download '$($DownloadURL.path)'"}
    }
  } #END foreach
} #END function