Skip to content

Commit

Permalink
Add some powershell modules and script to install profile
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgwagner committed Sep 7, 2014
1 parent ec35365 commit 2fef56e
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 0 deletions.
20 changes: 20 additions & 0 deletions PowerShellProfile/Git.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
$ScriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition

function Init-GitSubmodules()
{
git submodule update --init --recursive
}

function Get-LatestCode()
{
git stash
git pull --rebase origin
git stash pop
}
Set-Alias gitp Get-LatestCode -Scope Global

$PoshGitPath = (Join-Path $ScriptPath "posh-git")

Import-Module $PoshGitPath -Scope Global

& "$PoshGitPath\profile.example.ps1"
15 changes: 15 additions & 0 deletions PowerShellProfile/Install-Profile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
###
### Setup a PowerShell profile that will look at our loader for modules
###

$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest

$ScriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition

if (!(test-path $profile))
{
New-Item -path $profile -type file -force
}

Add-Content $profile ". (Resolve-Path '$ScriptPath\Microsoft.PowerShell_profile.ps1')"
140 changes: 140 additions & 0 deletions PowerShellProfile/Microsoft.PowerShell_profile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
## Do things that should be loaded on PS start here

$ScriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition

function global:Import($Module)
{
Import-Module (Join-Path $ScriptPath $Module) -DisableNameChecking
}

Import Git
Import Redis

## function to launch enterprise email in IE

Set-Alias msbuild "$(Get-Content ENV:WINDIR)\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe" -Scope global

function Install-Chocolatey()
{
if(-not $env:ChocolateyInstall -or -not (Test-Path "$env:ChocolateyInstall"))
{
Write-Host "Chocolatey Not Found, Installing..."
iex ((new-object net.webclient).DownloadString('http://chocolatey.org/install.ps1'))
}
}

function Open-IE ($url)
{
$ie = new-object -comobject internetexplorer.application;

$ie.Navigate($url);

$ie.Visible = $true;
}

function RemoteDesktop($server)
{
mstsc /v:"$server"
}

## "%ProgramFiles%\Internet Explorer\iexplore.exe"

## "%ProgramFiles(x86)%\Mozilla Firefox\firefox.exe"

## "%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe"

## Try to find an app in x86 or the 'normal' ProgramFiles
function Find-Program($folderName)
{
$p1 = "C:\Program Files\$folderName"

if(!(test-path $p1))
{
$p2 = "C:\Program Files (x86)\$folderName"

if((test-path $p2))
{
$p2
}
}
else
{
$p1
}
}

function xsd()
{
& (Find-Program "Microsoft SDKs\Windows\v7.0A\Bin\xsd.exe") $args
}

function touch($file)
{
if(test-path $file) {
$f = get-item $file;
$d = get-date
$f.LastWriteTime = $d
}
else
{
"" | out-file -FilePath $file -Encoding ASCII
}
}

function sha1()
{
<#
Thank you Brad Wilson
http://bradwilson.typepad.com/blog/2010/03/calculating-sha1-in-powershell.html
#>

[Reflection.Assembly]::LoadWithPartialName("System.Security") | out-null
$sha1 = new-Object System.Security.Cryptography.SHA1Managed

$args | %{
resolve-path $_ | %{
write-host ([System.IO.Path]::GetFilename($_.Path))

$file = [System.IO.File]::Open($_.Path, "open", "read")
$sha1.ComputeHash($file) | %{
write-host -nonewline $_.ToString("x2")
}
$file.Dispose()

write-host
write-host
}
}
}

function Confirm-IsAdmin()
{
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Warning "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!"
return $false
}

return $true
}

function Watch-File($filename, $lines = 5)
{
Get-Content -Path $filename -Tail $lines -Wait
}
Set-Alias tail Watch-File -Scope Global

function Load-Assembly($assembly)
{
echo $assembly

if(test-path $assembly)
{
$assemblyPath = get-item $assembly
[System.Reflection.Assembly]::LoadFrom($assemblyPath)
}
else
{
[System.Reflection.Assembly]::LoadWithPartialName("$assembly")
}
}
165 changes: 165 additions & 0 deletions PowerShellProfile/Redis.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
###
### Commands for querying or clearing the Redis cache
###

$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest

$ScriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition

$RedisFolder = (Join-Path "$ScriptPath\.." "Redis")

# Local Redis instance defaults
$_Server = "localhost"
$_Port = 6379
$_Db = 0

<#
.SYNOPSIS
Flush all of the databases from a given Redis cache instance.
#>
function Flush-All($Server=$_Server, $Port=$_Port)
{
ExecuteCommandAgainstRedis $Server $Port $_Db flushall
}

