Skip to content

Instantly share code, notes, and snippets.

@GABeech
Created June 22, 2018 19:10
Show Gist options
  • Save GABeech/98df2f95fb3a79cd2ccaa80a439aa975 to your computer and use it in GitHub Desktop.
Save GABeech/98df2f95fb3a79cd2ccaa80a439aa975 to your computer and use it in GitHub Desktop.
My Powershell Profile
function Clear-DeletedBranches
{
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
param(
[Parameter(Mandatory = $false)]
[string]
$GitDir = $PWD
)
$before = git branch -a
$prun = git remote prune origin --dry-run
if($prun)
{
Write-Host "Branches to Be Pruned" -ForegroundColor Green
Write-Host $prun -ForegroundColor Red
if($PSCmdlet.ShouldProcess("Remove Local Branches?"))
{
git remote prune origin
$after = git branch -a
$removed = Compare-Object -ReferenceObject $before -DifferenceObject $after
foreach($b in $removed.InputObject)
{
$localName = $b.Split('/')[-1]
git branch -D $localName
}
}
}
else
{
Write-Host "Nothing To prune" -ForegroundColor Green
}
}
# Put the SSH_AGENT_PD and SSH_AUTH_SOCK in the User Env Variables
# This way we don't need to worry about having to be int he right process for
# SSH-AGENT to work
function Set-SSHAgent
{
$SSHPID = [System.Environment]::GetEnvironmentVariable("SSH_AGENT_PID", "Process")
$SSHSOCK = [System.Environment]::GetEnvironmentVariable("SSH_AUTH_SOCK", "Process")
$USSHPID = [System.Environment]::GetEnvironmentVariable("SSH_AGENT_PID", "User")
$USSHSOCK = [System.Environment]::GetEnvironmentVariable("SSH_AUTH_SOCK", "User")
if(($USSHPID -ne $SSHPID) -or ($USSHSOCK -ne $SSHSOCK))
{
[Environment]::SetEnvironmentVariable('SSH_AGENT_PID', $SSHPID, 'User')
[Environment]::SetEnvironmentVariable('SSH_AUTH_SOCK', $SSHSOCK, 'User')
}
}
$profileLoadStart = Get-Date
$poshStart = measure-command { Import-Module posh-git }
$poshLoadTime = "Posh-git Loaded [{0:g}]" -f $poshStart
write-output $poshLoadTime
# Chocolatey profile
$ChocolateyStart = Measure-Command {
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile))
{
Import-Module "$ChocolateyProfile"
}
}
$ChocoLoadString = "Chocolatey Loaded: [{0:g}]" -f $ChocolateyStart
Write-Output $ChocoLoadString
# Start SSH Agent on new session
# NOOP if it is already running, so lets just be sure.
Start-SSHAgent
# Check to see if my keys are already loaded
$sshDirectory = "C:\Users\George.Beech\.ssh"
$privateKeys = Get-ChildItem -Path $sshDirectory | Where-Object { !$_.Extension -and $_ -like "id_*"}
$loadedKeys = ssh-add.exe -l
foreach($key in $privateKeys)
{
$keyInfo = ssh-keygen.exe -lf $key.FullName
if($loadedKeys.Contains($keyInfo))
{
continue
}
ssh-add.exe $key.FullName
}
Set-SSHAgent
$profileLoadEnd = Get-Date
$profileLoadTime = $profileLoadEnd - $profileLoadStart
$loadTimeString = "Profile Loaded [{0:g}]" -f $profileLoadTime
Write-Output $loadTimeString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment