Skip to content

Instantly share code, notes, and snippets.

@Stephanevg
Last active November 12, 2022 10:45
Show Gist options
  • Save Stephanevg/afc652029b5a7bb4ca37bb1429cb3363 to your computer and use it in GitHub Desktop.
Save Stephanevg/afc652029b5a7bb4ca37bb1429cb3363 to your computer and use it in GitHub Desktop.
# Version 1.2.12
# check if newer version
$gistUrl = "https://api.github.com/gists/a208d2bd924691bae7ec7904cab0bd8e"
$latestVersionFile = [System.IO.Path]::Combine("$HOME",'.latest_profile_version')
$versionRegEx = "# Version (?<version>\d+\.\d+\.\d+)"
if ([System.IO.File]::Exists($latestVersionFile)) {
$latestVersion = [System.IO.File]::ReadAllText($latestVersionFile)
$currentProfile = [System.IO.File]::ReadAllText($profile)
[version]$currentVersion = "0.0.0"
if ($currentProfile -match $versionRegEx) {
$currentVersion = $matches.Version
}
if ([version]$latestVersion -gt $currentVersion) {
Write-Verbose "Your version: $currentVersion" -Verbose
Write-Verbose "New version: $latestVersion" -Verbose
$choice = Read-Host -Prompt "Found newer profile, install? (Y)"
if ($choice -eq "Y" -or $choice -eq "") {
try {
$gist = Invoke-RestMethod $gistUrl -ErrorAction Stop
$gistProfile = $gist.Files."profile.ps1".Content
Set-Content -Path $profile -Value $gistProfile
Write-Verbose "Installed newer version of profile" -Verbose
. $profile
return
}
catch {
# we can hit rate limit issue with GitHub since we're using anonymous
Write-Verbose -Verbose "Was not able to access gist, try again next time"
}
}
}
}
Function Initialize-Nuget{
if(!(get-PackagePRovider -Name Nuget -ErrorAction SilentlyContinue) ){
install-packageProvider -name nuget -minimumversion 2.8.5.201 -force
}
}
Function Install-MyModules {
$Modules = @("ComputerManagementDSC","PSHTML","PSClassUtils","PsGraph","PsReadLine","cChoco","powershell-yaml","ImportExcel")
Set-PSRepository -Name PsGallery -InstallationPolicy Trusted
foreach($mod in $modules){
Install-Module -Name $Mod -Force
}
install-module pester -Force -SkipPublisherCheck
}
Function Ensure-Chocolatey{
if(!Get-Command -Name choco.exe -ErrorAction SilentlyContinue){
write-verbose "Chocolatey not found. Downloading and installing."
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
}else{
write-verbose "Chocolatey detected!"
}
}
function Initialize-Profile {
$null = Start-ThreadJob -Name "Get version of `$profile from gist" -ArgumentList $gistUrl, $latestVersionFile, $versionRegEx -ScriptBlock {
param ($gistUrl, $latestVersionFile, $versionRegEx)
try {
$gist = Invoke-RestMethod $gistUrl -ErrorAction Stop
$gistProfile = $gist.Files."profile.ps1".Content
[version]$gistVersion = "0.0.0"
if ($gistProfile -match $versionRegEx) {
$gistVersion = $matches.Version
Set-Content -Path $latestVersionFile -Value $gistVersion
}
}
catch {
# we can hit rate limit issue with GitHub since we're using anonymous
Write-Verbose -Verbose "Was not able to access gist to check for newer version"
}
}
if ((Get-Module PSReadLine).Version -lt 2.2) {
throw "Profile requires PSReadLine 2.2+"
}
# setup psdrives
if ([System.IO.File]::Exists([System.IO.Path]::Combine("$HOME",'Test'))) {
New-PSDrive -Root ~/test -Name Test -PSProvider FileSystem -ErrorAction Ignore > $Null
}
$Code = [System.IO.Path]::Combine("$HOME",'Code')
if (![System.IO.File]::Exists($Code)) {
mkdir $Code -Force > $null
}
if ([System.IO.File]::Exists($Code)) {
New-PSDrive -Root ~/Code -Name Code -PSProvider FileSystem -ErrorAction Ignore > $Null
}
# add path to dotnet global tools
$env:PATH += [System.IO.Path]::PathSeparator + [System.IO.Path]::Combine("$HOME",'.dotnet','tools')
# ensure dotnet cli is in path
$dotnet = Get-Command dotnet -CommandType Application -ErrorAction Ignore
if ($null -eq $dotnet) {
if ([System.IO.File]::Exists("$HOME/.dotnet/dotnet")){
$env:PATH += [System.IO.Path]::PathSeparator+ [System.IO.Path]::Combine("$HOME",'.dotnet')
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment