-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
add-host-entry.ps1
59 lines (50 loc) · 2.65 KB
/
add-host-entry.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
##**************************************************************************************
##**************************************************************************************
##**************************************************************************************
##**************************************************************************************
##************░█▀▀▄░▒█░░░░▒█▀▀█░▒█▀▀▀█░▒█▀▀▄░█▀▀▄░▒█▀▀▀█░▒█░▄▀░▒█▀▀▀░▀▀█▀▀**************
##************▒█▄▄█░▒█░░░░▒█░▄▄░▒█░░▒█░▒█▀▀▄▒█▄▄█░░▀▀▀▄▄░▒█▀▄░░▒█▀▀▀░░▒█░░**************
##************▒█░▒█░▒█▄▄█░▒█▄▄▀░▒█▄▄▄█░▒█▄▄█▒█░▒█░▒█▄▄▄█░▒█░▒█░▒█▄▄▄░░▒█░░**************
##**************************************************************************************
##************************** ▀▄▀▄▀▄GitHub - algobasket▄▀▄▀▄▀ ***************************
##**************************************************************************************
##******************************** Made By Algobasket ********************************
##**************************************************************************************
# Function to read .env file
function Get-EnvVarsFromFile {
param (
[string]$envFilePath
)
$envVars = @{}
if (Test-Path $envFilePath) {
$lines = Get-Content -Path $envFilePath
foreach ($line in $lines) {
if ($line -match '^\s*#') { continue } # Skip comments
if ($line -match '^\s*$') { continue } # Skip empty lines
if ($line -match '^\s*(\w+)\s*=\s*(.+)\s*$') {
$name = $matches[1]
$value = $matches[2]
$envVars[$name] = $value
}
}
}
return $envVars
}
# Path to .env file
$envFilePath = ".env"
# Get environment variables from .env file
$envVars = Get-EnvVarsFromFile -envFilePath $envFilePath
# Extract specific environment variables
$IP = $envVars["IP"]
$DOMAIN = $envVars["DOMAIN"]
$HOST_ENTRY = $envVars["HOST_ENTRY"]
# Path to hosts file
$hostsFile = "C:\Windows\System32\drivers\etc\hosts"
# Check if the entry already exists
if ((Get-Content $hostsFile) -notcontains $HOST_ENTRY) {
# Add the entry to the hosts file
Add-Content -Path $hostsFile -Value $HOST_ENTRY
Write-Output "Entry added to hosts file."
} else {
Write-Output "Entry already exists in hosts file."
}