Skip to content

Commit bdccfa2

Browse files
committed
Cleanup files by Install and Uninstall
1 parent 5ee9e92 commit bdccfa2

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

LabTech.psm1

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -665,14 +665,6 @@ Function Uninstall-LTService{
665665
'HKU:\*\Software\Microsoft\Installer\Products\C4D064F3712D4B64086B5BDE05DBC75F'
666666
)
667667

668-
<#
669-
If ($WhatIfPreference -ne $True) {
670-
#Cleanup previous uninstallers
671-
Remove-Item 'Uninstall.exe','Uninstall.exe.config' -ErrorAction SilentlyContinue -Force -Confirm:$False
672-
New-Item "$env:windir\temp\LabTech\Installer" -type directory -ErrorAction SilentlyContinue | Out-Null
673-
}#End If
674-
#>
675-
676668
$xarg = "/x ""$UninstallBase\$UninstallMSI"" /qn"
677669
}#End Begin
678670

@@ -900,6 +892,12 @@ Function Uninstall-LTService{
900892
} ElseIf ($WhatIfPreference -ne $True) {
901893
Write-Error "ERROR: Line $(LINENUM): No valid server was reached to use for the uninstall." -ErrorAction Stop
902894
}#End If
895+
896+
If ($WhatIfPreference -ne $True) {
897+
#Cleanup uninstall files
898+
Remove-Item "$UninstallBase\$UninstallEXE","$UninstallBase\$UninstallMSI" -ErrorAction SilentlyContinue -Force -Confirm:$False
899+
}#End If
900+
903901
Write-Debug "Exiting $($myInvocation.InvocationName) at line $(LINENUM)"
904902
}#End End
905903
}#End Function Uninstall-LTService
@@ -1117,11 +1115,12 @@ Function Install-LTService{
11171115
}#End If
11181116
}#End If
11191117

