-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bid_Sniper_Windows_Installer.vbs
189 lines (154 loc) · 6.64 KB
/
Bid_Sniper_Windows_Installer.vbs
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
Dim shell, fso, tempDir, profilePath, dropboxUrl, zipFile, extractPath, fullExtractPath, logFile
' Initialize objects
Set shell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
' Expand environment variables
tempDir = shell.ExpandEnvironmentStrings("%TEMP%")
profilePath = shell.ExpandEnvironmentStrings("%USERPROFILE%")
' Set paths
dropboxUrl = "https://dl.dropboxusercontent.com/scl/fi/0tx8dtk5iqkyp8g5iebk7/Bid_Sniper_win32.zip?rlkey=izl1nt0ithhhmx7vvh00c4n68&dl=1&raw=1"
zipFile = tempDir & "\Bid_Sniper.zip"
extractPath = profilePath
fullExtractPath = profilePath & "\Bid_Sniper"
logFile = tempDir & "\download_extract_log.txt"
' Delete log file if it exists
If fso.FileExists(logFile) Then fso.DeleteFile logFile, True
' Check if the script is running as administrator
Function IsAdmin()
Dim adminStatus
' Run the net session command and check its exit code
adminStatus = shell.Run("net session", 0, True)
' If the command was successful (exit code 0), return True, else False
If adminStatus = 0 Then
IsAdmin = True
Else
IsAdmin = False
End If
End Function
Sub ElevateIfNotAdmin()
' Check if the script is running as an administrator
If Not IsAdmin() Then
CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """", "", "runas", 1
WScript.Quit
End If
End Sub
Call ElevateIfNotAdmin()
Sub DisplayMessage(message)
MsgBox message
End Sub
' Function to check for errors by reading the log file
Function CheckForErrors()
If fso.FileExists(logFile) Then
Dim logFileContent, logContent
Set logFileContent = fso.OpenTextFile(logFile, 1, False)
logContent = logFileContent.ReadAll
logFileContent.Close
' Check if log contains "Exception" or any typical error message
If InStr(logContent, "Exception") > 0 Or InStr(logContent, "Error") > 0 Then
CheckForErrors = True
Else
CheckForErrors = False
End If
Else
CheckForErrors = True
End If
End Function
Sub KillRunningAppProcesses()
Dim objWMIService, colProcesses, objProcess, exePath
On Error Resume Next
' Connect to WMI service
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
If Err.Number <> 0 Then
MsgBox "Error: Unable to connect to WMI service. Ensure WMI is enabled and you have administrative privileges.", vbCritical
Exit Sub
End If
' Query for all processes
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process")
If Err.Number <> 0 Then
MsgBox "Error: Unable to query processes. Ensure you have administrative privileges.", vbCritical
Exit Sub
End If
' Loop through all processes and terminate those in "\Bid_Sniper\" path
For Each objProcess In colProcesses
exePath = ""
On Error Resume Next
exePath = objProcess.ExecutablePath
On Error GoTo 0
If InStr(LCase(exePath), "\bid_sniper\") > 0 Then
objProcess.Terminate
End If
Next
' Clean up objects
Set colProcesses = Nothing
Set objWMIService = Nothing
On Error GoTo 0
End Sub
Sub DownloadAndExtract()
Dim downloadAndExtractScript, psCommand
downloadAndExtractScript = _
"param($url, $zipFile, $extractPath, $fullExtractPath)" & vbCrLf & _
"if (Test-Path $zipFile) { Remove-Item $zipFile -Force }" & vbCrLf & _
"if (Test-Path $fullExtractPath) { Remove-Item $fullExtractPath -Recurse -Force }" & vbCrLf & _
"Start-BitsTransfer -Source $url -Destination $zipFile" & vbCrLf & _
"Expand-Archive -Path $zipFile -DestinationPath $extractPath -Force" & vbCrLf & _
"New-NetFirewallRule -DisplayName 'Allow Bid Sniper App' -Direction Inbound -Program '" & fullExtractPath & "\node_modules\electron\dist\electron.exe' -Action Allow -Profile Any"
psCommand = "powershell -NoProfile -Command " & _
"""& {" & downloadAndExtractScript & "} -url '" & dropboxUrl & "' -zipFile '" & zipFile & "' -extractPath '" & extractPath & "' -fullExtractPath '" & fullExtractPath & "' 2>&1 | Out-File -FilePath '" & logFile & "' -Encoding utf8"""
shell.Run psCommand, 1, True
End Sub
Function ReadLogFile()
Dim logFileContent, logContent
If fso.FileExists(logFile) Then
On Error Resume Next
Set logFileContent = fso.OpenTextFile(logFile, 1, False)
If Not logFileContent.AtEndOfStream Then
logContent = logFileContent.ReadAll
Else
logContent = ""
End If
logFileContent.Close
On Error GoTo 0
ReadLogFile = logContent
Else
ReadLogFile = "An unexpected error occurred: the log file was not created."
End If
End Function
Function InstallationSuccessful()
InstallationSuccessful = fso.FileExists(fullExtractPath & "\node.exe")
End Function
Sub CreateLaunchScript()
Dim launchScriptPath, launchScript
launchScriptPath = fullExtractPath & "\LaunchBidSniper.vbs"
Set launchScript = fso.CreateTextFile(launchScriptPath, True)
launchScript.WriteLine "Set WshShell = CreateObject(""WScript.Shell"")"
launchScript.WriteLine "WshShell.Run ""cmd /c cd /d """"" & fullExtractPath & """"" && set NODE_ENV=production && """"" & fullExtractPath & "\node.exe"""" """"" & fullExtractPath & "\node_modules\electron\cli.js"""" """"" & fullExtractPath & """"""", 0, False"
launchScript.Close
End Sub
Sub CreateDesktopShortcut()
Dim shortcutPath, shortcut, iconPath
shortcutPath = shell.SpecialFolders("Desktop") & "\Bid Sniper.lnk"
iconPath = fullExtractPath & "\img\icons\win\icon.ico"
' Check if shortcut exists and delete if it does
If fso.FileExists(shortcutPath) Then fso.DeleteFile shortcutPath, True
Set shortcut = shell.CreateShortcut(shortcutPath)
With shortcut
.TargetPath = fullExtractPath & "\LaunchBidSniper.vbs"
.WorkingDirectory = fullExtractPath
.Arguments = ""
.IconLocation = iconPath
.Save
End With
End Sub
Call KillRunningAppProcesses()
' Main script execution
DisplayMessage "This script will now download and install Bid Sniper and create a desktop shortcut for it. PowerShell will open and close during this process, which is normal."
Call DownloadAndExtract()
Dim logContent
logContent = ReadLogFile()
If Not CheckForErrors() And InstallationSuccessful() Then
Call CreateLaunchScript()
Call CreateDesktopShortcut()
DisplayMessage "Bid Sniper is now installed. You can launch it from the desktop shortcut."
Else
DisplayMessage "An error occurred during the installation: " & vbCrLf & logContent
End If