-
Notifications
You must be signed in to change notification settings - Fork 14
/
Install-PSNode.ps1
70 lines (57 loc) · 5.03 KB
/
Install-PSNode.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
function Install-PSNode
{
<#
.Synopsis
Install a PSNode server on the local machine
.Description
Installs a PSNode server on a local machine
.Example
Install-PSNode "http://*:9090" -Command { 'hello world' }
.Link
Start-PSNode
.Link
Open-Port
#>
[OutputType([Nullable])]
param(
# The server url, ie. http://localhost:9090/
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)]
[string]$Server,
# The command to run within the server
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=1)]
[ScriptBlock]$Command,
# The authentication type
[Parameter(Position=2, ValueFromPipelineByPropertyName=$true)]
[Net.AuthenticationSchemes]
$AuthenticationType = "Anonymous"
)
process {
$safeServerName = $Server.Replace("/", "").Replace(":","").Replace('*', 'star')
Add-SecureSetting -Name "PSNode$safeservername" -String "$command"
$port = ([uri]$server.Replace('*', 'place')).Port
Open-Port -Port $port -Name "Port $port ForPSNode"
$startScript =
"Import-Module Pipeworks;
`$command = Get-SecureSetting `"PSNode$safeservername`" -ValueOnly
`$command = [ScriptBlock]::Create(`$command)
Start-PSNode -Server '$server' -Command `$command -AuthenticationType '$AuthenticationType' -DoNotReturn
"
#region Create Task to Start the Server
$scheduler = New-Object -ComObject Schedule.Service
$scheduler.Connect()
$task = $scheduler.NewTask(0)
$task.Principal.RunLevel = 1
$task.Settings.MultipleInstances = 3
$task.Settings.RunOnlyIfNetworkAvailable = $true
$action = $task.Actions.create(0)
$action.path = "$pshome\powershell.exe"
$base64 = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($startScript))
$action.arguments = "-sta -noexit -windowstyle hidden -encodedCommand $base64 "
$null = $task.Triggers.create(7)
$logonTrigger = $task.Triggers.create(9)
$logonTrigger.UserID = "$(whoami)"
$registeredTask = $scheduler.GetFolder("").RegisterTask("PSNode-$safeservername", $task.XmlText, 6, $null, $null, 3, $null)
$null = $registeredTask
#endregion Create Task to Start the Server
}
}