13
13
3. Create the follow script arguments
14
14
a) -Token {{client.PerchToken}}
15
15
. NOTES
16
- Version: 1.0
16
+ Version: 1.1
17
17
Author: redanthrax
18
18
Creation Date: 2022-04-08
19
+ Update Date: 2024-04-16
19
20
#>
20
21
21
22
Param (
22
- [Parameter (Mandatory )]
23
23
[string ]$Token ,
24
24
25
+ [string ]$LatestVersion = " 2023.05.12" ,
26
+
25
27
[switch ]$Uninstall
26
28
)
27
29
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
+
28
74
function Win_PerchLogShipper_Install {
29
- [CmdletBinding ()]
75
+ [CmdletBinding (DefaultParameterSetName = " InstallSet " )]
30
76
Param (
31
- [Parameter (Mandatory )]
77
+ [Parameter (Mandatory = $true , ParameterSetName = " InstallSet " )]
32
78
[string ]$Token ,
33
79
80
+ [string ]$LatestVersion ,
81
+
82
+ [Parameter (Mandatory = $true , ParameterSetName = " UninstallSet" )]
34
83
[switch ]$Uninstall
35
84
)
36
85
37
86
Begin {
87
+ $Upgrade = $false
38
88
$Apps = @ ()
39
89
$Apps += Get-ItemProperty " HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
40
90
$Apps += Get-ItemProperty " HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*"
41
91
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
+ }
44
100
}
45
101
46
102
[Net.ServicePointManager ]::SecurityProtocol = [Net.SecurityProtocolType ]::Tls12
@@ -64,6 +120,13 @@ function Win_PerchLogShipper_Install {
64
120
}
65
121
}
66
122
123
+ if ($Upgrade ) {
124
+ Write-Output " Attempting upgrade of Perch Log Shipper"
125
+ }
126
+ else {
127
+ Write-Output " Installing Perch log shipper"
128
+ }
129
+
67
130
$source = " https://cdn.perchsecurity.com/downloads/perch-log-shipper-latest.exe"
68
131
$destination = " C:\packages$random \perch-log-shipper-latest.exe"
69
132
Invoke-WebRequest - Uri $source - OutFile $destination
@@ -81,6 +144,13 @@ function Win_PerchLogShipper_Install {
81
144
Write-Output " Install error code: $code ."
82
145
Exit 1
83
146
}
147
+
148
+ if ($Upgrade ) {
149
+ Write-Output " Perch log shipper upgraded"
150
+ }
151
+ else {
152
+ Write-Output " Perch log shipper installed"
153
+ }
84
154
}
85
155
Catch {
86
156
$exception = $_.Exception
@@ -94,17 +164,29 @@ function Win_PerchLogShipper_Install {
94
164
Remove-Item - Path " C:\packages$random " - Recurse - Force
95
165
}
96
166
167
+ if ($error ) {
168
+ Exit 1
169
+ }
170
+
97
171
Exit 0
98
172
}
99
173
}
100
174
101
175
if (-not (Get-Command ' Win_PerchLogShipper_Install' - errorAction SilentlyContinue)) {
102
176
. $MyInvocation.MyCommand.Path
103
177
}
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
+ }
108
190
}
109
191
110
192
Win_PerchLogShipper_Install @scriptArgs
0 commit comments