-
-
Save mdjx/0153ae2b69f87adcacb8c677c4c4a5a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
THIS IS AN EXAMPLE | |
#> | |
# Return WAN IP | |
function WANIP { | |
Invoke-RestMethod http://ifconfig.me/ip | |
} | |
# Custom Get-Help | |
function Get-Help { | |
if ($args) { | |
start "https://www.google.com/search?q=$args" | |
} else { | |
start "https://www.google.com/" | |
} | |
} | |
<# | |
# Profile deployment script | |
-- Start Copy -- | |
# Define Gist Id which contains our profile script | |
$ProfileGistID = '0153ae2b69f87adcacb8c677c4c4a5a6' | |
function VerifyRemoteProfile { | |
Param( | |
[Parameter(Mandatory=$true)] | |
[psobject]$Gist, | |
[Parameter(Mandatory=$true)] | |
[string]$PSCachedProfile | |
) | |
# Define possible profile loading options | |
Add-Type -TypeDefinition @" | |
public enum ProfileLoadOption { | |
LoadCached, | |
LoadRemote | |
} | |
"@ | |
# Set default to load locally cached profile | |
$LoadProfile = [ProfileLoadOption]::LoadCached | |
$PSRemoteProfileVersionFile = ([System.IO.FileInfo]$profile).DirectoryName + "\PSRemoteProfileVersions.json" | |
# Load current profile versions or create new instance if none are present | |
try { | |
$VersionData = Get-Content $PSRemoteProfileVersionFile -Raw -ErrorAction Stop | ConvertFrom-Json | |
$NewFile = $false | |
} | |
catch { | |
Write-Host "No profile version file found, creating..." | |
$VersionData = New-Object -TypeName PSObject -Property @{ | |
LastModified = Get-Date -Year 1900 -Format u | |
LastCommitHash = "None" | |
} | |
$NewFile = $true | |
} | |
# Loading Gist data | |
$LastModified = $Gist.updated_at | |
$LastCommitHash = $Gist.history[0].version | |
# Request approval if remote profile has changed, otherwise load cached version | |
if (($VersionData.LastModified -ne $LastModified) -or ($VersionData.LastCommitHash -ne $LastCommitHash)) { | |
Write-Host "-----------------------------------------" | |
Write-Host "Local Last Modified timestamp is $(([datetime]$VersionData.LastModified).ToLocalTime().ToString()), remote is $(([datetime]$LastModified).ToLocalTime().ToString())" | |
Write-Host "Local Commit Hash is $($VersionData.LastCommitHash), remote is $LastCommitHash" | |
Write-Host "-----------------------------------------" | |
# Show diff if $NewFile is False | |
if ($NewFile -eq $false) { | |
$CurrentProfile = Get-Content $PSCachedProfile | |
$NewProfile = $Gist.files.PSProfile.content.Split([Environment]::NewLine) | |
Write-Host "[+] Added Lines" | |
$NewProfile | % { if ($_ -notin $CurrentProfile) {Write-Host $_ -ForegroundColor Green}} | |
Write-Host "[+] Removed Lines" | |
$CurrentProfile | % { if ($_ -notin $NewProfile) {Write-Host $_ -ForegroundColor Red}} | |
} | |
# Present options to accept or reject changed profile | |
$Deny = New-Object System.Management.Automation.Host.ChoiceDescription '&Deny','Do not allow loading of the new profile' | |
$Allow = New-Object System.Management.Automation.Host.ChoiceDescription '&Allow','Allow loading of the new profile' | |
$Choices = [System.Management.Automation.Host.ChoiceDescription[]]($Deny,$Allow) | |
$Prompt = 'Do you wish to allow loading the changed profile?' | |
$Result = $Host.UI.PromptForChoice($null, $Prompt, $Choices, 0) | |
if ($Result -eq 1) { | |
$LoadProfile = [ProfileLoadOption]::LoadRemote | |
# Upading local version file | |
$VersionData.LastModified = $LastModified | |
$VersionData.LastCommitHash = $LastCommitHash | |
$VersionData | ConvertTo-Json | Out-File $PSRemoteProfileVersionFile -Force | |
# Upading cached profile | |
$Gist.files.PSProfile.content | Out-File $PSCachedProfile -Force | |
} | |
else { | |
Write-Host "Loading remote profile rejected, falling back to locally cached version" | |
$LoadProfile = [ProfileLoadOption]::LoadCached | |
} | |
} else { | |
$LoadProfile = [ProfileLoadOption]::LoadCached | |
} | |
Write-Output $LoadProfile | |
} | |
$PSCachedProfile = ([System.IO.FileInfo]$profile).DirectoryName + "\PSCachedPofile.ps1" | |
try { | |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 | |
$Gist = Invoke-RestMethod https://api.github.com/gists/$ProfileGistID | |
$LoadProfile = VerifyRemoteProfile -Gist $Gist -PSCachedProfile $PSCachedProfile | |
# Load remote profile | |
if ($LoadProfile -eq "LoadRemote") { | |
Write-Host "Loading remote profile" -ForegroundColor Green | |
Invoke-Expression ($Gist).files.PSProfile.content | |
} | |
# Load cached profile | |
if ($LoadProfile -eq "LoadCached") { | |
. $PSCachedProfile | |
} | |
} catch { | |
# Load cached version in the event of an error | |
if (!(Test-Path $PSCachedProfile)) { | |
Write-Warning "Locally cached copy of remote profile not found, expected at $PSCachedProfile" | |
} else { | |
. $PSCachedProfile | |
} | |
} | |
# Clean up after ourselves | |
Remove-Variable Gist, LoadProfile, ProfileGistID, PSCachedProfile -ErrorAction SilentlyContinue | |
-- End Copy -- | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much for sharing your great work with Blog & Code and your great analyses, explanations, and concepts. And your scripts are remarkably elegantly programmed - always a great learning lesson to study them :-)