-
Notifications
You must be signed in to change notification settings - Fork 1
/
Start-LTProcess.psm1
118 lines (114 loc) · 3.32 KB
/
Start-LTProcess.psm1
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
function Start-LTProcess
{
<#
.Synopsis
LT helper for starting slient installations.
.DESCRIPTION
Used from starting silent installations with LabTech scripts.
Designed to handle failures and timeout in the event of a hung installation.
.EXAMPLE
Example of how to use this cmdlet
.EXAMPLE
Another example of how to use this cmdlet
.INPUTS
None.
.OUTPUTS
String.
.NOTES
String output is due to LT being unable to handle the Write-Error cmdlet and will only handle stdout from Write-Output.
.COMPONENT
The component this cmdlet belongs to
.ROLE
LabTech program install assisstence.
.FUNCTIONALITY
Used to ease the work of installing a program via LabTech.
#>
Param
(
# Path to the executable.
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string]
$FilePath,
# Any parameters needed by the executable.
[Parameter(Mandatory=$false,
Position=1)]
[string[]]
$ArgumentList = @(),
# Time out for installation, default is ten minutes in milliseconds.
[Parameter(Mandatory=$false,
Position=2)]
[int32]
$TimeOut = 600000,
# Switch to silence output.
[Parameter(Mandatory=$false,
Position=3)]
[switch]
$Silent
)
Begin
{
try
{
if(!$(Test-Path $FilePath))
{
throw [System.IO.FileNotFoundException]
}
}
catch [System.IO.FileNotFoundException]
{
Write-Output "The $($FilePath.Substring($FilePath.LastIndexOf('\') + 1)) file could not be found, please verify the path exists."
}
}
Process
{
try
{
$process = Start-Process -FilePath $FilePath -ArgumentList $ArgumentList -PassThru
if((Get-ItemProperty -Path $FilePath).Extension -eq ".msi")
{
$process = Get-Process msiexec | Sort-Object StartTime | Select-Object -Last 1 #Finds the msi process id.
}
do {Start-Sleep -Seconds 1}
until ($process.HasExited -or -not $process.WaitForExit($TimeOut))
if($process.ExitCode -eq 0)
{
if($Silent)
{
Write-Output "The $($FilePath.Substring($FilePath.LastIndexOf('\') + 1)) file has installed successfully."
}
}
else
{
$process.Kill()
if($Silent)
{
Write-Output "The $($FilePath.Substring($FilePath.LastIndexOf('\') + 1)) file did not install, please install manually."
}
}
}
catch [System.Exception]
{
if($process.HasExited)
{
if($Silent)
{
Write-Output "The $($FilePath.Substring($FilePath.LastIndexOf('\') + 1)) file did not install, please install manually."
}
}
else
{
$process.Kill()
if($Silent)
{
Write-Output "The $($FilePath.Substring($FilePath.LastIndexOf('\') + 1)) file did not install, please install manually."
}
}
}
}
End
{
Write-Verbose "The cmdlet has reach the end."
}
}