-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pipe-Command.ps1
48 lines (44 loc) · 1.2 KB
/
Pipe-Command.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
# Use this cmdlet to pipe files to regular commands
# Example:
# dir -r *.js | select -first 3 | pipe atom
# dir -r *.js | select -first 3 | %% devenv _ /edit
# dir -r *.js | select -first 3 | %% git checkout
function Pipe-Command
{
[CmdletBinding()]
PARAM(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[IO.FileInfo[]]$File,
[Parameter(Mandatory=$true, ValueFromRemainingArguments=$true, Position=0)]
[string]$Command,
[switch]$WhatIf
)
BEGIN
{
if ($Command -match '^git ' -and !$Command.Trim().EndsWith(' _')) {
$Command += ' --';
}
}
PROCESS
{
$QuotedPath = "`"$($file.FullName)`""
if ($Command -cmatch ' _ ') {
$CommandToRun = $Command.Replace(' _ ', " $QuotedPath ")
}
else {
$CommandToRun = "$Command $QuotedPath"
}
if ($WhatIf) {
Write-Host "What if: $CommandToRun"
}
else {
if (Get-Command Write-Information) {
Write-Information $CommandToRun
}
Invoke-Expression $CommandToRun
}
}
END {}
}
Set-Alias pipe Pipe-Command
Set-Alias %% Pipe-Command