forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathUtilities.ps1
More file actions
77 lines (67 loc) · 2.46 KB
/
PathUtilities.ps1
File metadata and controls
77 lines (67 loc) · 2.46 KB
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
Function Get-PathVariable {
[cmdletbinding()]
[OutputType("EnvPath")]
Param(
[ValidateSet("All", "User", "Machine")]
[string]$Scope = "All"
)
Write-Verbose "Starting $($myinvocation.MyCommand)"
#private helper function to create the custom object
Function NewEnvPath {
[cmdletbinding()]
Param(
[Parameter(ValueFromPipeline)]
[string]$Path, [string]$Scope
)
Process {
[pscustomobject]@{
PSTypeName = "EnvPath"
Scope = $Scope
Computername = [System.Environment]::MachineName
UserName = [System.Environment]::UserName
Path = $path
Exists = Test-Path $Path
}
}
} #newEnvPath
$user = {
Write-Verbose "Getting USER paths"
#filter out blanks if path ends in a splitter
$paths = [System.Environment]::GetEnvironmentVariable("PATH", "User") -split $splitter | Where-Object { $_ }
Write-Verbose "Found $($paths.count) path entries"
$paths | NewEnvPath -Scope User
}
$machine = {
Write-Verbose "Getting MACHINE paths"
$paths = [System.Environment]::GetEnvironmentVariable("PATH", "Machine") -split $splitter | Where-Object { $_ }
Write-Verbose "Found $($paths.count) path entries"
$paths | NewEnvPath -Scope Machine
}
$lx = {
Write-Verbose "Getting ALL paths (Non-Windows)"
$paths = [System.Environment]::GetEnvironmentVariable("PATH", "Process") -split $splitter | Where-Object { $_ }
Write-Verbose "Found $($paths.count) path entries"
$paths | NewEnvPath -scope "Process"
}
Write-Verbose "Using scope setting of $Scope"
#get the path separator character specific to this operating system
$splitter = [System.IO.Path]::PathSeparator
if ($IsLinux -OR $IsMacOS) {
Invoke-Command -scriptblock $lx
}
elseif ($scope -eq "User") {
Invoke-Command -scriptblock $user
}
elseif ($scope -eq "Machine") {
Invoke-Command -scriptblock $machine
}
else {
Write-Verbose "Getting ALL paths (Windows)"
$paths = @()
$paths += Invoke-Command -scriptblock $user
$paths += Invoke-Command -scriptblock $machine
Write-Verbose "Found $($paths.count) path entries"
$paths
}
Write-Verbose "Ending $($myinvocation.MyCommand)"
} #end function