-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicrosoft.PowerShell_profile.ps1
329 lines (257 loc) · 10.5 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
# Heavily customized profile originating from Posh-Git
$h = (Get-Host).UI.RawUI;
$h.BackgroundColor = "Black"
$h.ForegroundColor = "White";
Set-PSReadlineOption -TokenKind Parameter -ForegroundColor Magenta
#cls
# From: https://github.com/lzybkr/PSReadLine
if ($host.Name -eq 'ConsoleHost')
{
Import-Module PSReadline
Set-PSReadlineKeyHandler -Key Tab -Function Complete # sets bash style completion
}
# To initially install, execute from an elevated shell:
# Find-Module TabExpansionPlusPlus -repository PsGallery | Install-Module -Scope AllUsers
# EFind-Module NPMTabCompletion -repository PsGallery | Install-Module -Scope AllUsers
# From a freshly opened prompt (that does not need to be elevated) or uncomment here
Import-Module TabExpansionPlusPlus
Import-Module NPMTabCompletion
Push-Location (Split-Path -Path $MyInvocation.MyCommand.Definition -Parent)
#start-ssh-agent.cmd # this is started by default with post-git module using -quiet
cd ~\source\PS-Git
. .\posh-git.ps1
Pop-Location
$h.WindowTitle = "Posh Git PowerShell Session for - Jeff Turner";
$global:GitAllSettings = New-Object PSObject -Property @{
FolderForegroundColor = [ConsoleColor]::Cyan
}
Function export-command-hist()
{
Get-History | Export-Csv ~\history.csv
}
Function import-command-hist()
{
Import-Csv ~\history.csv | select -last 1000 | Add-History
}
Function eternal-hist($Pattern)
{
Import-Csv ~\history.csv | where {$_.CommandLine -match $Pattern} | Format-Table EndExecutionTime, CommandLine -auto
}
Function bye
{
Get-History -Count 500 | Export-CSV -Append ~\history.csv
exit
}
Function FF($Pattern)
{
Get-ChildItem . -Recurse -include * | Select-String -Pattern $Pattern
}
Function git-all()
{
$s = $global:GitAllSettings
dir -r -i .git -fo | % {
pushd $_.fullname
cd ..
write-host -fore $s.FolderForegroundColor (get-location).Path
git-trackall
popd
}
}
Function git-trackall()
{
$branchessp = git branch | %{$_ -replace "origin/"}
$branches = echo $branchessp | %{$_ -replace " *"}
if($branches){
$branches | foreach {
# if not on current branch
if ($_.StartsWith("*")){
Write-Host $_ ': current branch will not be tracked'
}else{
Write-Host 'Tracking ' $_
git branch --track $_
}
}
}else{
Write-Host 'No tracked branches for this repository'
}
git status
}
[System.Reflection.Assembly]::LoadWithPartialName("System.Diagnostics")
$sw = new-object system.diagnostics.stopwatch
$sw.Start()
#git-all
#git remote update
#git pull -a
import-command-hist
$sw.Stop()
Write-Host "Completed in " $sw.Elapsed
#git branch -v
Write-Host "Remember to use 'bye' to exit shell..."
Function MyHist {
<#
.SYNOPSIS
Gets a list of the commands entered during the current session.
.DESCRIPTION
The MyHist cmdlet gets session history, that is, the list of commands entered during the current session. This is a modified version of Get-History. In addition to the parameters of the original command, this version allows you to select only unique history entries as well as filter by a regular expression pattern.
Windows PowerShell automatically maintains a history of each session. The number of entries in the session history is determined by the value of the $MaximumHistoryCount preference variable. Beginning in Windows PowerShell 3.0, the default value is 4096.
You can save the session history in XML or CSV format. By default, history files are saved in the home directory, but you can save the file in any location.
For more information about the history features in Windows PowerShell, see about_History (http://go.microsoft.com/fwlink/?LinkID=113233).
.PARAMETER Count
Displays the specified number of the most recent history entries. By, default, MyHist gets all entries in the session history. If you use both the Count and Id parameters in a command, the display ends with the command that is specified by the Id parameter.
In Windows PowerShell 2.0, by default, MyHist gets the 32 most recent entries.
.PARAMETER Id
Specifies the ID number of an entry in the session history. MyHist gets only the specified entry. If you use both the Id and Count parameters in a command, MyHist gets the most recent entries ending with the entry specified by the Id parameter.
.PARAMETER Pattern
A regular expression pattern to match something in the commandline.
.PARAMETER Unique
Only display command history items with a unique command.
.EXAMPLE
PS C:\>MyHist -unique
This command gets the unique entries in the session history. The default display shows each command and its ID, which indicates the order of execution as well as the command's start and stop time.
.EXAMPLE
PS C:\>MyHist -pattern "service"
This command gets entries in the command history that include "service".
.NOTES
The session history is a list of the commands entered during the session. The session history represents the order of execution, the status, and the start and end times of the command. As you enter each command, Windows PowerShell adds it to the history so that you can reuse it. For more information about the command history, see about_History (http://go.microsoft.com/fwlink/?LinkID=113233).
Beginning in Windows PowerShell 3.0, the default value of the $MaximumHistoryCount preference variable is 4096. In Windows PowerShell 2.0, the default value is 64. For more information about the $MaximumHistoryCount variable, see about_Preference_Variables (http://go.microsoft.com/fwlink/?LinkID=113248).
Learn more about PowerShell:
http://jdhitsolutions.com/blog/essential-powershell-resources/
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
****************************************************************
.INPUTS
Int64
.OUTPUTS
icrosoft.PowerShell.Commands.HistoryInfo
.LINK
Add-History
Clear-History
Invoke-History
.LINK
about_History
#>
[CmdletBinding()]
Param(
[Parameter(Position=0, ValueFromPipeline=$true)]
[ValidateRange(1, 9223372036854775807)]
[long[]]$Id,
[Parameter(Position=1)]
[ValidateRange(0, 32767)]
[int]$Count,
#parameters that I added
[switch]$Unique,
[regex]$Pattern
)
Begin {
#insert verbose messages to provide debugging and tracing information
Write-Verbose "Starting $($MyInvocation.Mycommand)"
Write-Verbose "Using parameter set $($PSCmdlet.ParameterSetName)"
Write-Verbose ($PSBoundParameters | out-string)
#remove Unique and Pattern parameters if used since they are not part of Get-History
if ($Unique) {
$PSBoundParameters.Remove("Unique") | Out-Null
}
if ($Pattern) {
$PSBoundParameters.Remove("Pattern") | Out-Null
}
} #begin
Process {
#splat bound parameters to Get-History
$all = Get-History @PSBoundParameters
if ($Pattern) {
#use v4 Where method for performance purposes
Write-Verbose "Searching for commandlines matching $pattern"
$all = $all.where({$_.commandline -match $pattern})
}
if ($Unique) {
Write-Verbose "Selecting unique items"
$all = $all | Select-Object -Unique
}
#write results to pipeline and add a new property
$all | Add-Member -MemberType ScriptProperty -Name "Runtime" -value {$this.EndExecutionTime - $this.StartExecutionTime} -PassThru -force
} #process
End {
Write-Verbose "Ending $($MyInvocation.Mycommand)"
} #end
} #end Function MyHist
# Setup Gulp command completion:
$gulp_completion_Process = {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
# Load up an assembly to read the gulpfile's sha1
if(-not $global:GulpSHA1Managed) {
[Reflection.Assembly]::LoadWithPartialName("System.Security") | out-null
$global:GulpSHA1Managed = new-Object System.Security.Cryptography.SHA1Managed
}
# setup a global (in-memory) cache
if(-not $global:GulpfileShaCache) {
$global:GulpfileShaCache = @{};
}
$cache = $global:GulpfileShaCache;
# Get the gulpfile's sha1
$sha1gulpFile = (resolve-path gulpfile.js -ErrorAction Ignore | %{
$file = [System.IO.File]::Open($_.Path, "open", "read")
[string]::join('', ($global:GulpSHA1Managed.ComputeHash($file) | %{ $_.ToString("x2") }))
$file.Dispose()
})
# lookup the sha1 for previously cached task lists.
if($cache.ContainsKey($sha1gulpFile)){
$tasks = $cache[$sha1gulpFile];
} else {
$tasks = (gulp --tasks-simple).split("`n");
$cache[$sha1gulpFile] = $tasks;
}
$tasks |
where { $_.startswith($commandName) }
Sort-Object |
foreach { New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', ('{0}' -f $_) }
}
if (-not $global:options) {
$global:options = @{
CustomArgumentCompleters = @{};
NativeArgumentCompleters = @{}
}
}
$gulp_completion_Process = {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
# Load up an assembly to read the gulpfile's sha1
if(-not $global:GulpSHA1Managed) {
[Reflection.Assembly]::LoadWithPartialName("System.Security") | out-null
$global:GulpSHA1Managed = new-Object System.Security.Cryptography.SHA1Managed
}
# setup a global (in-memory) cache
if(-not $global:GulpfileShaCache) {
$global:GulpfileShaCache = @{};
}
$cache = $global:GulpfileShaCache;
# Get the gulpfile's sha1
$sha1gulpFile = (resolve-path gulpfile.js -ErrorAction Ignore | %{
$file = [System.IO.File]::Open($_.Path, "open", "read")
[string]::join('', ($global:GulpSHA1Managed.ComputeHash($file) | %{ $_.ToString("x2") }))
$file.Dispose()
})
# lookup the sha1 for previously cached task lists.
if($cache.ContainsKey($sha1gulpFile)){
$tasks = $cache[$sha1gulpFile];
} else {
$tasks = (gulp --tasks-simple).split("`n");
$cache[$sha1gulpFile] = $tasks;
}
$tasks |
where { $_.startswith($commandName) }
Sort-Object |
foreach { New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', ('{0}' -f $_) }
}
if (-not $global:options) {
$global:options = @{
CustomArgumentCompleters = @{};
NativeArgumentCompleters = @{}
}
}
$global:options['NativeArgumentCompleters']['gulp'] = $gulp_completion_Process
$function:tabexpansion2 = $function:tabexpansion2 -replace 'End\r\n{','End { if ($null -ne $options) { $options += $global:options} else {$options = $global:options}'
Invoke-Expression ((gulp --completion=powershell) -join [System.Environment]::NewLine)
#define an optional alias
Set-Alias -Name ht -Value MyHist