-
Notifications
You must be signed in to change notification settings - Fork 0
/
CL_RePoster_Windows_Installer.vbs
149 lines (123 loc) · 5.55 KB
/
CL_RePoster_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
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/bxmy2hdjuowhglvbzwzvy/CraigsList_RePoster_win32.zip?rlkey=q8o2n2v0wjj2f9ckknnkzav9d&dl=1&raw=1"
zipFile = tempDir & "\CraigsList_RePoster.zip"
extractPath = profilePath
fullExtractPath = profilePath & "\CraigsList_RePoster"
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 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 CraigsList RePoster 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 & "\Launch.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") & "\CraigsList RePoster.lnk"
iconPath = fullExtractPath & "\assets\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 & "\Launch.vbs"
.WorkingDirectory = fullExtractPath
.Arguments = ""
.IconLocation = iconPath
.Save
End With
End Sub
' Main script execution
DisplayMessage "This script will now download and install CraigsList RePoster 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 "CraigsList RePoster is now installed. You can launch it from the desktop shortcut."
Else
DisplayMessage "An error occurred during the installation: " & vbCrLf & logContent
End If