Skip to content

Instantly share code, notes, and snippets.

@Alex-Yates
Last active July 24, 2020 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alex-Yates/46da822dd788672eb3b990fd3e08474b to your computer and use it in GitHub Desktop.
Save Alex-Yates/46da822dd788672eb3b990fd3e08474b to your computer and use it in GitHub Desktop.
Function Get-Script{
param (
[Parameter(Mandatory=$true)][string]$script, # the name of the file (needs to be in repo root, otherwise tweak this function)
[string]$owner = "my-username", # repo owner (github username)
[string]$repo = "my-repo", # name of repo
[string]$branch = "master",
[string]$outputDir = ""
)
# the raw page for the script
$uri = "https://raw.githubusercontent.com/$owner/$repo/$branch/$script"
# choosing a default output directory if none specified
if ($outputDir -like "") {
$currentDir = Get-Location
$outputDir = "$currentDir\$repo"
}
# ensuring the output directory exists
if ((test-path $outputDir) -ne $true) {
Write-Output " Creating directory $outputDir"
New-Item -ItemType "Directory" -Path $outputDir | Out-Null
}
# logging
Write-Output "Downloading $script"
Write-Output " from: $uri"
Write-Output " to: $outputDir"
# reading the page and saving to file
Invoke-WebRequest -Uri $uri -OutFile $outFile -Verbose
}
# Example execution:
# Get-Script -script "my-script.ps1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment