-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.ps1
111 lines (101 loc) · 3.37 KB
/
install.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
param (
[string]$localPath,
[string]$sharePath,
[string]$aUser,
[string]$aPass,
[string]$apiURL
)
# this script is for installing a new server into the solution
# run this with the user that is intended to run the cleanup script
# this script created resources on the server based on the user that runs it
# check if the localPath variable is given, if not just use the default
if (!$localPath) {
$localPath = "C:\Cleanup"
}
# check if the sharepath is given, if not then ask for it
if (!$sharePath) {
$sharePath = Read-Host "input share path for the cleanup script"
}
if (!$apiURL) {
$apiURL = Read-Host "Input API URL"
}
# check if the api user credentials are given
# currently it will ask for credentials instead of finding them elsewhere.
if ((!$aUser) -or (!$aPass)) {
$aUser = Read-Host "Input API username"
$aPass = Read-Host "Input API password"
}
# move files to a new folder located on the root
Write-Host "Checking folder and copying file"
try {
$folder = Get-ChildItem -Path $localPath -Force -ErrorAction SilentlyContinue
if (!$folder) {
New-Item -Path ("$($localPath.Split('\')[0])\") -Name ($localPath.Split('\')[1]) -ItemType Directory
Copy-Item -Path ".\cleanup.ps1" -Destination $localPath
}
else {
$file = Get-ChildItem -Path $localPath
if (!$file) {
Copy-Item -Path ".\cleanup.ps1" -Destination $localPath
}
}
}
catch {
Write-Host "caught error while moving script"
Write-Host $Error
}
# create a config file with the given data
Write-Host "Creating config file"
try {
$config = @{
url = $apiURL
share = $sharePath
}
$file = Get-ChildItem -Path $localPath | Where-Object { $_.Name -eq "config" }
if (!$file) {
$config | ConvertTo-Json | Out-File -FilePath "$($localPath)\config.json"
Write-Host "Created config file: $($localPath)\config.json"
}
}
catch {
Write-Host "caught error while saving config"
Write-Host $Error
}
# create credentials for contacting the API
Write-Host "Creating credentials"
.\createCredentials.ps1 -localPath $localPath -username $aUser -secret $aPass
# add the server to the API
Write-Host "Adding server to the API"
.\newServer.ps1 -localPath $localPath
# create a scehduled task that runs the cleanup script
Write-Host "creating scheduled task"
try {
$Error.Clear()
$action = New-ScheduledTaskAction -Execute "Powershell.exe" -Argument ".\cleanup.ps1 -ExecutionPolicy Bypass" -WorkingDirectory "$($localPath)"
$trigger = New-ScheduledTaskTrigger -Daily -At 1am # this is currently hardcoded as i dont want to create a function that checks the wanted trigger
$principal = New-ScheduledTaskPrincipal -UserId "$($env:USERDOMAIN)\$($env:USERNAME)" -LogonType ServiceAccount -RunLevel Highest
$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Hours 1).Hours
$task = New-ScheduledTask `
-Action $action `
-Principal $principal `
-Trigger $trigger `
-Settings $settings
Register-ScheduledTask `
-TaskName "cleanup" `
-InputObject $task `
-Force `
-ErrorAction Stop
}
catch {
Write-Host "caught error while setting up task"
Write-Host $Error
}
# create an eventlog for the cleanup script to write to
Write-Host "creating event log"
try {
New-EventLog -LogName "Cleanup" -Source "Cleanup script" -ErrorAction Stop -ErrorVariable eventError
}
catch {
Write-Host "caught error while setting up event log"
Write-Host $eventError
}