Skip to content

Instantly share code, notes, and snippets.

@phwelo
Last active February 5, 2018 19:03
Show Gist options
  • Save phwelo/362b94f4bd3d1a18c2abfe9c5348a8ac to your computer and use it in GitHub Desktop.
Save phwelo/362b94f4bd3d1a18c2abfe9c5348a8ac to your computer and use it in GitHub Desktop.
My Powershell Profile 11/1/2017 #powershell #windows
Import-Module VMware.VimAutomation.Core
# Connect-VIServer mtcvc01.llamasoft.local
function a ([string]$param) {
$apath = 'C:\Program Files\Atom x64\atom.exe'
[Diagnostics.Process]::Start($apath,$param)
}
Class IPAddressList {
[string]$Name
[ipaddress]$IPAddress
}
function ipaddress ([switch]$Public) {
# First take care of public IP
$IPList = @()
$PublicIP = New-Object IPAddressList
$PublicIP.Name = "Public"
$PublicIP.IPAddress = Invoke-RestMethod 'https://api.ipify.org'
$IPList += $PublicIP
# Then Loop the viable other IPs and list them
if(!$Public) {
$InterfaceAddresses = Get-NetIPAddress -AddressFamily IPv4 -AddressState Preferred -PrefixOrigin Dhcp | Select IPAddress, InterfaceAlias | Sort-Object InterfaceAlias
foreach($Interface in $InterfaceAddresses) {
$IPEntry = New-Object IPAddressList
$IPEntry.IPAddress = $Interface.IPAddress
$IPEntry.Name = $Interface.InterfaceAlias
$IPList += $IPEntry
}
}
return $IPList
}
function rc {
& rubocop --config $env:userprofile\rubocop.yml
}
$PrimaryFolders = @('c:\temp\confmgmt','C:\temp\provision_vms')
$CurrentWorking = $PrimaryFolders[0]
function Get-ChildItem-Color {
if ($Args[0] -eq $true) {
$ifwide = $true
if ($Args.Length -gt 1) {
$Args = $Args[1..($Args.length - 1)]
} else {
$Args = @()
}
} else {
$ifwide = $false
}
if (($Args[0] -eq "-a") -or ($Args[0] -eq "--all")) {
$Args[0] = "-Force"
}
$width = $host.UI.RawUI.WindowSize.Width
$items = Invoke-Expression "Get-ChildItem `"$Args`"";
$lnStr = $items | select-object Name | sort-object { "$_".length } -descending | select-object -first 1
$len = $lnStr.name.length
$cols = If ($len) {($width+1)/($len+2)} Else {1};
$cols = [math]::floor($cols);
if(!$cols){ $cols=1;}
$color_fore = $Host.UI.RawUI.ForegroundColor
$compressed_list = @(".7z", ".gz", ".rar", ".tar", ".zip", ".cab", ".msi", ".vhd", ".vxd")
$executable_list = @(".exe", ".bat", ".cmd", ".py", ".pl", ".ps1",
".psm1", ".vbs", ".rb", ".reg", ".fsx")
$dll_pdb_list = @(".dll", ".pdb")
$text_files_list = @(".csv", ".lg", "markdown", ".rst", ".txt", ".xml", ".json", ".sql")
$configs_list = @(".cfg", ".config", ".conf", ".ini")
$images_list = @(".gif",".jpg",".png",".bmp",".jpeg",".tif",".svg",".ico")
$color_table = @{}
foreach ($Extension in $compressed_list) {
$color_table[$Extension] = "Yellow"
}
foreach ($Extension in $images_list) {
$color_table[$Extension] = "Magenta"
}
foreach ($Extension in $executable_list) {
$color_table[$Extension] = "Blue"
}
foreach ($Extension in $text_files_list) {
$color_table[$Extension] = "DarkGreen"
}
foreach ($Extension in $dll_pdb_list) {
$color_table[$Extension] = "Darkblue"
}
foreach ($Extension in $configs_list) {
$color_table[$Extension] = "Green"
}
$i = 0
$pad = [math]::ceiling(($width+2) / $cols) - 3
$nnl = $false
$items |
%{
if ($_.GetType().Name -eq 'DirectoryInfo') {
$c = 'Green'
$length = ""
} else {
$c = $color_table[$_.Extension]
if ($c -eq $none) {
$c = $color_fore
}
$length = $_.length
}
# get the directory name
if ($_.GetType().Name -eq "FileInfo") {
$DirectoryName = $_.DirectoryName
} elseif ($_.GetType().Name -eq "DirectoryInfo") {
$DirectoryName = $_.Parent.FullName
}
if ($ifwide) { # Wide (ls)
if ($LastDirectoryName -ne $DirectoryName) { # change this to `$LastDirectoryName -ne $DirectoryName` to show DirectoryName
if($i -ne 0 -AND $host.ui.rawui.CursorPosition.X -ne 0){ # conditionally add an empty line
write-host ""
}
Write-Host -Fore $color_fore ("`n Directory: $DirectoryName`n")
}
$nnl = ++$i % $cols -ne 0
# truncate the item name
$towrite = $_.Name
if ($towrite.length -gt $pad) {
$towrite = $towrite.Substring(0, $pad - 3) + "..."
}
Write-Host ("{0,-$pad}" -f $towrite) -Fore $c -NoNewLine:$nnl
if($nnl){
write-host " " -NoNewLine
}
} else {
If ($LastDirectoryName -ne $DirectoryName) { # first item - print out the header
Write-Host "`n Directory: $DirectoryName`n"
Write-Host "Mode LastWriteTime Length Name"
Write-Host "---- ------------- ------ ----"
}
$Host.UI.RawUI.ForegroundColor = $c
Write-Host ("{0,-7} {1,25} {2,10} {3}" -f $_.mode,
([String]::Format("{0,10} {1,8}",
$_.LastWriteTime.ToString("d"),
$_.LastWriteTime.ToString("t"))),
$length, $_.name)
$Host.UI.RawUI.ForegroundColor = $color_fore
++$i # increase the counter
}
$LastDirectoryName = $DirectoryName
}
if ($nnl) { # conditionally add an empty line
Write-Host ""
}
}
function Get-ChildItem-Format-Wide {
$New_Args = @($true)
$New_Args += "$Args"
Invoke-Expression "Get-ChildItem-Color $New_Args"
}
Set-Alias -name dir -value Get-Childitem-Color -Option AllScope
Set-Alias -name ls -value Get-ChildItem-Format-Wide -Option AllScope
function bump {
function version-isolate{
[array]$metadata = Get-Content .\metadata.rb
[string]$verline = $metadata|where-object{$_ -like "*version*"}
$version = $verline.split("'")[1]
return $version
}
function version-increase{
$version = version-isolate
$version_array = $version.split(".")
$major = $version_array[0]
$minor = $version_array[1]
[int]$build = $version_array[2]
$build ++
$BumpedVersion = "$major.$minor.$build"
return $BumpedVersion
}
function version-replace{
$newver = version-increase
$oldver = version-isolate
$metadata = Get-Content .\metadata.rb
$NewContent = $metadata.replace($oldver,$newver)
Set-Content -value $NewContent -Path .\metadata.rb
}
version-replace
}
function cur { set-location c:\chef\cookbooks\llama_amzn }
function cc { set-location c:\chef\cookbooks}
function ca { set-location c:\aws}
function bu {
bump
berks install
berks upload --no-ssl-verify
[System.Console]::Beep(523, 125);
[System.Console]::Beep(392, 125);
[System.Console]::Beep(330, 125);
[System.Console]::Beep(440, 125);
[System.Console]::Beep(494, 125);
[System.Console]::Beep(466, 125);
[System.Console]::Beep(440, 125);
}
function editprof {
$original_path = get-location
set-location "~\documents\WindowsPowershell"
code "Microsoft.Powershell_profile.ps1"
set-location $original_path
}
function vmconn {
connect-viserver hqvmwvc01.llamasoft.local
write-host ''
write-host 'Connected to vmware server'
}
$env:SSL_CERT_FILE="C:\opscode\cacert.pem"
# Get username:
[System.Security.Principal.WindowsPrincipal]$global:currentUser =
New-Object System.Security.Principal.WindowsPrincipal(
[System.Security.Principal.WindowsIdentity]::GetCurrent()
)
if($global:currentUser.IsInRole(
[System.Security.Principal.WindowsBuiltInRole]::Administrator)
) {
$user = $global:currentUser.Identities.Name + " (Administrator)";
} else {
$user = $global:currentUser.Identities.Name
}
(Get-Host).UI.RawUI.WindowTitle = $user + " on " + [System.Net.Dns]::GetHostName() + " (v" + (Get-Host).Version + ")";
function colors{
[enum]::GetValues([System.ConsoleColor]) | Foreach-Object {Write-Host $_ -ForegroundColor $_}
}
function prompt {
$pwdarr = $pwd -split '\\'
if($env:ConEmuANSI) {
$colorsarray = @('Blue', 'DarkGreen', 'Green', 'Yellow', 'Green', 'DarkGreen', 'Blue', 'Cyan', 'Magenta', 'Cyan')
} else {
$colorsarray = @('DarkBlue', 'DarkCyan', 'DarkGray', 'Black', 'DarkGray', 'DarkCyan', 'DarkBlue', 'DarkCyan', 'DarkGray', 'Black')
}
$i = 0
foreach ($section in $pwdarr){
$color1 = $colorsarray[($i - 1)]
$color2 = $colorsarray[$i]
$i++
$color3 = $colorsarray[$i]
write-host $section -NoNewline -BackgroundColor $color1 -ForegroundColor Gray
if($i -eq ($pwdarr.count)){
write-host "$([char]0xE0B0)" -NoNewline -ForegroundColor $color1
} else {
write-host "$([char]0xE0B0)" -NoNewline -ForegroundColor $color1 -BackgroundColor $color2
}
}
return ' '
}
set-location $CurrentWorking
# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
$env:AWS_ACCESS_KEY_ID=''
$env:AWS_SECRET_ACCESS_KEY=''
powershell -noprofile -new_console:s40V
powershell -noprofile -new_console:s2TH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment