-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGet-BashPath.ps1
90 lines (81 loc) · 3.36 KB
/
Get-BashPath.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
function Get-BashPath {
<#
.SYNOPSIS
To take a normal Windows path and convert it to a bash path for things like git bash.
.DESCRIPTION
To take a normal Windows path and convert it to a bash path for things like git bash.
.PARAMETER Path
A path to a file or files. Can include wildcards and the wildcards will be resolved to underlying
files. Can be a single path, an array of paths, or from the pipeline.
.PARAMETER IncludeInput
Switch to indicate if input parameters should be included in the output, aliased to 'IncludeOriginal'
.PARAMETER NoResolvePath
Switch to not resolve the provided path to see if it exists
.EXAMPLE
Get-BashPath -Path 'c:\temp\*.csv'
/C/temp/Encoding\ Time.csv
.EXAMPLE
Get-BashPath -Path 'c:\temp\*.csv' -IncludeInput
Posh bash
---- ----
C:\temp\Encoding Time.csv /C/temp/Encoding\ Time.csv
.EXAMPLE
$Special = Show-SpecialFolder -IncludeLocations | Where-Object {$_.Location -and $_.Location -match 'Program Files'}
$Special | Add-Member -MemberType NoteProperty -Name BashPath -Value 'x'
foreach ($s in $special) { $s.BashPath = Get-BashPath -Path $s.Location }
$Special
Would return
SpecialFolder Location BashPath
------------- -------- --------
CommonProgramFiles C:\Program Files\Common Files /C/Program\ Files/Common\ Files
CommonProgramFilesX86 C:\Program Files (x86)\Common Files /C/Program\ Files\ (x86)/Common\ Files
ProgramFiles C:\Program Files /C/Program\ Files
ProgramFilesX86 C:\Program Files (x86) /C/Program\ Files\ (x86)
.NOTES
The file(s) must exist for this function to work
#>
#region Parameter
[CmdletBinding(ConfirmImpact='Low')]
[OutputType('psobject')]
Param(
[Parameter(Mandatory, HelpMessage = 'Enter a path to resolve. * and ? are acceptable wildcards', Position = 0, ValueFromPipeline)]
[string[]] $Path,
[Alias('IncludeOriginal')]
[switch] $IncludeInput,
[switch] $NoResolvePath
)
#endregion Parameter
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
}
process {
foreach ($currentPath in $Path) {
if (-not $NoResolvePath) {
$resolve = [array] (Resolve-Path -Path $currentPath).Path
if (-not $resolve) {
# nothing returned
return $null
}
# .replace(' ','\ ')
foreach ($r in $resolve) {
$bash = ('/' + $r.replace('\','/').replace(':','').replace(' ','\ '))
if ($IncludeInput) {
New-Object -TypeName psobject -Property ([ordered] @{ Posh = $r; bash = $bash})
} else {
$bash
}
}
} else {
$bash = ('/' + $currentPath.replace('\','/').replace(':','').replace(' ','\ '))
if ($IncludeInput) {
New-Object -TypeName psobject -Property ([ordered] @{ Posh = $r; bash = $bash})
} else {
$bash
}
}
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}