-
Notifications
You must be signed in to change notification settings - Fork 84
/
New-PSDRestPS.ps1
190 lines (159 loc) · 5.72 KB
/
New-PSDRestPS.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
[CmdLetBinding()]
Param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$RestPSRootPath,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$PathtoNSSMexe,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$RestPSListenerPort,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$SecretKey,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$CertificateFriendlyName,
[Switch]$Test
)
Function Test-PSDModule{
[CmdLetBinding()]
Param(
$Name
)
If((Get-Module -ListAvailable $Name).count -eq 1){
Return $true
}
else{
Return $false
}
}
Function Set-PSDRestPS{
[CmdLetBinding()]
Param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$RestPSRootPath,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$PathtoNSSMexe,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$RestPSListenerPort,
$CertificateFriendlyName
)
$RestPSRootPath = $RestPSRootPath.TrimEnd("\")
if($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent){
$VerbosePreference = "Continue"
}
Write-Verbose "Verbose is on"
$Folder = ($RestPSRootPath | Split-Path)
if((Test-Path -Path $Folder) -ne $true){
Write-Warning "Unable to access $Folder"
Write-Warning "Will exit"
Return
}
if((Test-PSDModule -Name RESTPS) -ne $true){
Write-Warning "The RESTPS Module is not installed, please install"
Write-Warning "Will exit"
Return
}
if((Test-Path -Path $PathtoNSSMexe) -ne $true){
Write-Warning "Unable to access NSSM.exe"
Write-Warning "Will exit"
Return
}
# Import Module
Import-Module RestPS -Force
# Initual Configuration
Invoke-DeployRestPS -LocalDir $RestPSRootPath
# Fix the routes
$RestPSRootPathW = $RestPSRootPath.Replace("\","/")
(Get-Content -Path "$RestPSRootPath\endpoints\RestPSRoutes.json").Replace("c:/RestPS","$RestPSRootPathW") | Out-File -FilePath "$RestPSRootPath\endpoints\RestPSRoutes.json" -Encoding ascii
# New Service ConfigFolder
New-Item -Path "$RestPSRootPath\Service" -ItemType Directory -Force
# Create Service script
$ScriptPath = "$RestPSRootPath\Service\StartRestPS.ps1"
Set-Content -Path $ScriptPath -Value "Start-Transcript -Path $RestPSRootPath\Service\Start.log"
Add-Content -Path $ScriptPath -Value "`$ServerCert = Get-ChildItem Cert:\LocalMachine\My | Where-Object {`$_.FriendlyName -eq `"$CertificateFriendlyName`"}"
Add-Content -Path $ScriptPath -Value "Import-Module RESTPS -Force"
Add-Content -Path $ScriptPath -Value "`$ServerParams = @{"
Add-Content -Path $ScriptPath -Value "RoutesFilePath = `"$RestPSRootPath\endpoints\RestPSRoutes.json`""
Add-Content -Path $ScriptPath -Value "Port = `"$RestPSListenerPort`""
Add-Content -Path $ScriptPath -Value "SSLThumbprint = `"`$(`$ServerCert.Thumbprint)`""
Add-Content -Path $ScriptPath -Value "AppGuid = '12345678-db90-4b66-8b01-88f7af2e36ca'"
Add-Content -Path $ScriptPath -Value "VerificationType = 'VerifyUserAuth'"
Add-Content -Path $ScriptPath -Value "Logfile = `"$RestPSRootPath\Service\RestPS.log`""
Add-Content -Path $ScriptPath -Value "LogLevel = `"ALL`""
Add-Content -Path $ScriptPath -Value "RestPSLocalRoot = `"$RestPSRootPath`""
Add-Content -Path $ScriptPath -Value "}"
Add-Content -Path $ScriptPath -Value "Start-RestPSListener @ServerParams"
Add-Content -Path $ScriptPath -Value "Stop-Transcript"
# Make RestPS a Service
$PoShPath = (Get-Command powershell).Source
$NewServiceName = "RestPS"
$PoShScriptPath = "$RestPSRootPath\Service\StartRestPS.ps1"
$Arguments = ‘-ExecutionPolicy Bypass -NoProfile -File "{0}"‘ -f $PoShScriptPath
& $PathtoNSSMexe install $NewServiceName $PoShPath $Arguments
& $PathtoNSSMexe status $NewServiceName
& $PathtoNSSMexe dump $NewServiceName
# Change the name of the Services
& $PathtoNSSMexe set $NewServiceName description “RestFul API Services”
# Start the Services
Get-Service -Name RestPS | Start-Service
}
Function Test-PSDRestPS{
Param(
$RestPSListenerPort
)
$result = Invoke-RestMethod -Uri http://localhost:$RestPSListenerPort/process?name=powershell -Method Get -UseBasicParsing
if(($result.ProcessName) -eq "powershell"){
Return $true
}
else{
Return $false
}
}
Function New-PSDAuthList{
Param(
$Path,
$SecretKey
)
$UserAuth = @{
UserData = @(
@{
UserName = 'PSDClient'
SystemAuthString = "$SecretKey"
}
)
}
$UserAuth | Export-Clixml -Path $Path -Force
}
Function New-PSDGetRestUserAuthFile{
Param(
$RestPSPath
)
$RestPSPath = $RestPSPath.TrimEnd("\")
$Path = "$RestPSPath\Bin\Get-RestUserAuth.ps1"
Set-Content -Path $path -Value "function Get-RestUserAuth {"
Add-Content -Path $path -Value " `$UserAuth = Import-Clixml -Path $RestPSPath\Service\Auth.usr"
Add-Content -Path $path -Value " `$UserAuth"
Add-Content -Path $path -Value "}"
}
Set-PSDRestPS -RestPSRootPath $RestPSRootPath -PathtoNSSMexe $PathtoNSSMexe -RestPSListenerPort $RestPSListenerPort -CertificateFriendlyName $CertificateFriendlyName
if($Test){
$result = Test-PSDRestPS -RestPSListenerPort $RestPSListenerPort
switch ($result)
{
'True' {
Write-Output "RestPS seems to be working"
}
'False' {
Write-Output "RestPS does not work"
}
Default {
Write-Output "RestPS does not work"
}
}
}