Public/Invoke-WSLPath.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
function Invoke-WSLPath {
  <#
      .Synopsis
      Covert a path inbetween the Windows and the WSL format

      .DESCRIPTION
      Use "wslpath" to convert the path

      .EXAMPLE
      # Convert Windows Path to WSL - /mnt/c/temp/
      Invoke-WSLPath -Path 'C:\temp\'

      .EXAMPLE
      # Convert WSL Path to Windows - \\wsl$\Ubuntu-18.04\usr\bin\ssh
      Invoke-WSLPath -Path '/usr/bin/ssh' -ToWindows
  #>

  [CmdletBinding(DefaultParameterSetName='WSL')]
  [Alias('wslpath')]
  [OutputType()]
  Param(
    # Path to be converted
    [Parameter(
        Mandatory,
        ValueFromPipeline
    )]
    [ValidateScript({
          if(-not ($_ | Test-Path -IsValid) ){
            throw 'Path is not valid'
          }
          return $true
    })]
    [string[]]$Path,
    # Convert Path to Windows format
    [Parameter(ParameterSetName='Win')]
    [switch]$ToWindows,
    # Convert Path to WSL format - Default
    [Parameter(ParameterSetName='WSL')]
    [switch]$ToWSL
  )
  Begin {
    if (-not (Get-Command -Name wsl.exe -ErrorAction SilentlyContinue)) {throw 'Cannot locate WSL'}
    $ArgList = [System.Collections.ArrayList]@()
    $Results = [System.Collections.Generic.List[string]]@()
    $ConvertTo = switch ($PSCmdlet.ParameterSetName) {
      'WSL' { '-u' ; break }
      'Win' { '-w' ; break }
      default { throw 'something went wrong' }
    }
  }
  Process {
    foreach ($Item in $Path) {
      $ArgList.AddRange((
          'wslpath', '-a',
          $ConvertTo, ([regex]::Escape($Item))
      ))
      $CPath = & wsl $ArgList 2>$null
      $Results.Add($CPath)
      $ArgList.Clear()
    }
  }
  End {
    return $Results
  }
}