-
Notifications
You must be signed in to change notification settings - Fork 4
/
Labtech Patch Automation.ps1
168 lines (139 loc) · 3.98 KB
/
Labtech Patch Automation.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
#Function Declarations
######################
Function Download-Patch
{
Param
(
[Parameter(Mandatory = $true, Position = 0)]
[string]$DownloadURL,
[Parameter(Mandatory = $true, Position = 0)]
[string]$SavePath
)
try
{
$DownloadObj = new-object System.Net.WebClient;
$DownloadObj.DownloadFile($DownloadURL, $SavePath);
}
catch
{
$Output = $_.exception | Format-List -force | Out-String
log-message "[*ERROR*] : $Output"
}
}
Function Log-Message
{
<#
.SYNOPSIS
A function to write ouput messages to a logfile.
.DESCRIPTION
This function is designed to send timestamped messages to a logfile of your choosing.
Use it to replace something like write-host for a more long term log.
.PARAMETER StrMessage
The message being written to the log file.
.EXAMPLE
PS C:\> log-message -StrMessage 'This is the message being written out to the log.'
.NOTES
N/A
#>
Param
(
[Parameter(Mandatory = $True, Position = 0)]
[String]$Message
)
add-content -path $LogFilePath -value ($Message)
Write-Output $Message
}
function Get-SQLResult
{
param
(
[Parameter(Mandatory = $true, Position = 0)]
[string]$Query
)
$result = .\mysql.exe --host="localhost" --user="root" --password="$rootpass" --database="LabTech" -e "$query" --batch --raw -N;
return $result;
}
Function Output-Exception
{
$Output = $_.exception | Format-List -force | Out-String
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$UsefulData = $reader.ReadToEnd();
Write-log "[*ERROR*] : `n$Output `n$Usefuldata "
}
#Variable Declarations
###########################
$ErrorActionPreference = 'SilentlyContinue'
[String]$LogFilePath = "$Env:windir\Temp\PSUpdateLog.txt"
[String]$PatchSavePath = "$Env:windir\Temp\CurrentPatch.exe"
[String]$PatchResultsPath = "$Env:windir\Temp\LTPatchLog.txt"
[String]$CustomTableName = "lt_patchinformation"
[String]$SQLDir = "C:\Program Files (x86)\Labtech\Mysql\bin\"
[String]$PatchDownloadLink = "http://labtech-msp.com/release/LabTechPatch_10.5.2.247.exe"
#Get Root Pass
###########################
$rootpass = (get-itemproperty "HKLM:\SOFTWARE\Wow6432Node\LabTech\Setup").rootpassword
If($rootpass -eq $Null -or $rootpass -eq "")
{
Log-message -Message "Unable to retrieve root password"
Return "Unable to retrieve root password"
exit;
}
#Remove Possible Leftovers
###########################
Remove-Item -Path $LogFilePath -Force
Remove-Item -Path $PatchSavePath -Force
Remove-Item -Path $PatchResultsPath -Force
#Kill the LTClient Process
###########################
IF(Get-process -Name 'LTClient')
{
Stop-Process -Name 'LTClient' -Force
Log-message -Message "The LTClient process has been killed!"
}
#Download the Patch
###########################
Set-Location "$Env:Windir\"
Download-Patch -DownloadURL $PatchDownloadLink -SavePath $PatchSavePath
If(!Test-Path $PatchSavePath)
{
Log-Message "Failed to download the patch."
Return "Failed to download the patch."
exit;
}
#Run the Patch
###########################
$AllArgs = "/t 360 /p 360"
Start-Process -FilePath "$PatchSavePath" -ArgumentList $AllArgs -Wait -WindowStyle Hidden
$LogFileResults = Get-content -Path $PatchResultsPath
#Check For the new Table
###########################
set-location "$sqldir";
$TableQuery = @"
SELECT *
FROM information_schema.tables
WHERE table_schema = 'LabTech'
AND table_name = `'$CustomTableName`'
LIMIT 1;
"@
$TableCheck = get-sqlresult -query $TableQuery
If($TableCheck -eq $null)
{
Log-message "Unable to find $CustomTableName in the database."
[bool]$TableResult = $False
}
Else
{
Log-message "Found $CustomTableName in the database."
[bool]$TableResult = $True
}
If($LogFileResults -match "LabTech Server has been successfully updated" -and $TableResult -eq $True)
{
log-message "Patch was Successful"
Return "Success"
}
Else
{
log-message "Patch Failed"
Return "Failure"
}