forked from mikemaccana/powershell-profile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Microsoft.PowerShell_profile.ps1
349 lines (281 loc) · 9.58 KB
/
Microsoft.PowerShell_profile.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# Install-Module Find-String
# Set-ExecutionPolicy unrestricted
# Install OpenSSH
# Note: do not install chocolatey. Use Install-Package instead.
# Get-PackageProvider
# Get-PackageSource -Provider chocolatey
# Install-Package -Name openssh
Add-PathVariable "${env:ProgramFiles}\OpenSSH"
Add-PathVariable "${env:ProgramFiles}\rethinkdb"
Add-PathVariable "${env:ProgramFiles}\7-Zip"
Add-PathVariable "C:\OpenSSL-Win32\bin"
# For working less (except in ISE)
# Install-Package Pscx
# For history with up/down arrows
# Install-Package PSReadLine
Import-Module PSReadLine
# https://technet.microsoft.com/en-us/magazine/hh241048.aspx
$MaximumHistoryCount = 10000
# Tab completion for git
# Install-Module posh-git
# Load posh-git example profile
# . 'C:\Users\mike\Documents\WindowsPowerShell\Modules\posh-git\profile.example.ps1'
# https://gallery.technet.microsoft.com/scriptcenter/Get-NetworkStatistics-66057d71
. 'C:\Users\mike\powershell\Get-NetworkStatistics.ps1'
function uptime {
Get-WmiObject win32_operatingsystem | select csname, @{LABEL='LastBootUpTime';
EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
}
function edit-powershell-profile {
subl $profile
}
function reload-powershell-profile {
& $profile
}
# https://blogs.technet.microsoft.com/heyscriptingguy/2012/12/30/powertip-change-the-powershell-console-title
function change-title([string]$newtitle) {
$host.ui.RawUI.WindowTitle = $newtitle + ' – ' + $host.ui.RawUI.WindowTitle
}
# From http://stackoverflow.com/questions/7330187/how-to-find-the-windows-version-from-the-powershell-command-line
function get-windows-build {
[Environment]::OSVersion
}
# http://mohundro.com/blog/2009/03/31/quickly-extract-files-with-powershell/
function unarchive([string]$file, [string]$outputDir = '') {
if (-not (Test-Path $file)) {
$file = Resolve-Path $file
}
if ($outputDir -eq '') {
$outputDir = [System.IO.Path]::GetFileNameWithoutExtension($file)
}
7z e "-o$outputDir" $file
}
#######################################################
# Prompt Tools
#######################################################
# https://github.com/gummesson/kapow/blob/master/themes/bashlet.ps1
function prompt {
$realLASTEXITCODE = $LASTEXITCODE
Write-Host $(Truncate-HomeDirectory("$pwd")) -ForegroundColor Yellow -NoNewline
Write-Host " $" -NoNewline
$global:LASTEXITCODE = $realLASTEXITCODE
Return " "
}
function Truncate-HomeDirectory($Path) {
$Path.Replace("$home", "~")
}
function Test-FileInSubPath([System.IO.DirectoryInfo]$Child, [System.IO.DirectoryInfo]$Parent) {
write-host $Child.FullName | select '*'
$Child.FullName.StartsWith($Parent.FullName)
}
# Produce UTF-8 by default
# https://news.ycombinator.com/item?id=12991690
$PSDefaultParameterValues["Out-File:Encoding"]="utf8"
#######################################################
# Dev Tools
#######################################################
function subl {
& "$env:ProgramFiles\Sublime Text 3\subl.exe" @args
}
function explorer {
explorer.exe .
}
function gg {
& git grep -i @args
}
# See https://jira.atlassian.com/browse/SRCTREEWIN-394 for some limits here
function stree {
& "${env:ProgramFiles(x86)}\Atlassian\SourceTree\SourceTree.exe" -f $pwd
}
function open($file) {
ii $file
}
# http://stackoverflow.com/questions/39148304/fuser-equivalent-in-powershell/39148540#39148540
function fuser($relativeFile){
$file = Resolve-Path $relativeFile
echo "Looking for processes using $file"
foreach ( $Process in (Get-Process)) {
foreach ( $Module in $Process.Modules) {
if ( $Module.FileName -like "$file*" ) {
$Process | select id, path
}
}
}
}
#######################################################
# Useful shell aliases
#######################################################
function findfile($name) {
ls -recurse -filter "*${name}*" -ErrorAction SilentlyContinue | foreach {
$place_path = $_.directory
echo "${place_path}\${_}"
}
}
function get-path {
($Env:Path).Split(";")
}
#######################################################
# Unixlike commands
#######################################################
function df {
get-volume
}
function sed($file, $find, $replace){
(Get-Content $file).replace("$find", $replace) | Set-Content $file
}
function sed-recursive($filePattern, $find, $replace) {
$files = ls . "$filePattern" -rec
foreach ($file in $files) {
(Get-Content $file.PSPath) |
Foreach-Object { $_ -replace "$find", "$replace" } |
Set-Content $file.PSPath
}
}
function grep($regex, $dir) {
if ( $dir ) {
ls $dir | select-string $regex
return
}
$input | select-string $regex
}
function grepv($regex) {
$input | ? { !$_.Contains($regex) }
}
function which($name) {
Get-Command $name | Select-Object -ExpandProperty Definition
}
# Should really be name=value like Unix version of export but not a big deal
function export($name, $value) {
set-item -force -path "env:$name" -value $value;
}
function pkill($name) {
ps $name -ErrorAction SilentlyContinue | kill
}
function pgrep($name) {
ps $name
}
function touch($file) {
"" | Out-File $file -Encoding ASCII
}
# From https://github.com/keithbloom/powershell-profile/blob/master/Microsoft.PowerShell_profile.ps1
function sudo {
$file, [string]$arguments = $args;
$psi = new-object System.Diagnostics.ProcessStartInfo $file;
$psi.Arguments = $arguments;
$psi.Verb = "runas";
$psi.WorkingDirectory = get-location;
[System.Diagnostics.Process]::Start($psi) >> $null
}
# https://gist.github.com/aroben/5542538
function pstree {
$ProcessesById = @{}
foreach ($Process in (Get-WMIObject -Class Win32_Process)) {
$ProcessesById[$Process.ProcessId] = $Process
}
$ProcessesWithoutParents = @()
$ProcessesByParent = @{}
foreach ($Pair in $ProcessesById.GetEnumerator()) {
$Process = $Pair.Value
if (($Process.ParentProcessId -eq 0) -or !$ProcessesById.ContainsKey($Process.ParentProcessId)) {
$ProcessesWithoutParents += $Process
continue
}
if (!$ProcessesByParent.ContainsKey($Process.ParentProcessId)) {
$ProcessesByParent[$Process.ParentProcessId] = @()
}
$Siblings = $ProcessesByParent[$Process.ParentProcessId]
$Siblings += $Process
$ProcessesByParent[$Process.ParentProcessId] = $Siblings
}
function Show-ProcessTree([UInt32]$ProcessId, $IndentLevel) {
$Process = $ProcessesById[$ProcessId]
$Indent = " " * $IndentLevel
if ($Process.CommandLine) {
$Description = $Process.CommandLine
} else {
$Description = $Process.Caption
}
Write-Output ("{0,6}{1} {2}" -f $Process.ProcessId, $Indent, $Description)
foreach ($Child in ($ProcessesByParent[$ProcessId] | Sort-Object CreationDate)) {
Show-ProcessTree $Child.ProcessId ($IndentLevel + 4)
}
}
Write-Output ("{0,6} {1}" -f "PID", "Command Line")
Write-Output ("{0,6} {1}" -f "---", "------------")
foreach ($Process in ($ProcessesWithoutParents | Sort-Object CreationDate)) {
Show-ProcessTree $Process.ProcessId 0
}
}
function unzip ($file) {
$dirname = (Get-Item $file).Basename
echo("Extracting", $file, "to", $dirname)
New-Item -Force -ItemType directory -Path $dirname
expand-archive $file -OutputPath $dirname -ShowProgress
}
# From https://certsimple.com/blog/openssl-shortcuts
function openssl-view-certificate ($file) {
openssl x509 -text -noout -in $file
}
function openssl-view-csr ($file) {
openssl req -text -noout -verify -in $file
}
function openssl-view-key ($file) {
openssl rsa -check -in $file
}
function openssl-view-pkcs12 ($file) {
openssl pkcs12 -info -in $file
}
# Connecting to a server (Ctrl C exits)
function openssl-client ($server) {
openssl s_client -status -connect $server:443
}
# Convert PEM private key, PEM certificate and PEM CA certificate (used by nginx, Apache, and other openssl apps)
# to a PKCS12 file (typically for use with Windows or Tomcat)
function openssl-convert-pem-to-p12 ($key, $cert, $cacert, $output) {
openssl pkcs12 -export -inkey $key -in $cert -certfile $cacert -out $output
}
# Convert a PKCS12 file to PEM
function openssl-convert-p12-to-pem ($p12file, $pem) {
openssl pkcs12 -nodes -in $p12file -out $pemfile
}
# Convert a crt to a pem file
function openssl-crt-to-pem($crtfile) {
v
openssl x509 -in $crtfile -out $basename.pem -outform PEM
}
# Check the modulus of a certificate (to see if it matches a key)
function openssl-check-certificate-modulus {
openssl x509 -noout -modulus -in "${1}" | shasum -a 256
}
# Check the modulus of a key (to see if it matches a certificate)
function openssl-check-key-modulus {
openssl rsa -noout -modulus -in "${1}" | shasum -a 256
}
# Check the modulus of a certificate request
function openssl-check-key-modulus {
openssl req -noout -modulus -in "${1}" | shasum -a 256
}
# Encrypt a file (because zip crypto isn't secure)
function openssl-encrypt () {
openssl aes-256-cbc -in "${1}" -out "${2}"
}
# Decrypt a file
function openssl-decrypt () {
openssl aes-256-cbc -d -in "${1}" -out "${2}"
}
# For setting up public key pinning
function openssl-key-to-hpkp-pin() {
openssl rsa -in "${1}" -outform der -pubout | openssl dgst -sha256 -binary | openssl enc -base64
}
# For setting up public key pinning (directly from the site)
function openssl-website-to-hpkp-pin() {
openssl s_client -connect "${1}":443 | openssl x509 -pubkey -noout | openssl rsa -pubin -outform der | openssl dgst -sha256 -binary | openssl enc -base64
}
# Combines the key and the intermediate in a unified PEM file
# (eg, for nginx)
function openssl-key-and-intermediate-to-unified-pem() {
echo -e "$(cat "${1}")\n$(cat "${2}")" > "${1:0:-4}"_unified.pem
}
# http://stackoverflow.com/questions/39221953/can-i-make-powershell-tab-complete-show-me-all-options-rather-than-picking-a-sp
Set-PSReadlineKeyHandler -Chord Tab -Function MenuComplete
echo 'Mike profile loaded.'