Newer
Older
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
function Stop-MyService {
Write-Host -NoNewLine "Checking status of $($ServiceName) service: "
try {
$Service = Get-Service $ServiceName -ErrorAction Stop
Write-Host $Service.Status
}
catch {
Write-Host "[FAILED]" -Fore Red
Write-Host "Can't query the service state, stopping."
Exit
}
if ($Service.Status -ne "Stopped") {
Write-Host -NoNewLine "Stopping service $($ServiceName): "
try {
Stop-Service $ServiceName -ErrorAction Stop
Write-Host "[OK]" -Fore Green
}
catch {
Write-Host "[FAILED]" -Fore Red
}
}
}
function Start-MyService {
Write-Host -NoNewLine "Starting service $($ServiceName): "
try {
Start-Service $ServiceName -ErrorAction Stop
Write-Host "[OK]" -Fore Green
}
catch {
$ex = $_.Exception
Write-Host "[FAILED]" -Fore Red
Write-Host $ex.Message
Write-Host "Please check if your configuration is valid!"
Write-Host "Showing last 20 lines of service log file:"
Write-Host "=========================================="
Get-Content "$($ServiceDir)\service.log" -Tail 20
}
}
function Copy-FileIfNew([string]$SourcePath, [string]$Destination) {
# SourcePath is expected to be a FILE name
# Destination is expected to be a PATH
if (Test-Path "$Destination\$SourcePath") {
return
}
try {
Copy-Item -Path $SourcePath -Destination $Destination -ErrorAction Stop
}
catch {
$ex = $_.Exception
Write-Host "Copying $($SourcePath) FAILED!" -Fore Red
Write-Host $ex.Message
Exit
}
}
function Copy-ServiceFiles {
Write-Host -NoNewLine "Updating / installing service files: "
$TargetDir = New-Item -ItemType Container -Force -Path $ServiceDir
try {
Copy-Item -Recurse -Force -Path "$ServiceName\*" -Destination $ServiceDir -ErrorAction Stop
Copy-FileIfNew "configuration.xml" $ServiceDir
Copy-FileIfNew "status.xml" $ServiceDir
Copy-FileIfNew "service.log" $ServiceDir
Clear-Content "$($ServiceDir)\service.log"
Write-Host "[OK]" -Fore Green
}
catch {
$ex = $_.Exception
Write-Host "[FAILED]" -Fore Red
Write-Host $ex.Message
Exit
}
}
function Install-Service {
Write-Host "Installing service $($ServiceName)... "
Write-Host "========================================================================"
$InstallUtil = "C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe"
$ServiceExe = $ServiceDir + "\" + $ServiceName + ".exe"
$ArgList = ("/username=$ServiceUser", "/password=$ServicePasswd", "/unattended", "$ServiceExe")
$InstallProcess = Start-Process -FilePath "$InstallUtil" -ArgumentList $ArgList -Wait -NoNewWindow -PassThru
Write-Host "========================================================================"
Write-Host "InstallUtil exit code: $($InstallProcess.ExitCode)"
Write-Host "========================================================================"
}
function Uninstall-FileCopyService {
try {
$FCS = "FileCopyService"
$Service = Get-Service $FCS -ErrorAction Stop
Stop-Service $FCS
}
catch {
return
}
Write-Host "Uninstalling service $($FCS)... "
Write-Host "========================================================================"
$InstallUtil = "C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe"
$ServiceExe = "C:\Tools\" + $FCS + "\" + $FCS + ".exe"
$ArgList = ("/uninstall", "$ServiceExe")
$InstallProcess = Start-Process -FilePath "$InstallUtil" -ArgumentList $ArgList -Wait -NoNewWindow -PassThru
Write-Host "========================================================================"
Write-Host "InstallUtil exit code: $($InstallProcess.ExitCode)"
Write-Host "========================================================================"
}
$LocalConfiguration = ".\ScriptsConfig.ps1"
if (Test-Path $LocalConfiguration) {
. $LocalConfiguration
} else {
Write-Host "Can't find configuration '$LocalConfiguration'!" -Fore Red
Exit
}
Uninstall-FileCopyService
try {
$Service = Get-Service $ServiceName -ErrorAction Stop
Write-Host "Service $($ServiceName) already installed, updating."
Stop-MyService
Copy-ServiceFiles
}
catch {
Copy-ServiceFiles
Install-Service
}
Start-MyService