forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-CommandSyntax.ps1
More file actions
71 lines (64 loc) · 2.5 KB
/
Copy pathGet-CommandSyntax.ps1
File metadata and controls
71 lines (64 loc) · 2.5 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
Function Get-CommandSyntax {
[cmdletbinding()]
[alias('gsyn')]
[OutputType('System.String')]
Param(
[Parameter(
Position = 0,
Mandatory,
HelpMessage = 'Enter the name of a PowerShell cmdlet or function. Ideally it has been loaded into the current PowerShell session.'
)]
[ValidateScript({ Get-Command -Name $_ })]
[string]$Name,
[Parameter(HelpMessage = 'Enter a specific provider name. The default is all currently loaded providers.')]
[ArgumentCompleter({ (Get-PSProvider).name })]
[ValidateScript({ (Get-PSProvider).Name -contains $_ })]
[string]$ProviderName
)
Write-Verbose "Starting $($MyInvocation.MyCommand)"
#define a scriptblock to run for each provider
$test = {
Param($Provider)
if ($host.name -match 'console') {
"$([char]0x1b)[1;4;38;5;155m$($provider.name)$([char]0x1b)[0m"
}
else {
$provider.name
}
#get first drive
$path = "$($provider.drives[0]):\"
Write-Verbose '..getting syntax'
Push-Location
Set-Location $path
$syn = Get-Command -Name $Name -Syntax | Out-String
Write-Verbose '..getting dynamic parameters'
$get = Get-Command -Name $name
$dynamic = ($get.parameters.GetEnumerator() | Where-Object { $_.value.IsDynamic }).key
Pop-Location
if ($dynamic) {
Write-Verbose "...found $($dynamic.count) dynamic parameters"
Write-Verbose "...$($dynamic -join ',')"
foreach ($param in $dynamic) {
if ($host.name -match 'console') {
$syn = $syn -replace "\b$param\b", "$([char]0x1b)[1;38;5;213m$param$([char]0x1b)[0m"
}
else {
#must be in the PowerShell ISE so don't use any ANSI formatting
}
}
}
$syn
} #test script block
if ($PSBoundParameters.ContainsKey('ProviderName')) {
Write-Verbose "Testing with the $($ProviderName) Provider"
Invoke-Command -ScriptBlock $test -ArgumentList (Get-PSProvider $ProviderName)
}
else {
#process all currently loaded providers
foreach ($provider in (Get-PSProvider)) {
Write-Verbose "Testing with the $($provider.name) Provider"
Invoke-Command -ScriptBlock $test -ArgumentList $provider
} #foreach Provider
}
Write-Verbose "Ending $($MyInvocation.MyCommand)"
}