-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFunctions.ps1
62 lines (52 loc) · 1.87 KB
/
Functions.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
function Get-PredefinedFunctions {
param([string[]]$names)
return $names | ForEach-Object {
Get-FunctionJson -functionName $_
} | Where-Object { $_ -ne $null }
}
function Get-FunctionJson {
param([string]$functionName)
# if the function is not exist, return null
if (-not (Get-Command $functionName -ErrorAction SilentlyContinue)) {
Write-Warning "Function $functionName does not exist."
return $null
}
# generate a json object based on the help content of the function
$help = Get-Help $functionName
# if the help doesn't include description, return null
if (-not $help.description) {
Write-Warning "Function $functionName does not have a description."
return $null
}
# if any parameters don't include description, return null
if ($help.parameters.parameter | Where-Object { -not $_.description }) {
Write-Warning "Function $functionName has parameters without description."
return $null
}
$json = [pscustomobject]@{
type = "function"
function = @{
name = $help.Name
description = $help.description[0].Text
parameters = @{
type = "object"
properties = Get-FunctionParameters -obj $help.parameters.parameter
required = @(
$help.parameters.parameter | Where-Object { $_.required -eq $true } | Select-Object -ExpandProperty Name
)
}
}
}
return $json
}
function Get-FunctionParameters {
param([psobject[]]$obj)
$result = [PSCustomObject]@{}
foreach ($item in $obj) {
$result | Add-Member -MemberType NoteProperty -Name $item.Name -Value @{
type = $item.type.name.tolower()
description = $item.description[0].Text
}
}
return $result
}