Skip to content

Commit a284173

Browse files
Merge pull request #226 from redanthrax/LogShipperUpdate
added logic for upgrade, updated params
2 parents 98380b3 + da9d1e3 commit a284173

File tree

1 file changed

+92
-10
lines changed

1 file changed

+92
-10
lines changed

scripts_wip/Win_PerchLogShipper_Install.ps1

Lines changed: 92 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,90 @@
1313
3. Create the follow script arguments
1414
a) -Token {{client.PerchToken}}
1515
.NOTES
16-
Version: 1.0
16+
Version: 1.1
1717
Author: redanthrax
1818
Creation Date: 2022-04-08
19+
Update Date: 2024-04-16
1920
#>
2021

2122
Param(
22-
[Parameter(Mandatory)]
2323
[string]$Token,
2424

25+
[string]$LatestVersion = "2023.05.12",
26+
2527
[switch]$Uninstall
2628
)
2729

30+
function Compare-SoftwareVersion {
31+
param (
32+
[Parameter(Mandatory = $true)]
33+
[string]$Version1,
34+
35+
[Parameter(Mandatory = $true)]
36+
[string]$Version2
37+
)
38+
39+
# Split the version strings into individual parts
40+
$versionParts1 = $Version1 -split '\.'
41+
$versionParts2 = $Version2 -split '\.'
42+
43+
# Get the minimum number of parts between the two versions
44+
$minParts = [Math]::Min($versionParts1.Count, $versionParts2.Count)
45+
46+
# Compare the version parts
47+
for ($i = 0; $i -lt $minParts; $i++) {
48+
$part1 = [int]$versionParts1[$i]
49+
$part2 = [int]$versionParts2[$i]
50+
51+
if ($part1 -gt $part2) {
52+
return $true
53+
}
54+
elseif ($part1 -lt $part2) {
55+
return $false
56+
}
57+
}
58+
59+
# If all parts are equal, check the length of the version strings
60+
if ($versionParts1.Count -gt $versionParts2.Count) {
61+
# Check the additional part in Version1
62+
$additionalPart = $versionParts1[$minParts..($versionParts1.Count - 1)] -join '.'
63+
return ![string]::IsNullOrEmpty($additionalPart)
64+
}
65+
elseif ($versionParts1.Count -lt $versionParts2.Count) {
66+
# Check the additional part in Version2
67+
$additionalPart = $versionParts2[$minParts..($versionParts2.Count - 1)] -join '.'
68+
return [string]::IsNullOrEmpty($additionalPart)
69+
}
70+
71+
return $true
72+
}
73+
2874
function Win_PerchLogShipper_Install {
29-
[CmdletBinding()]
75+
[CmdletBinding(DefaultParameterSetName = "InstallSet")]
3076
Param(
31-
[Parameter(Mandatory)]
77+
[Parameter(Mandatory = $true, ParameterSetName = "InstallSet")]
3278
[string]$Token,
3379

80+
[string]$LatestVersion,
81+
82+
[Parameter(Mandatory = $true, ParameterSetName = "UninstallSet")]
3483
[switch]$Uninstall
3584
)
3685

3786
Begin {
87+
$Upgrade = $false
3888
$Apps = @()
3989
$Apps += Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
4090
$Apps += Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*"
4191
if ($null -ne (Get-Service | Where-Object { $_.DisplayName -Match "perch" }) -and -Not($Uninstall)) {
42-
Write-Output "Perch already installed."
43-
Exit 0
92+
$perch = $Apps | Where-Object { $_.DisplayName -Match "perch"}
93+
if (Compare-SoftwareVersion $perch.DisplayVersion $LatestVersion) {
94+
Write-Output "Perch $($perch.DisplayVersion) already installed."
95+
Exit 0
96+
}
97+
else {
98+
$Upgrade = $true
99+
}
44100
}
45101

46102
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
@@ -64,6 +120,13 @@ function Win_PerchLogShipper_Install {
64120
}
65121
}
66122

123+
if ($Upgrade) {
124+
Write-Output "Attempting upgrade of Perch Log Shipper"
125+
}
126+
else {
127+
Write-Output "Installing Perch log shipper"
128+
}
129+
67130
$source = "https://cdn.perchsecurity.com/downloads/perch-log-shipper-latest.exe"
68131
$destination = "C:\packages$random\perch-log-shipper-latest.exe"
69132
Invoke-WebRequest -Uri $source -OutFile $destination
@@ -81,6 +144,13 @@ function Win_PerchLogShipper_Install {
81144
Write-Output "Install error code: $code."
82145
Exit 1
83146
}
147+
148+
if ($Upgrade) {
149+
Write-Output "Perch log shipper upgraded"
150+
}
151+
else {
152+
Write-Output "Perch log shipper installed"
153+
}
84154
}
85155
Catch {
86156
$exception = $_.Exception
@@ -94,17 +164,29 @@ function Win_PerchLogShipper_Install {
94164
Remove-Item -Path "C:\packages$random" -Recurse -Force
95165
}
96166

167+
if ($error) {
168+
Exit 1
169+
}
170+
97171
Exit 0
98172
}
99173
}
100174

101175
if (-not(Get-Command 'Win_PerchLogShipper_Install' -errorAction SilentlyContinue)) {
102176
. $MyInvocation.MyCommand.Path
103177
}
104-
105-
$scriptArgs = @{
106-
Token = $Token
107-
Uninstall = $Uninstall
178+
179+
$scriptArgs = @{}
180+
if ($Token) {
181+
$scriptArgs = @{
182+
Token = $Token
183+
LatestVersion = $LatestVersion
184+
}
185+
}
186+
if ($Uninstall) {
187+
$scriptArgs = @{
188+
Uninstall = $Uninstall
189+
}
108190
}
109191

110192
Win_PerchLogShipper_Install @scriptArgs

0 commit comments

Comments
 (0)