ContainerInfo/Get-NavContainerPath.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
<#
 .Synopsis
  Get the container file system path of a host file
 .Description
  Enumerates the shared folders with the container and returns the container file system path for a file shared with the container.
 .Parameter containerName
  Name of the container for which you want to find the filepath
 .Parameter path
  Path of a file in the host file system
 .Parameter throw
  Include the throw switch to throw an exception if the folder isn't shared with the container
 .Example
  $containerPath = Get-NavContainerPath -containerName navserver -path c:\programdata\navcontainerhelper\extensions\test2\my
#>

function Get-NavContainerPath {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$true)]
        [string] $containerName,
        [Parameter(Mandatory=$true)]
        [string] $path,
        [switch] $throw
    )

    Process {
        $containerPath = ""
        if ($path.StartsWith(":")) {
            $path =$path.Substring(1)
            $exist = Invoke-ScriptInNavContainer -containerName $containerName -ScriptBlock { Param($path)
                Test-Path $path
            } -ArgumentList $path
            if ($exist) {
                $containerPath = $path
            }
            if ($throw -and "$containerPath" -eq "") {
                throw "The path $path does not exist in the container $containerName"
            }
        } else {
            $sharedFolders = Get-NavContainerSharedFolders -containerName $containerName
            $sharedFolders.GetEnumerator() | ForEach-Object {
                if ($path -eq $_.Name -or ($containerPath -eq "" -and $path.StartsWith($_.Name+"\", "OrdinalIgnoreCase"))) {
                    $containerPath = ($_.Value + $path.Substring($_.Name.Length))
                }
            }
            if ($throw -and "$containerPath" -eq "") {
                throw "The path $path is not shared with the container $containerName (nor is any of it's parent folders)"
            }
        }
        return $containerPath
    }
}
Set-Alias -Name Get-BCContainerPath -Value Get-NavContainerPath
Export-ModuleMember -Function Get-NavContainerPath -Alias Get-BCContainerPath