forked from Tiberriver256/PoshAltiris
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConvertTo-Function.ps1
126 lines (84 loc) · 3.59 KB
/
ConvertTo-Function.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
Function Create-HTMLDOMFromFile
{
Param(
[String] $FileName,
[String] $TagName,
[Int] $OuputCount = 11
)
$HTML = New-Object -Com "HTMLFile";
$Content = Get-Content -Path $FileName -raw
#To convert raw content to HTML DOM (Document object Model) and 2 stands for DOM level 2
$HTML.IHTMLDocument2_write($Content)
if($TagName)
{
#Some data wrangling to extract the exact information
$HTML.all.tags($TagName) | select innertext -ExpandProperty innertext -First $OuputCount
}
else
{
$HTML
}
}
Function ConvertTo-Function ($WebService, $ServiceName) {
start-process hh.exe -ArgumentList @("-decompile", "$((Get-Location).Path + '\DecompiledHelp')", "$((Get-Item .\ASDK8.0.chm).FullName)") -Wait
$WebServiceMethods = $WebService | gm -MemberType Method | where {$_.Name -notmatch "Async" -and$_.Definition -notmatch "System.IAsyncResult" -and @("Discover","Dispose","ToString","Abort","InitializeLifetimeService","GetType","GetHashcode","Equals","GetLifeTimeService","CreateObjRef") -notcontains $_.name}
foreach ($Method in $WebServiceMethods) {
Write-Host "Parsing method: $Method"
$CurrentMethodDefinition = [regex]::matches($Method.Definition, "^(.*?)\s(\w*)\(((\w*)\s(\w*),?\s?)*\).*$")
$HtmlHelpFileName = Get-Item ".\DecompiledHelp\html\*_$($ServiceName)Lib*$($CurrentMethodDefinition.Captures[0].Groups[2].Value).htm"
$HTMLDOMFile = Create-HTMLDOMFromFile -FileName $HtmlHelpFileName.FullName
$Function = @"
Function $($CurrentMethodDefinition.Captures[0].Groups[2].Value) {
<#
.SYNOPSIS
$(($HTMLDOMFile.body.getElementsByClassName("summary"))[0].textcontent)$(
for($i = 0; $i -lt $CurrentMethodDefinition.captures[0].groups[4].Captures.count; $i++) {
@"
.PARAMETER $($CurrentMethodDefinition[0].captures[0].groups[5].Captures[$i].Value)
$(($HTMLDOMFile.getElementById("parameters").childnodes[$i+1].innerText -split '\n')[2])
"@
}
@"
.EXAMPLE
$($HTMLDomFile.getElementById("exampleSection").InnerText)
"@
$(if($HTMLDomFile.getElementById("remarksSection").InnerText -notmatch "The CLI is being deprecated. Please see the CLI Programming Guide."){@"
.NOTES
$($HTMLDomFile.getElementById("remarksSection").InnerText)
"@}
)
)
#>
param (
"@
for($i = 0; $i -lt $CurrentMethodDefinition.captures[0].groups[4].Captures.count; $i++) {
$Function += "`t`t`t[Parameter(Mandatory=`$true)]`n`t`t`t[$($CurrentMethodDefinition.captures[0].groups[4].Captures[$i].Value)]`$$($CurrentMethodDefinition[0].captures[0].groups[5].Captures[$i].Value),`n"
}
$Function += @"
[Parameter(Mandatory=`$true)]
[string]`$Server,
[PSCredential]`$Credential
)
`$Body = @{
$(
for($i = 0; $i -lt $CurrentMethodDefinition.captures[0].groups[4].Captures.count; $i++) {
"`t`t`t$($CurrentMethodDefinition.captures[0].groups[5].Captures[$i].Value) = `$$($CurrentMethodDefinition[0].captures[0].groups[5].Captures[$i].Value)`n"
}
)
}
`$WebServiceUrl = "$($WebService.Url -replace "^\w*:\/\/.*?\/",'')/$($CurrentMethodDefinition[0].Captures[0].Groups[2].Value)"
if(`$Credential)
{
Invoke-RestMethod -Uri "https://`$Server/`$WebServiceUrl" -Method Post -Body `$Body -Credential `$Credential
}
else
{
Invoke-RestMethod -Uri "https://`$Server/`$WebServiceUrl" -Method Post -Body `$Body -UseDefaultCredentials
}
}
"@
$Function | Out-String
}
Remove-Item .\DecompiledHelp -Force -Recurse
}