-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstaller.ps1
130 lines (108 loc) · 4.88 KB
/
installer.ps1
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
# Function to prompt user with a default value
function Get-UserInputWithDefault {
param (
[string]$PromptMessage,
[string]$DefaultValue
)
$user_input = Read-Host "$PromptMessage (Default as $DefaultValue)"
if (-not [string]::IsNullOrWhiteSpace($user_input)) {
Write-Host "Using: $user_input"
return $user_input
} else {
Write-Host "Using default value: $DefaultValue"
return $DefaultValue
}
}
# Ask for Program install path with default
$programInstallPath = Get-UserInputWithDefault "Enter the Program Install Path" "$env:LocalAppData\Programs\ActionLogger"
# Ask for Log output path with default
$logOutputPath = Get-UserInputWithDefault "Enter the Log Output Path" "$env:LocalAppData\ActionLogger\logs"
# Ask if should start on windows startup with default value Y
$startOnStartup = Get-UserInputWithDefault "Start on Windows Startup? [Y/n]" "Y"
# Initialize startup variables
$startupLinkPath = ""
$startupDir = ""
if ($startOnStartup -ieq "Y") {
# Ask for startup directory if starting on windows startup
$startupDir = Get-UserInputWithDefault "Enter the Startup Directory" "$env:AppData\Microsoft\Windows\Start Menu\Programs\Startup"
$startupLinkPath = "$startupDir\ActionLogger.lnk"
}
# Generate uninstaller script with forceful termination of actionlogger.exe
$uninstallerScriptPath = ".\uninstaller.ps1"
$uninstallScriptContent = @"
# Uninstaller script to remove program files and startup link
# Stop actionlogger.exe if running
if (Get-Process -Name actionlogger -ErrorAction SilentlyContinue) {
Write-Host "Stopping actionlogger"
Stop-Process -Name actionlogger -Force
# Sleep for 1 second to ensure the process is stopped
Start-Sleep -Seconds 1
}
# Remove Program Install Path
Remove-Item -Recurse -Force "$programInstallPath"
# Remove Startup Link if exists
if (Test-Path "$startupLinkPath") {
Remove-Item -Force "$startupLinkPath"
}
if (Test-Path "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\ActionLogger.lnk") {
Remove-Item -Force "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\ActionLogger.lnk"
}
Write-Host "ActionLogger uninstalled successfully."
"@
$uninstallScriptContent | Out-File -FilePath "$uninstallerScriptPath" -Encoding UTF8
# Start installation
# Stop the program
$processName = 'actionlogger'
$process = Get-Process -Name $processName -ErrorAction SilentlyContinue
if ($process) {
Write-Host "Stopping $processName"
Stop-Process -Name $processName -Force
}
# Check if the target folder exists
if (Test-Path "$programInstallPath") {
# Delete the folder and recreate it
Write-Host "Deleting older binary folder $programInstallPath"
Remove-Item -Path "$programInstallPath" -Recurse -Force
Write-Host "Creating new binary folder $programInstallPath"
New-Item -Path "$programInstallPath" -ItemType Directory | Out-Null
} else {
# Create the folder and its parent folders
Write-Host "Creating new binary folder $programInstallPath"
New-Item -Path "$programInstallPath" -ItemType Directory -Force | Out-Null
}
# Copy uninstaller script to programInstallPath
Write-Host "Copying uninstaller script to $programInstallPath"
Copy-Item -Path "$uninstallerScriptPath" -Destination "$programInstallPath" -Force
# Check if the log output folder exists. If not, create it
if (-not (Test-Path "$logOutputPath")) {
Write-Host "Creating log output folder $logOutputPath"
New-Item -Path "$logOutputPath" -ItemType Directory -Force | Out-Null
}
# Copy all files in folder to programInstallPath
$distFolderPath = ".\dist\actionlogger"
Write-Host "Copying all files in $distFolderPath to $programInstallPath"
Copy-Item -Path "$distFolderPath\*" -Destination "$programInstallPath" -Force -Recurse
# Copy vendor files (icon, blacklist rules) from ./vendor/* to the programInstallPath
Write-Host "Copying vendor files to $programInstallPath"
$vendorFolderPath = ".\vendor"
Copy-Item -Path "$vendorFolderPath\*" -Destination "$programInstallPath" -Force -Recurse
# Create shortcut for actionlogger.exe in the startup folder
$shell = New-Object -ComObject WScript.Shell
$shortcut = $shell.CreateShortcut($startupLinkPath)
$shortcut.TargetPath = "$programInstallPath\actionlogger.exe"
$shortcut.Save()
# Write path to config.txt
$configFilePath = "$programInstallPath\config.txt"
@"
$logOutputPath
"@ | Out-File -FilePath "$configFilePath" -Encoding UTF8
Write-Host "Setup completed. Configuration and uninstaller script have been created."
# Ask if should create start menu shortcut
$createStartMenuShortcut = Get-UserInputWithDefault "Create start menu shortcut? [Y/n]" "Y"
if ($createStartMenuShortcut -ieq "Y") {
# Create start menu shortcut under user profile
$shortcutPath = "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\ActionLogger.lnk"
$shortcut = $shell.CreateShortcut($shortcutPath)
$shortcut.TargetPath = "$programInstallPath\actionlogger.exe"
$shortcut.Save()
}