1120-
$logpath = [System.Environment]::ExpandEnvironmentVariables("%windir%\temp\LabTech")
1118+
$InstallBase="${env:WINDIR}\Temp\LabTech"
1119+
$InstallMSI='Agent_Install.msi'
11211120
$logfile = "LTAgentInstall"
1122-
$curlog = "$($logpath)\$($logfile).log"
1123-
If (-not (Test-Path -PathType Container -Path "$logpath\Installer" )){
1124-
New-Item "$logpath\Installer" -type directory -ErrorAction SilentlyContinue | Out-Null
1121+
$curlog = "$($InstallBase)\$($logfile).log"
1122+
If (-not (Test-Path -PathType Container -Path "$InstallBase\Installer" )){
1123+
New-Item "$InstallBase\Installer" -type directory -ErrorAction SilentlyContinue | Out-Null
11251124
}#End if
11261125
If ((Test-Path -PathType Leaf -Path $($curlog))){
11271126
If ($PSCmdlet.ShouldProcess("$($curlog)","Rotate existing log file")){
@@ -1226,20 +1225,20 @@ Function Install-LTService{
12261225
}#End If
12271226
#>
12281227
If ( $PSCmdlet.ShouldProcess($installer, "DownloadFile") ) {
1229-
Write-Debug "Line $(LINENUM): Downloading Agent_Install.msi from $installer"
1230-
$Script:LTServiceNetWebClient.DownloadFile($installer,"${env:windir}\temp\LabTech\Installer\Agent_Install.msi")
1231-
If((Test-Path "${env:windir}\temp\LabTech\Installer\Agent_Install.msi") -and !((Get-Item "${env:windir}\temp\LabTech\Installer\Agent_Install.msi" -EA 0).length/1KB -gt 1234)) {
1232-
Write-Warning "WARNING: Line $(LINENUM): Agent_Install.msi size is below normal. Removing suspected corrupt file."
1233-
Remove-Item "${env:windir}\temp\LabTech\Installer\Agent_Install.msi" -ErrorAction SilentlyContinue -Force -Confirm:$False
1228+
Write-Debug "Line $(LINENUM): Downloading $InstallMSI from $installer"
1229+
$Script:LTServiceNetWebClient.DownloadFile($installer,"$InstallBase\Installer\$InstallMSI")
1230+
If((Test-Path "$InstallBase\Installer\$InstallMSI") -and !((Get-Item "$InstallBase\Installer\$InstallMSI" -EA 0).length/1KB -gt 1234)) {
1231+
Write-Warning "WARNING: Line $(LINENUM): $InstallMSI size is below normal. Removing suspected corrupt file."
1232+
Remove-Item "$InstallBase\Installer\$InstallMSI" -ErrorAction SilentlyContinue -Force -Confirm:$False
12341233
Continue
12351234
}#End If
12361235
}#End If
12371236

12381237
If ($WhatIfPreference -eq $True) {
12391238
$GoodServer = $Svr
1240-
} ElseIf (Test-Path "${env:windir}\temp\LabTech\Installer\Agent_Install.msi") {
1239+
} ElseIf (Test-Path "$InstallBase\Installer\$InstallMSI") {
12411240
$GoodServer = $Svr
1242-
Write-Verbose "Agent_Install.msi downloaded successfully from server $($Svr)."
1241+
Write-Verbose "$InstallMSI downloaded successfully from server $($Svr)."
12431242
} Else {
12441243
Write-Warning "WARNING: Line $(LINENUM): Error encountered downloading from $($Svr). No installation file was received."
12451244
Continue
@@ -1294,13 +1293,13 @@ Function Install-LTService{
12941293

12951294
#Build parameter string
12961295
$iarg =(@(
1297-
"/i `"${env:windir}\temp\LabTech\Installer\Agent_Install.msi`"",
1296+
"/i `"$InstallBase\Installer\$InstallMSI`"",
12981297
"SERVERADDRESS=$GoodServer",
12991298
$(If ($ServerPassword -and $ServerPassword -match '.') {"SERVERPASS=""$ServerPassword"""} Else {""}),
13001299
$(If ($LocationID -and $LocationID -match '^\d+$') {"LOCATION=$LocationID"} Else {""}),
13011300
$(If ($TrayPort -and $TrayPort -ne 42000) {"SERVICEPORT=$TrayPort"} Else {""}),
13021301
"/qn",
1303-
"/l ""$logpath\$logfile.log""") | Where-Object {$_}) -join ' '
1302+
"/l ""$InstallBase\$logfile.log""") | Where-Object {$_}) -join ' '
13041303

13051304
Try{
13061305
If ( $PSCmdlet.ShouldProcess("msiexec.exe $($iarg)", "Execute Install") ) {
@@ -1378,6 +1377,11 @@ Function Install-LTService{
13781377
Return
13791378
}#End Catch
13801379

1380+
If ($WhatIfPreference -ne $True) {
1381+
#Cleanup Install file
1382+
Remove-Item "$InstallBase\Installer\$InstallMSI" -ErrorAction SilentlyContinue -Force -Confirm:$False
1383+
}#End If
1384+
13811385
If ( $WhatIfPreference -ne $True ) {
13821386
$tmpLTSI = Get-LTServiceInfo -EA 0 -Verbose:$False -WhatIf:$False -Confirm:$False -Debug:$False
13831387
If (($tmpLTSI)) {
@@ -1390,10 +1394,10 @@ Function Install-LTService{
13901394
}#End If
13911395
} Else {
13921396
If (($Error)) {
1393-
Write-Error "ERROR: Line $(LINENUM): There was an error installing LabTech. Check the log, ${env:windir}\temp\LabTech\LTAgentInstall.log $($Error[0])"
1397+
Write-Error "ERROR: Line $(LINENUM): There was an error installing LabTech. Check the log, $InstallBase\$logfile.log $($Error[0])"
13941398
Return
13951399
} ElseIf (!($NoWait)) {
1396-
Write-Error "ERROR: Line $(LINENUM): There was an error installing LabTech. Check the log, ${env:windir}\temp\LabTech\LTAgentInstall.log"
1400+
Write-Error "ERROR: Line $(LINENUM): There was an error installing LabTech. Check the log, $InstallBase\$logfile.log"
13971401
Return
13981402
} Else {
13991403
Write-Warning "WARNING: Line $(LINENUM): LabTech installation may not have succeeded." -WarningAction Continue

0 commit comments

Comments
 (0)