<#
.SYNOPSIS
Flush a single DB in Redis from the cache.
#>
function Flush-Cache($Server=$_Server, $Port=$_Port, $Db=$_Db)
{
ExecuteCommandAgainstRedis $Server $Port $Db flushdb
}

<#
.SYNOPSIS
Starts a local Redis cache instance on your local machine, running on port 6379 (default).
You will still need to update the App/Web.config files to utilize this cache for testing.
#>
function Start-Cache()
{
& "$RedisFolder\Redis-Server.exe" "$RedisFolder\redis.windows.conf"
}

<#
.SYNOPSIS
Installs a Redis cache instance on your local machine, running on port 6379 (default).
#>
function Install-CacheService()
{
& "$RedisFolder\Redis-Server.exe" "--service-install" "$RedisFolder\redis.windows.conf"

& "$RedisFolder\Redis-Server.exe" "--service-start"
}

<#
.SYNOPSIS
Removes the Redis cache instance on your local machine.
#>
function Remove-CacheService()
{
& "$RedisFolder\Redis-Server.exe" "--service-stop"

& "$RedisFolder\Redis-Server.exe" "--service-uninstall"
}

<#
.SYNOPSIS
Starts a Windows GUI for managing Redis cache instances
#>
function Start-CacheManager()
{
#chocolatey install redis-desktop-manager
& "${Env:ProgramFiles(x86)}\RedisDesktopManager\redis-desktop-manager.exe"
}

<#
.SYNOPSIS
Search through an instance of a Redis cache for a given key pattern.
#>
function Search-CacheKeys($KeyPattern, $Server=$_Server, $Port=$_Port, $Db=$_Db)
{
ExecuteCommandAgainstRedis $Server $Port $Db KEYS "$KeyPattern"
}

<#
.SYNOPSIS
Print the general Redis server info.
#>
function Get-CacheInfo($Server=$_Server, $Port=$_Port)
{
ExecuteCommandAgainstRedis $Server $Port $_Db "info"
}

<#
.SYNOPSIS
Get the data stored at a particular key in Redis.
#>
function Get-Key($Key, $Server=$_Server, $Port=$_Port, $Db=$_Db)
{
ExecuteCommandAgainstRedis $Server $Port $Db GET "$Key"
}

<#
.SYNOPSIS
Remove a key from the Redis cache.
#>
function Delete-Key($Key, $Server=$_Server, $Port=$_Port, $Db=$_Db)
{
ExecuteCommandAgainstRedis $Server $Port $Db DEL "$Key"
}

function Get-PubSubChannels($Server=$_Server, $Port=$_Port)
{
ExecuteCommandAgainstRedis $Server $Port $_Db PUBSUB CHANNELS
}

<#
.SYNOPSIS
Subscribe to the given Pub/Sub Redis channel and see all data posted to it.
#>
function Subscribe-PubSub($ChannelName, $Server=$_Server, $Port=$_Port)
{
ExecuteCommandAgainstRedis $Server $Port $_Db SUBSCRIBE $ChannelName
}

<#
.SYNOPSIS
Dumps all of the keys in a Redis database to a json file.
Probably shouldn't run this against production
#>
function Read-CacheToJson($KeyPattern="*", $Server=$_Server, $Port=$_Port, $Db=$_Db)
{
$Keys = Search-CacheKeys -KeyPattern $KeyPattern -Server $Server -Port $Port -Db $Db

foreach($Key in $Keys.Split("`n"))
{
if([string]::IsNullOrEmpty($Key)){ break; }

Get-Key -Key $Key -Server $Server -Port $Port -Db $Db | Out-File "$Key.json"
}
}

<#
.SYNOPSIS
Delete all keys that match a given pattern.
This results in N+1 connections to Redis, which means it's probably slow as hell
and should only be used against small keyspaces.
#>
function Delete-Keys($KeyPattern, $Server=$_Server, $Port=$_Port, $Db=$_Db)
{
$Keys = Search-CacheKeys -KeyPattern $KeyPattern -Server $Server -Port $Port -Db $Db

foreach($Key in $Keys.Split("`n"))
{
Delete-Key $Key $Server $Port $Db
}
}

function ExecuteCommandAgainstRedis($Server, $Port, $Db, $Command, $Params="")
{
& "$RedisFolder\Redis-Cli.exe" -h $Server -p $Port -n $Db $Command $Params
}

Export-ModuleMember -Function *-*

0 comments on commit 2fef56e

Please sign in to comment.