Skip to content

Commit 91eb74c

Browse files
Merge pull request #35 from DarrenWhite99/TLSTesting
Add Update-LTService function
2 parents 9deb4e3 + 1ce5013 commit 91eb74c

File tree

1 file changed

+263
-7
lines changed

1 file changed

+263
-7
lines changed

LabTech.psm1

Lines changed: 263 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
Tested Versions: v10.5, v11, v12
1212
1313
.NOTES
14-
Version: 1.4
14+
Version: 1.5
1515
Author: Chris Taylor
1616
Website: labtechconsulting.com
1717
Creation Date: 3/14/2016
@@ -29,13 +29,16 @@
2929
Update Date: 8/7/2018
3030
Purpose/Change: Added support for TLS 1.2
3131
32+
Update Date: 8/28/2018
33+
Purpose/Change: Added Update-LTService function
34+
3235
#>
3336

3437
if (-not ($PSVersionTable)) {Write-Warning 'PS1 Detected. PowerShell Version 2.0 or higher is required.';return}
3538
if (-not ($PSVersionTable) -or $PSVersionTable.PSVersion.Major -lt 3 ) {Write-Verbose 'PS2 Detected. PowerShell Version 3.0 or higher may be required for full functionality.'}
3639

3740
#Module Version
38-
$ModuleVersion = "1.4"
41+
$ModuleVersion = "1.5"
3942

