-
Notifications
You must be signed in to change notification settings - Fork 4
/
Install-PIServer.ps1
526 lines (434 loc) · 18.2 KB
/
Install-PIServer.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
<#
.SYNOPSIS
Installs the PI Server on Windows Server 2016 or Windows 10, 64-bit architecture only.
.DESCRIPTION
Installs SQL Server Express, PI Server, and optional PI Connector and performs configuration tasks.
Only components whose installers are passed as parameters are installed.
.PARAMETERS
.PARAMETER sql
Path to Microsoft SQL Server Express installer. Installs only if specified.
.PARAMETER piserver
Path to PI Server installer. Installs only if specified.
.PARAMETER pilicdir
Specifies the directory containing the pilicense.dat file. Defaults to the local directory.
.PARAMETER pidrive
Specifies the drive letter to use for PI Archive/Queue files. Defaults to D:\, or C:\ if no D:\ is found.
.PARAMETER afdatabase
Specifies a PI AF Database to create after PI Server install. Creates only if specified.
.PARAMETER pibundle
Specifies a self-extracting PI install kit. Installs only if specified.
This parameter can be used to install most PI software other than the PI Server kit.
.PARAMETER silentini
Specifies a silent.ini to use with the self-extracting PI install kit. If not specified, will use the default silent.ini.
.PARAMETER dryRun
Specifies to run the script in Dry-Run mode. Logs will be written, but no install/configuration actions will occur.
.PARAMETER remote
Specifies script is being run by remote PowerShell session. This flag is used by the test pipeline.
.EXAMPLES
.EXAMPLE
> .\Install-PIServer.ps1 -sql C:\Kits\SQL\Setup.exe -piserver C:\Kits\PI\PI-Server.exe -pibundle C:\Kits\PI\PIProcessBook.exe
Installs Microsoft SQL Server Express, installs PI Server 'typical' components (including PI Data Archive and PI AF Server),
and installs PI ProcessBook using its self-extracting install kit.
.EXAMPLE
> .\Install-PIServer.ps1 -sql C:\Kits\SQL\Setup.exe
Installs Microsoft SQL Server Express only.
.NOTES
This is a sample script and is not signed. It may be necessary to disable the restrictions on
ExecutionPolicy: Set-ExecutionPolicy Unrestricted
#>
#region Parameters
param(
# TODO: Investigate Powershell 'ini' or config file
[string]$sql, # Path to SQL Server Express install kit
[string]$piserver, # Path to PI Server install kit
[string]$pilicdir, # Directory containing pilicense file to use
[string]$pidrive, # Drive letter to use for PI Data Archive
[string]$afdatabase, # Optional PI AF Database to create after install
[string]$pibundle, # Path to self-extracting PI install kit
[string]$silentini, # Path to silent.ini for self-extracting PI install kit
[switch]$dryRun, # Dry run, log but do not install
[switch]$remote # Remote PowerShell
)
#endregion
#region Startup
# Start stopwatch as soon as possible!
$StartTime = Get-Date -f "yyyy-MM-ddTHH-mm-ss"
$Time = [System.Diagnostics.Stopwatch]::StartNew()
$LogFile = ".\Install-PIServer $StartTime.log"
$PiServerLogFile = ".\Install-PIServer $StartTime.piserver.log"
$SqlInstance = "SQLExpress"
$ErrorActionPreference = "Stop"
Write-Output "Using log file: $LogFile"
#endregion
#region Helper Functions
function Write-Log([string]$outputText) {
Write-Output $outputText | Out-File -Force -FilePath $LogFile -Append
}
function Write-LogError([string]$outputText) {
Write-Log $outputText
throw $outputText
}
function Write-LogFunction([string]$functionName, [string]$outputText) {
Write-Log "${functionName}: $outputText"
}
function Write-LogFunctionError([string]$functionName, [string]$outputText) {
Write-LogError "${functionName}: $outputText"
}
function Write-LogFunctionEnter([string]$functionName) {
Write-Log "${functionName}: Entering Function"
}
function Write-LogFunctionExit([string]$functionName, [string]$elapsedTime) {
if ($elapsedTime -ne "") {
Write-Log "${functionName}: Exiting Function, Elapsed time: $elapsedTime"
}
else {
Write-Log "${functionName}: Exiting Function"
}
}
#endregion
#region Script Functions
function Confirm-System() {
$func = "Confirm-System"
Write-LogFunctionEnter $func
Write-LogFunction $func "Checking System Compatibility..."
# Check that this is a 64-bit system
$bitness = (Get-CimInstance Win32_OperatingSystem).OSArchitecture
if ($bitness -NotLike "*64*") {
Write-LogFunctionError $func "Operating System not supported. This script is intended for 64 bit systems only."
}
# Check that this is Windows major version 10
$osVersion = (Get-CimInstance Win32_OperatingSystem).Version
if (-not ($osVersion -like "10.0*")) {
Write-LogFunctionError $func "Operating system not supported. This script is intended for Windows Server 2016 or Windows 10 systems only."
}
Write-LogFunction $func "System compatibility checked"
Write-LogFunctionExit $func
}
function Confirm-ParamSet($sql, $piserver, $pidrive, $pilicdir, $pibundle, $silentini) {
$func = "Confirm-Param-Set"
Write-LogFunctionEnter $func
Write-LogFunction $func "Checking Script Parameters..."
# Checks for SQL Server Express
if ($sql -ne "") {
# Installing SQL Server Express
if (Test-Path $sql -type leaf) {
Write-LogFunction $func "Found SQL Server Express install kit at: '$sql'"
}
else {
Write-LogFunctionError $func "Failed to find SQL Server Express install kit at: '$sql'"
}
}
# Checks for PI Server
if ($piserver -ne "") {
# Installing PI Server
if (Test-Path $piserver -type leaf) {
Write-LogFunction $func "Found PI Server install kit at: '$piserver'"
}
else {
Write-LogFunctionError $func "Failed to find PI Server install kit at: '$piserver'"
}
# Check that PI drive can be found
if ($pidrive -ne "") {
# Specified drive as parameter, check it exists
$testDrive = "${pidrive}:"
if ((Test-Path $testDrive) -and (Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='$testDrive'").DriveType -eq [int]3) {
$PiDirectory = "$testDrive\PI"
}
else {
Write-LogFunctionError $func "PI Drive specified '$pidrive' was not found."
}
}
else {
# No specified drive, try D: or C:
if ((Test-Path D:) -and (Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='D:'").DriveType -eq [int]3) {
$PiDirectory = "D:\PI"
}
elseif ((Test-Path C:) -and (Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='C:'").DriveType -eq [int]3) {
$PiDirectory = "C:\PI"
}
else {
Write-LogFunctionError $func "Failed to find local D: or C: drive for PI Data Archive directory. Please specify a drive using the -pidrive parameter."
}
}
Set-Variable PiDirectory "$PiDirectory" -Scope Script
Write-LogFunction $func "Will use '$PiDirectory' as PI Data Archive directory"
# Check that pilicense.dat can be found
if ($pilicdir -ne "") {
# Specified pilicdir as parameter, check pilicense.dat exists
if (-not(Test-Path "$pilicdir\pilicense.dat" -type leaf)) {
Write-LogFunctionError $func "Failed to find pilicense in folder: '$pilicdir'"
}
}
else {
# No specified pilicense, try .\pilicense.dat
$pilicdir = "."
if (-not (Test-Path "$pilicdir\pilicense.dat" -type leaf)) {
Write-LogFunctionError $func "Failed to find local pilicense.dat. Please specify a pilicense using the -pilicense parameter."
}
}
$resolvedPath = Resolve-Path $pilicdir
if ($pilicdir -ne $resolvedPath.Path) {
Write-LogFunction $func "Specified pilicdir '$pilicdir' appears to be a relative path, kit requires absolute path"
Write-LogFunction $func "Resolved pilicdir '$pilicdir' to '$($resolvedPath.Path)'"
$pilicdir = $resolvedPath.Path
}
Set-Variable pilicdir "$pilicdir" -Scope Script
Write-LogFunction $func "Found pilicense.dat in folder: '$pilicdir'"
}
# Checks for PI Bundle
if ($pibundle -ne "") {
# Installing PI Bundle
if (Test-Path $pibundle -type leaf) {
Write-LogFunction $func "Found self-extracting PI install kit at: '$pibundle'"
}
else {
Write-LogFunctionError $func "Failed to find self-extracting PI install kit at: '$pibundle'"
}
# Check that 7zip can be found
$7zipPath = "$env:ProgramFiles\7-Zip\7z.exe"
if (-not (Test-Path $7zipPath -type leaf)) {
Write-LogFunctionError $func "Failed to find 7-Zip at: '$7zipPath', ensure it is installed"
}
Set-Variable 7zipPath "$7zipPath" -Scope Script
Write-LogFunction $func "Found 7-Zip at: '$7zipPath'"
# If specified, check that silent.ini can be found
if ($silentini -ne "") {
if (Test-Path $silentini -type leaf) {
Write-LogFunction $func "Found silent.ini at: '$silentini'"
}
else {
Write-LogFunctionError $func "Failed to find silent.ini at: '$silentini'"
}
}
}
Write-LogFunction $func "Script parameters checked"
Write-LogFunctionExit $func
}
function Install-SQLServerExpress($remote, $sql) {
$func = "Install-SQLServerExpress"
Write-LogFunctionEnter $func
$fTime = [System.Diagnostics.Stopwatch]::StartNew()
Write-LogFunction $func "Installing Microsoft SQL Server Express..."
Write-LogFunction $func "Using install kit: '$sql'"
$params =
@(
"/Q",
"/IACCEPTSQLSERVERLICENSETERMS=TRUE",
"/ACTION=INSTALL",
"/FEATURES=SQLENGINE,FULLTEXT",
"/INSTANCENAME=$SqlInstance",
"/UPDATEENABLED=FALSE",
"/SQLCOLLATION=SQL_LATIN1_GENERAL_CP1_CI_AS",
"/SQLSYSADMINACCOUNTS=BUILTIN\ADMINISTRATORS"
)
if ($remote -eq $true) {
# Install kit auto update does not work from remote PowerShell session
$params += ("/UPDATEENABLED=FALSE")
}
if ($dryRun -ne $true) {
Write-LogFunction $func "Starting install..."
# Begin install attempt using defined parameters
$rc = Start-Process -FilePath $sql -ArgumentList $params -Wait -PassThru
if (($rc.ExitCode -ne 0) -and ($rc.ExitCode -ne 3010)) {
# 3010 means ok, but need to reboot
Write-LogFunctionError $func "Microsoft SQL Server Express Installation failed with error code: $($rc.ExitCode)"
}
}
else {
Write-LogFunction $func "DryRun: Parameters: '$params'"
Write-LogFunction $func "DryRun: Skipping install"
}
Write-LogFunction $func "Microsoft SQL Server Express installation completed"
Write-LogFunctionExit $func $fTime.Elapsed.ToString()
}
function Install-PIServer($piserver, $pilicdir, $sql, $dryRun) {
$func = "Install-PIServer"
Write-LogFunctionEnter $func
$fTime = [System.Diagnostics.Stopwatch]::StartNew()
Write-LogFunction $func "Installing PI Server..."
Write-LogFunction $func "Using install kit: '$piserver'"
Write-LogFunction $func "Using license directory: '$pilicdir'"
Write-LogFunction $func "Using archive/queue directory: '$PiDirectory'"
if ($sql -ne "") {
# Check that SQL is running
$sqlservice = Get-Service -DisplayName "SQL Server ($SqlInstance)" -ErrorAction SilentlyContinue
$preference = $ErrorActionPreference
$ErrorActionPreference = "Continue"
if ($sqlservice.Status -ne "Running") {
Write-LogFunctionError $func "SQL Server ($SqlInstance) was not found or is not running."
}
$ErrorActionPreference = $preference
}
$params =
@(
"/Q",
"/NORESTART",
"/LOG .\$PiServerLogFile"
"REBOOT=Suppress",
"ADDLOCAL=TYPICAL",
"FDSQLDBSERVER=.\$SqlInstance",
"AFCLIENT_SHUTDOWN_OPTIONS=2",
"PI_LICDIR=$pilicdir",
"PI_INSTALLDIR=$PiDirectory"
"PI_ARCHIVEDATDIR=$PiDirectory\Archives\",
"PI_FUTUREARCHIVEDATDIR=$PiDirectory\Archives\Future\",
"PI_EVENTQUEUEDIR=$PiDirectory\Queues\",
"PI_ARCHIVESIZE=256",
"PI_AUTOCREATEARCHIVES=1"
)
if ($dryRun -ne $true) {
Write-LogFunction $func "Starting install..."
$rc = Start-Process $piserver -ArgumentList $params -Wait -PassThru -NoNewWindow
if (($rc.ExitCode -ne 0) -and ($rc.ExitCode -ne 3010)) {
# 3010 means ok, but need to reboot
Write-LogFunctionError $func "PI Server Installation failed with error code: $($rc.ExitCode)"
}
}
else {
Write-LogFunction $func "DryRun: Parameters: '$params'"
Write-LogFunction $func "DryRun: Skipping install"
}
Write-LogFunction $func "PI Server installation completed"
Write-LogFunctionExit $func $fTime.Elapsed.ToString()
}
function Update-Environment {
[CmdletBinding(SupportsShouldProcess = $true)]
param($dryRun)
$func = "Update-Environment"
Write-LogFunctionEnter $func
Write-LogFunction $func "Updating PowerShell PATH with new PI variables..."
# Use the Environment registry entries to update PATH with variables created by PI Installer
$locations = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
'HKCU:\Environment'
if ($dryRun -eq $true) {
Write-LogFunction $func "DryRun: List variables found but do not modify path"
}
$locations | ForEach-Object {
$k = Get-Item $_
$k.GetValueNames() | ForEach-Object {
$name = $_
$value = $k.GetValue($_)
if ($dryRun -eq $true) {
Write-LogFunction $func "DryRun: Found variable '$name' with value '$value'"
}
elseif ($userLocation -and $name -ieq 'PATH') {
$Env:Path += ";$value"
}
else {
Set-Item -Path Env:\$name -Value $value
}
}
$userLocation = $true
}
Write-LogFunction $func "PowerShell PATH updated"
Write-LogFunctionExit $func
}
function Add-InitialAFDatabase($afdatabase, $dryRun) {
$func = "Add-InitialAFDatabase"
Write-LogFunctionEnter $func
Write-LogFunction $func "Adding PI AF Database..."
if ($dryRun -eq $true) {
Write-LogFunction $func "DryRun: Skipping 'Get-AFServer -Name $env:computername'"
Write-LogFunction $func "DryRun: Skipping 'Add-AFDatabase -Name $afdatabase' -AFServer {server}"
}
else {
$afserver = Get-AFServer -Name $env:computername
Add-AFDatabase -Name $afdatabase -AFServer $afserver
}
Write-LogFunction $func "PI AF Database added"
Write-LogFunctionExit $func
}
function Expand-PIBundle($pibundle, $dryRun) {
$func = "Expand-PIBundle"
Write-LogFunctionEnter $func
Write-LogFunction $func "Expanding self-extracting PI install exe..."
$baseName = (Get-Item $pibundle).BaseName
if ($dryRun -eq $true) {
Write-LogFunction $func "DryRun: Skipping 7zip extraction of '$pibundle' to directory '$baseName'"
}
else {
Write-LogFunction $func "Starting 7zip..."
$rc = Start-Process -FilePath $7zipPath -ArgumentList "x -y -o""$baseName"" ""$pibundle""" -Wait -PassThru -NoNewWindow
if ($rc.ExitCode -ne 0) {
# 0 means ok
Write-LogFunctionError $func "Expanding self-extracting PI install exe failed with error code: $($rc.ExitCode)"
}
}
Write-LogFunction $func "Self-extracting PI install exe expanded"
Write-LogFunctionExit $func
}
function Install-PIBundle($pibundle, $dryRun) {
$func = "Install-PIBundle"
Write-LogFunctionEnter $func
$fTime = [System.Diagnostics.Stopwatch]::StartNew()
Write-LogFunction $func "Installing PI setup kit..."
$baseName = (Get-Item $pibundle).BaseName
$silentIniPath = $silentini
if ($silentini -eq "") {
# Use default silent.ini
$silentIniPath = ".\$basePath\silent.ini"
}
if ($dryRun -eq $true) {
Write-LogFunction $func "DryRun: Skipping install using '.\$baseName\Setup.exe' with silent ini '$silentIniPath'"
}
else {
$rc = Start-Process -FilePath ".\$baseName\Setup.exe" -ArgumentList "-f ""$silentIniPath""" -Wait -PassThru -NoNewWindow
if (($rc.ExitCode -ne 0) -and ($rc.ExitCode -ne 3010)) {
# 3010 means ok, but need to reboot
Write-LogFunctionError $func "Installing PI setup kit failed with error code: $($rc.ExitCode)"
}
}
Write-LogFunction $func "PI setup kit installed"
Write-LogFunctionExit $func $fTime.Elapsed.ToString()
}
#endregion
#region Main Script Body
Write-Log "Checking system and script parameters"
Confirm-System
Confirm-ParamSet -sql $sql -piserver $piserver -pidrive $pidrive -pilicdir $pilicdir -pibundle $pibundle -silentini $silentini
Write-Log ""
# Run SQL Server Express Install
if ($sql -ne "") {
Write-Log "-sql flag specified, starting SQL Server Express Install"
Install-SQLServerExpress -remote $remote -sql $sql
}
else {
Write-Log "-sql flag not specified, skipping SQL Server Express Install"
}
Write-Log ""
# Run PI Server Install
if ($piserver -ne "") {
Write-Log "-piserver flag specified, starting PI Server Install"
Install-PIServer -piserver $piserver -pilicdir $pilicdir -sql $sql -dryRun $dryRun
Update-Environment -dryRun $dryRun
if ($afdatabse -ne "") {
Add-InitialAFDatabase -afdatabase $afdatabase -dryRun $dryRun
}
}
else {
Write-Log "-piserver flag not specified, skipping PI Server Install"
}
Write-Log ""
# Run PI Bundle Install
if ($pibundle -ne "") {
Write-Log "-pibundle flag specified, starting PI Bundle Install"
Expand-PIBundle -pibundle $pibundle -dryRun $dryRun
Install-PIBundle -pibundle $pibundle -dryRun $dryRun
}
else {
Write-Log "-pibundle flag not specified, skipping PI Bundle Install"
}
Write-Log ""
#endregion
#region Completion Messages
$elapsed = $Time.Elapsed.ToString()
$EndTime = Get-Date -f "MM-dd-yyyy HH:mm:ss"
Write-Output "Script started at: '$StartTime'"
Write-Output "Script ended at: '$EndTime'"
Write-Output "Total elapsed time: '$elapsed'"
Write-Log "Script started at: '$StartTime'"
Write-Log "Script ended at: '$EndTime'"
Write-Log "Total elapsed time: '$elapsed'"
Write-Output "Complete!"
Write-Log "Complete!"
#endregion