-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.ps1
61 lines (54 loc) · 4.35 KB
/
install.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
# Ensure the script is running with administrative privileges
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Host "Run as Administrator." -ForegroundColor Red
exit
}
# Temporarily bypass the execution policy
Set-ExecutionPolicy Bypass -Scope Process -Force
# Function to check if a command exists
function Command-Exists {
param ([string]$Command)
return Get-Command $Command -ErrorAction SilentlyContinue | Out-Null
}
# Variable to track if installations are needed
$installChocolatey = -not (Command-Exists 'choco')
$installMongoDB = -not (Command-Exists 'mongod') -and (Command-Exists 'choco')
$installMongosh = -not (Command-Exists 'mongosh') -and (Command-Exists 'choco')
# Install Chocolatey if needed
if ($installChocolatey) {
try {
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
$chocoInstallScript = (New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')
# Redirect all output to $Null
$Null = Invoke-Expression $chocoInstallScript *> $Null
} catch {
Write-Host "Installation error." -ForegroundColor Red
exit
}
}
# Install MongoDB if needed
if ($installMongoDB) {
try {
$Null = choco install mongodb -y *> $Null
} catch {
Write-Host "Installation error." -ForegroundColor Red
exit
}
}
# Install MongoDB Shell (mongosh) if needed
if ($installMongosh) {
try {
$Null = choco install mongosh -y *> $Null
} catch {
Write-Host "Installation error." -ForegroundColor Red
exit
}
}
# Final check
if ((-not $installChocolatey -or (Command-Exists 'choco')) -and
(-not $installMongoDB -or (Command-Exists 'mongod')) -and
(-not $installMongosh -or (Command-Exists 'mongosh'))) {
Write-Host "All required software is installed." -ForegroundColor Green
} else {
Write-Host "Some software is missing." -ForegroundColor Red
}