4043
If ($env:PROCESSOR_ARCHITEW6432 -match '64' -and [IntPtr]::Size -ne 8) {
4144
Write-Warning '32-bit PowerShell session detected on 64-bit OS. Attempting to launch 64-Bit session to process commands.'
@@ -77,7 +80,7 @@ Function Get-LTServiceInfo{
7780
This function will pull all of the registry data into an object.
7881
7982
.NOTES
80-
Version: 1.3
83+
Version: 1.4
8184
Author: Chris Taylor
8285
Website: labtechconsulting.com
8386
Creation Date: 3/14/2016
@@ -92,6 +95,9 @@ Function Get-LTServiceInfo{
9295
Update Date: 3/12/2018
9396
Purpose/Change: Support for ShouldProcess to enable -Confirm and -WhatIf.
9497
98+
Update Date: 8/28/2018
99+
Purpose/Change: Remove '~' from server addresses.
100+
95101
.LINK
96102
http://labtechconsulting.com
97103
#>
@@ -125,7 +131,7 @@ Function Get-LTServiceInfo{
125131
}
126132
$key.BasePath = [System.Environment]::ExpandEnvironmentVariables($($key|Select-object -Expand BasePath -EA 0)) -replace '\\\\','\'
127133
if (($key) -ne $Null -and ($key|Get-Member|Where-Object {$_.Name -match 'Server Address'})) {
128-
$Servers = ($Key|Select-Object -Expand 'Server Address' -EA 0).Split('|')|ForEach-Object {$_.Trim()}
134+
$Servers = ($Key|Select-Object -Expand 'Server Address' -EA 0).Split('|')|ForEach-Object {$_.Trim() -replace '~',''}
129135
Add-Member -InputObject $key -MemberType NoteProperty -Name 'Server' -Value $Servers -Force
130136
}#End If
131137
}#End Try
@@ -768,7 +774,7 @@ Function Uninstall-LTService{
768774
#Run MSI uninstaller for current installation
769775
Write-Verbose "Launching MSI Uninstall."
770776
Write-Debug "Executing Command ""msiexec.exe $($xarg)"""
771-
Start-Process -Wait -FilePath "$env:windir\system32\msiexec.exe" -ArgumentList $xarg
777+
Start-Process -Wait -FilePath "$env:windir\system32\msiexec.exe" -ArgumentList $xarg -WorkingDirectory $env:TEMP
772778
Start-Sleep -Seconds 5
773779
} Else {
774780
Write-Verbose "WARNING: $($env:windir)\temp\LabTech\Installer\Agent_Install.msi was not found."
@@ -780,7 +786,7 @@ Function Uninstall-LTService{
780786
#Run Agent_Uninstall.exe
781787
Write-Verbose "Launching Agent Uninstaller"
782788
Write-Debug "Executing Command ""$($env:windir)\temp\Agent_Uninstall.exe"""
783-
Start-Process -Wait -FilePath "$($env:windir)\temp\Agent_Uninstall.exe"
789+
Start-Process -Wait -FilePath "$($env:windir)\temp\Agent_Uninstall.exe" -WorkingDirectory $env:TEMP
784790
Start-Sleep -Seconds 5
785791
} Else {
786792
Write-Verbose "WARNING: $($env:windir)\temp\Agent_Uninstall.exe was not found."
@@ -1186,7 +1192,7 @@ Function Install-LTService{
11861192
$svcRun = ('LTService') | Get-Service -EA 0 | Measure-Object | Select-Object -Expand Count
11871193
If ($svcRun -eq 0) {
11881194
Write-Verbose "Launching Installation Process: msiexec.exe $(($iarg))"
1189-
Start-Process -Wait -FilePath "$env:windir\system32\msiexec.exe" -ArgumentList $iarg
1195+
Start-Process -Wait -FilePath "$env:windir\system32\msiexec.exe" -ArgumentList $iarg -WorkingDirectory $env:TEMP
11901196
Start-Sleep 5
11911197
}
11921198
$svcRun = ('LTService') | Get-Service -EA 0 | Measure-Object | Select-Object -Expand Count
@@ -1478,6 +1484,249 @@ Function Redo-LTService{
14781484
#endregion Redo-LTService
14791485
Set-Alias -Name ReInstall-LTService -Value Redo-LTService
14801486

1487+
Function Update-LTService{
1488+
#region [Update-LTService]---------------------------------------------------
1489+
<#
1490+
.SYNOPSIS
1491+
This function will manually update the LabTech agent to the requested version.
1492+
1493+
.DESCRIPTION
1494+
This script will attempt to pull current server settings from machine, then download and run the agent updater.
1495+
1496+
1497+
.PARAMETER Version
1498+
This is the agent version to install.
1499+
Example: 120.240
1500+
This is needed to download the update file. If omitted, the version advertised by the server will be used.
1501+
1502+
.EXAMPLE
1503+
Update-LTService -Version 120.240
1504+
This will update the Automate agent to the specific version requested, using the server address in the registry.
1505+
1506+
.EXAMPLE
1507+
Update-LTService
1508+
This will update the Automate agent to the current version advertised, using the server address in the registry.
1509+
1510+
.NOTES
1511+
Version: 1.0
1512+
Author: Darren White
1513+
Creation Date: 8/28/2018
1514+
Purpose/Change: Initial function development
1515+
1516+
.LINK
1517+
http://labtechconsulting.com
1518+
#>
1519+
[CmdletBinding(SupportsShouldProcess=$True)]
1520+
Param(
1521+
[parameter(Position=0)]
1522+
[AllowNull()]
1523+
[string]$Version
1524+
)
1525+
1526+
Begin{
1527+
Set-Alias -name LINENUM -value Get-CurrentLineNumber -WhatIf:$False -Confirm:$False
1528+
Write-Debug "Starting $($myInvocation.InvocationName) at line $(LINENUM)"
1529+
Clear-Variable Svr, GoodServer, Settings -EA 0 -WhatIf:$False -Confirm:$False #Clearing Variables for use
1530+
$Settings = Get-LTServiceInfo -EA 0 -Verbose:$False -WhatIf:$False -Confirm:$False
1531+
$updaterPath = [System.Environment]::ExpandEnvironmentVariables("%windir%\temp\_LTUpdate")
1532+
$xarg=@("/o""$updaterPath""","/y")
1533+
$uarg=@("""$updaterPath\Update.ini""")
1534+
}#End Begin
1535+
1536+
Process{
1537+
if (-not ($Server)){
1538+
If ($Settings){
1539+
$Server = $Settings|Select-object -Expand 'Server' -EA 0
1540+
}
1541+
}
1542+
1543+
Foreach ($Svr in $Server) {
1544+
If (-not ($GoodServer)) {
1545+
If ($Svr -match '^(https?://)?(([12]?[0-9]{1,2}\.){3}[12]?[0-9]{1,2}|[a-z0-9][a-z0-9_-]*(\.[a-z0-9][a-z0-9_-]*){1,})$') {
1546+
If ($Svr -notmatch 'https?://.+') {$Svr = "http://$($Svr)"}
1547+
Try {
1548+
$SvrVerCheck = "$($Svr)/Labtech/Agent.aspx"
1549+
Write-Debug "Testing Server Response and Version: $SvrVerCheck"
1550+
$SvrVer = $Script:LTServiceNetWebClient.DownloadString($SvrVerCheck)
1551+
Write-Debug "Raw Response: $SvrVer"
1552+
$SVer = $SvrVer|select-string -pattern '(?<=[|]{6})[0-9]{1,3}\.[0-9]{1,3}'|ForEach-Object {$_.matches}|Select-Object -Expand value -EA 0
1553+
If (($SVer) -eq $Null) {
1554+
Write-Verbose "Unable to test version response from $($Svr)."
1555+
Continue
1556+
}
1557+
If ($Version -match '[1-9][0-9]{2}\.[0-9]{3}') {
1558+
$updater = "$($Svr)/Labtech/Updates/LabtechUpdate_$($Version).zip"
1559+
} ElseIf ([System.Version]$SVer -ge [System.Version]'105.001') {
1560+
$Version = $SVer
1561+
Write-Verbose "Using detected version ($Version) from server: $($Svr)."
1562+
$updater = "$($Svr)/Labtech/Updates/LabtechUpdate_$($Version).zip"
1563+
}
1564+
1565+
#Kill all running processes from $updaterPath
1566+
if (Test-Path $updaterPath){
1567+
$Executables = (Get-ChildItem $updaterPath -Filter *.exe -Recurse -ErrorAction SilentlyContinue|Select-Object -Expand FullName)
1568+
if ($Executables) {
1569+
Write-Verbose "Terminating LabTech Processes from $($updaterPath) if found running: $(($Executables) -replace [Regex]::Escape($updaterPath),'' -replace '^\\','')"
1570+
Get-Process | Where-Object {$Executables -contains $_.Path } | ForEach-Object {
1571+
Write-Debug "Terminating Process $($_.ProcessName)"
1572+
$($_) | Stop-Process -Force -ErrorAction SilentlyContinue
1573+
}
1574+
}
1575+
}#End If
1576+
1577+
#Remove $updaterPath - Depth First Removal, First by purging files, then Removing Folders, to get as much removed as possible if complete removal fails
1578+
@("$updaterPath") | foreach-object {
1579+
If ((Test-Path "$($_)" -EA 0)) {
1580+
If ( $PSCmdlet.ShouldProcess("$($_)","Remove Folder") ) {
1581+
Write-Debug "Removing Folder: $($_)"
1582+
Try {
1583+
Get-ChildItem -Path $_ -Recurse -Force -ErrorAction SilentlyContinue | Where-Object { ($_.psiscontainer) } | foreach-object { Get-ChildItem -Path "$($_.FullName)" -EA 0 | Where-Object { -not ($_.psiscontainer) } | Remove-Item -Force -ErrorAction SilentlyContinue -Confirm:$False -WhatIf:$False }
1584+
Get-ChildItem -Path $_ -Recurse -Force -ErrorAction SilentlyContinue | Where-Object { ($_.psiscontainer) } | Sort-Object { $_.fullname.length } -Descending | Remove-Item -Force -ErrorAction SilentlyContinue -Recurse -Confirm:$False -WhatIf:$False
1585+
Remove-Item -Recurse -Force -Path $_ -ErrorAction SilentlyContinue -Confirm:$False -WhatIf:$False
1586+
} Catch {}
1587+
}#End If
1588+
}#End If
1589+
}#End Foreach-Object
1590+
1591+
Try {
1592+
If (-not (Test-Path -PathType Container -Path "$updaterPath" )){
1593+
New-Item "$updaterPath" -type directory -ErrorAction SilentlyContinue | Out-Null
1594+
}#End if
1595+
$updaterTest = [System.Net.WebRequest]::Create($updater)
1596+
If (($Script:LTProxy.Enabled) -eq $True) {
1597+
Write-Debug "Proxy Configuration Needed. Applying Proxy Settings to request."
1598+
$updaterTest.Proxy=$Script:LTWebProxy
1599+
}#End If
1600+
$updaterTest.KeepAlive=$False
1601+
$updaterTest.ProtocolVersion = '1.0'
1602+
$updaterResult = $updaterTest.GetResponse()
1603+
$updaterTest.Abort()
1604+
If ($updaterResult.StatusCode -ne 200) {
1605+
Write-Warning "Line $(LINENUM): Unable to download LabtechUpdate.exe version $Version from server $($Svr)."
1606+
Continue
1607+
} Else {
1608+
If ( $PSCmdlet.ShouldProcess($updater, "DownloadFile") ) {
1609+
Write-Debug "Downloading LabtechUpdate.exe from $updater"
1610+
$Script:LTServiceNetWebClient.DownloadFile($updater,"$updaterPath\LabtechUpdate.exe")
1611+
If((Test-Path "$updaterPath\LabtechUpdate.exe") -and !((Get-Item "$updaterPath\LabtechUpdate.exe" -EA 0).length/1KB -gt 1234)) {
1612+
Write-Warning "Line $(LINENUM): LabtechUpdate.exe size is below normal. Removing suspected corrupt file."
1613+
Remove-Item "$updaterPath\LabtechUpdate.exe" -ErrorAction SilentlyContinue -Force -Confirm:$False
1614+
Continue
1615+
}#End If
1616+
}#End If
1617+
1618+
If ($WhatIfPreference -eq $True) {
1619+
$GoodServer = $Svr
1620+
} ElseIf (Test-Path "$updaterPath\LabtechUpdate.exe") {
1621+
$GoodServer = $Svr
1622+
Write-Verbose "LabtechUpdate.exe downloaded successfully from server $($Svr)."
1623+
} Else {
1624+
Write-Warning "Line $(LINENUM): Error encountered downloading from $($Svr). No update file was received."
1625+
Continue
1626+
}#End If
1627+
}#End If
1628+
}#End Try
1629+
Catch {
1630+
Write-Warning "Line $(LINENUM): Error encountered downloading $updater."
1631+
Continue
1632+
}
1633+
}#End Try
1634+
Catch {
1635+
Write-Warning "Line $(LINENUM): Error encountered downloading from $($Svr)."
1636+
Continue
1637+
}
1638+
} Else {
1639+
Write-Warning "Line $(LINENUM): Server address $($Svr) is not formatted correctly. Example: https://lt.domain.com"
1640+
}
1641+
} Else {
1642+
Write-Debug "Server $($GoodServer) has been selected."
1643+
Write-Verbose "Server has already been selected - Skipping $($Svr)."
1644+
}
1645+
}#End Foreach
1646+
}#End Process
1647+
1648+
End{
1649+
$detectedVersion = $Settings|Select-object -Expand 'Version' -EA 0
1650+
If ($Null -eq $detectedVersion){
1651+
Write-Error "ERROR: Line $(LINENUM): No existing installation was found." -ErrorAction Stop
1652+
Return
1653+
}
1654+
If ([System.Version]$detectedVersion -ge [System.Version]$Version) {
1655+
Write-Warning "Line $(LINENUM): Installed version detected ($detectedVersion) is higher than or equal to the requested version ($Version)."
1656+
Return
1657+
}
1658+
If (-not ($GoodServer)) {
1659+
Write-Warning "Line $(LINENUM): No valid server was detected."
1660+
Return
1661+
}
1662+
If ([System.Version]$SVer -gt [System.Version]$Version) {
1663+
Write-Warning "Line $(LINENUM): Server version detected ($SVer) is higher than the requested version ($Version)."
1664+
Return
1665+
}
1666+
1667+
Try{
1668+
Stop-LTService
1669+
}#End Try
1670+
Catch{
1671+
Write-Error "ERROR: Line $(LINENUM): There was an error stopping the services. $($Error[0])"
1672+
return
1673+
}#End Catch
1674+
1675+
Write-Output "Updating Agent with the following information: Server $($GoodServer), Version $Version"
1676+
Try{
1677+
If ($PSCmdlet.ShouldProcess("LabtechUpdate.exe $($xarg)", "Extracting update files")) {
1678+
If ((Test-Path "$updaterPath\LabtechUpdate.exe")) {
1679+
#Extract Update Files
1680+
Write-Verbose "Launching LabtechUpdate Self-Extractor."
1681+
Write-Debug "Executing Command ""LabtechUpdate.exe $($xarg)"""
1682+
Try {
1683+
Push-Location $updaterPath
1684+
& "$updaterPath\LabtechUpdate.exe" $($xarg) 2>''
1685+
Pop-Location
1686+
}
1687+
Catch {Write-Output "Error calling LabtechUpdate.exe."}
1688+
Start-Sleep -Seconds 5
1689+
} Else {
1690+
Write-Verbose "WARNING: $updaterPath\LabtechUpdate.exe was not found."
1691+
}
1692+
}#End If
1693+
1694+
If ($PSCmdlet.ShouldProcess("Update.exe $($uarg)", "Launching Updater")) {
1695+
If ((Test-Path "$updaterPath\Update.exe")) {
1696+
#Extract Update Files
1697+
Write-Verbose "Launching Labtech Updater"
1698+
Write-Debug "Executing Command ""Update.exe $($uarg)"""
1699+
Try {& "$updaterPath\Update.exe" $($uarg) 2>''}
1700+
Catch {Write-Output "Error calling Update.exe."}
1701+
Start-Sleep -Seconds 5
1702+
} Else {
1703+
Write-Verbose "WARNING: $updaterPath\Update.exe was not found."
1704+
}
1705+
}#End If
1706+
1707+
}#End Try
1708+
1709+
Catch{
1710+
Write-Error "ERROR: Line $(LINENUM): There was an error during the update process $($Error[0])" -ErrorAction Continue
1711+
}#End Catch
1712+
1713+
Try{
1714+
Start-LTService
1715+
}#End Try
1716+
Catch{
1717+
Write-Error "ERROR: Line $(LINENUM): There was an error starting the services. $($Error[0])"
1718+
return
1719+
}#End Catch
1720+
1721+
If ($WhatIfPreference -ne $True) {
1722+
If ($?) {}
1723+
Else {$Error[0]}
1724+
}#End If
1725+
Write-Debug "Exiting $($myInvocation.InvocationName) at line $(LINENUM)"
1726+
}#End End
1727+
}#End Function Update-LTService
1728+
#endregion Update-LTService
1729+
14811730
Function Get-LTError{
14821731
#region [Get-LTError]-----------------------------------------------------------
14831732
<#
@@ -3229,6 +3478,12 @@ Function Get-LTProxy{
32293478
}#End Function Get-LTProxy
32303479
#endregion Get-LTProxy
32313480

3481+
Function Get-CurrentLineNumber {
3482+
#region [Get-CurrentLineNumber]--------------------------------------------
3483+
$MyInvocation.ScriptLineNumber
3484+
}
3485+
#endregion Get-CurrentLineNumber
3486+
32323487
Function Initialize-LTServiceModule{
32333488
#region [Initialize-LTServiceModule]--------------------------------------------
32343489
<#
@@ -3307,6 +3562,7 @@ Start-LTService
33073562
Stop-LTService
33083563
Test-LTPorts
33093564
Uninstall-LTService
3565+
Update-LTService
33103566
"@) -replace "[`r`n,\s]+",',') -split ',')
33113567

33123568
$PublicAlias=@(((@"

0 commit comments

Comments
 (0)