-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_softwares.ps1
122 lines (103 loc) · 4.29 KB
/
install_softwares.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
param(
[string]$softwareListPath,
[switch]$upgrade,
[switch]$uninstallChocoIfInstalledByScript
)
# Convert relative path to full path
if (-not [System.IO.Path]::IsPathRooted($softwareListPath)) {
$softwareListPath = (Resolve-Path $softwareListPath).Path
}
# Determine the directory for log file creation
$logDirectory = Split-Path -Path $softwareListPath -Parent
$logFilePath = Join-Path -Path $logDirectory -ChildPath "installation_log.txt"
# Check if the script is running with admin privileges
function Ensure-AdminPrivileges {
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Host "Restarting script with admin privileges..."
# Restart as admin with original parameters
$arguments = "-NoExit -NoProfile -Command `"cd '$pwd'; & '$PSCommandPath'`""
if ($softwareListPath) { $arguments += " -softwareListPath `"$softwareListPath`"" }
if ($upgrade) { $arguments += " -upgrade" }
if ($uninstallChocoIfInstalledByScript) { $arguments += " -uninstallChocoIfInstalledByScript" }
Start-Process powershell -Verb runAs $arguments
exit
}
}
# Install Chocolatey if it's not already installed
function Install-Chocolatey {
if (-not (Get-Command choco -ErrorAction SilentlyContinue)) {
Write-Host "Installing Chocolatey..."
Set-ExecutionPolicy Bypass -Scope Process -Force
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
return $true # Chocolatey was installed by this script
}
return $false # Chocolatey was already installed
}
# Log messages to the log file
function Log-Message {
param (
[string]$message
)
Add-Content -Path $logFilePath -Value ((Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " - " + $message)
}
# Parse the software list file
function Parse-SoftwareList {
$sections = @{ }
$currentSection = ""
foreach ($line in Get-Content -Path $softwareListPath) {
$trimmedLine = $line.Trim()
if ($trimmedLine -match '^\[.+\]$') {
$currentSection = $trimmedLine.TrimStart('[').TrimEnd(']')
$sections[$currentSection] = @()
} elseif (-not [string]::IsNullOrWhiteSpace($trimmedLine) -and $trimmedLine -notmatch '^#') {
$sections[$currentSection] += $trimmedLine
}
}
return $sections
}
# Install software for a section
function Install-Section {
param (
[string]$sectionName,
[string[]]$softwareList
)
Write-Host "Installing section: $sectionName..."
Log-Message "Starting installation for section: $sectionName"
$softwareStr = $softwareList -join ' '
$chocoCommand = if ($upgrade) { "choco upgrade" } else { "choco install" }
$command = "$chocoCommand $softwareStr -y"
try {
$output = &cmd /c $command 2>&1 # Capture both stdout and stderr
Log-Message $output # Log the output of the Chocolatey command
Log-Message "Section $sectionName installed successfully."
} catch {
Log-Message "Error installing section $sectionName : $_"
}
}
function Main {
Ensure-AdminPrivileges
# Install Chocolatey if necessary
$chocoInstalledByScript = Install-Chocolatey
# Parse the software list
$sections = Parse-SoftwareList
Log-Message "================================================================================"
Log-Message "Starting installations of you softwares from $softwareListPath"
Log-Message "================================================================================"
# Install each section sequentially
foreach ($section in $sections.Keys) {
$softwareList = $sections[$section]
if ($softwareList.Count -gt 0) {
Install-Section -sectionName $section -softwareList $softwareList
}
}
# Display summary
Write-Host "Installation complete."
Write-Host "Check the log file for more details: $logFilePath"
# Optional: Uninstall Chocolatey if it was installed by this script
if ($uninstallChocoIfInstalledByScript -and $chocoInstalledByScript) {
Write-Host "Uninstalling Chocolatey..."
iex "choco uninstall chocolatey -y"
}
}
Main