Skip to content

Commit 2ba1ca3

Browse files
authored
Refactor: Convert script to native PowerShell for better efficiency (#325)
1 parent 596addd commit 2ba1ca3

1 file changed

Lines changed: 35 additions & 34 deletions

File tree

IPBanCore/Windows/Scripts/install_latest.ps1

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,24 @@ param
2626

2727
if ($PSVersionTable.PSVersion.Major -lt 5 -or ($PSVersionTable.PSVersion.Major -eq 5 -and $PSVersionTable.PSVersion.Minor -lt 1))
2828
{
29-
& echo "This script requires powershell 5.1 or greater"
29+
Write-Output "This script requires powershell 5.1 or greater"
3030
exit -1
3131
}
3232

33-
$INSTALL_PATH = "C:/Program Files/IPBan"
33+
$ProgressPreference = "SilentlyContinue"
34+
$INSTALL_PATH = "C:\Program Files\IPBan"
3435
$SERVICE_NAME = "IPBan"
3536
$ErrorActionPreference = "Stop"
3637
$tempPath = [System.IO.Path]::GetTempPath()
3738
[bool] $isUninstall = ($uninstall -eq "u" -or $uninstall -eq "uninstall")
3839

39-
$CONFIG_FILE = "$INSTALL_PATH/ipban.config"
40-
$INSTALL_EXE = "$INSTALL_PATH/DigitalRuby.IPBan.exe"
40+
$CONFIG_FILE = "$INSTALL_PATH\ipban.config"
41+
$INSTALL_EXE = "$INSTALL_PATH\DigitalRuby.IPBan.exe"
4142

4243
if (Get-Service $SERVICE_NAME -ErrorAction SilentlyContinue)
4344
{
4445
# create install path, ensure clean slate
45-
& echo "Removing existing service"
46+
Write-Output "Removing existing service"
4647
try
4748
{
4849
Stop-Service -Name $SERVICE_NAME -Force
@@ -55,45 +56,45 @@ if (Get-Service $SERVICE_NAME -ErrorAction SilentlyContinue)
5556

5657
if (Test-Path -Path $INSTALL_PATH)
5758
{
58-
& echo "Removing existing directory at $INSTALL_PATH"
59+
Write-Output "Removing existing directory at $INSTALL_PATH"
5960
if ($isUninstall -eq $False)
6061
{
61-
if (Test-Path "$INSTALL_PATH/ipban.config")
62+
if (Test-Path "$INSTALL_PATH\ipban.config")
6263
{
63-
copy "$INSTALL_PATH/ipban.config" $tempPath
64+
copy-item "$INSTALL_PATH\ipban.config" $tempPath
6465
}
65-
if (Test-Path "$INSTALL_PATH/ipban.override.config")
66+
if (Test-Path "$INSTALL_PATH\ipban.override.config")
6667
{
67-
copy "$INSTALL_PATH/ipban.override.config" $tempPath
68+
copy-item "$INSTALL_PATH\ipban.override.config" $tempPath
6869
}
69-
if (Test-Path "$INSTALL_PATH/ipban.sqlite")
70+
if (Test-Path "$INSTALL_PATH\ipban.sqlite")
7071
{
71-
copy "$INSTALL_PATH/ipban.sqlite" $tempPath
72+
copy-item "$INSTALL_PATH\ipban.sqlite" $tempPath
7273
}
73-
if (Test-Path "$INSTALL_PATH/nlog.config")
74+
if (Test-Path "$INSTALL_PATH\nlog.config")
7475
{
75-
copy "$INSTALL_PATH/nlog.config" $tempPath
76+
copy-item "$INSTALL_PATH\nlog.config" $tempPath
7677
}
7778
}
7879
else
7980
{
80-
& rm -r "$INSTALL_PATH"
81-
& echo "IPBan is fully uninstalled from this system"
81+
Remove-Item "$INSTALL_PATH" -Force -Recurse
82+
Write-Output "IPBan is fully uninstalled from this system"
8283
exit 0
8384
}
8485
}
8586

8687
# download zip file
87-
& mkdir -p $INSTALL_PATH -ErrorAction SilentlyContinue
88+
New-Item -Type Directory -path $INSTALL_PATH -ErrorAction SilentlyContinue
8889
$ReleaseAssets = Invoke-RestMethod "https://api.github.com/repos/DigitalRuby/IPBan/releases/latest"
8990
if ([System.Environment]::Is64BitOperatingSystem)
9091
{
9192
$url = ($ReleaseAssets.assets | ? name -Match "\-Windows\-x64").browser_download_url
9293
} else {
9394
$url = ($ReleaseAssets.assets | ? name -Match "\-Windows\-x86").browser_download_url
9495
}
95-
& echo "Downloading ipban from $Url"
96-
$ZipFile = "$INSTALL_PATH/IPBan.zip"
96+
Write-Output "Downloading ipban from $Url"
97+
$ZipFile = "$INSTALL_PATH\IPBan.zip"
9798

9899
# Forcing the Invoke-RestMethod PowerShell cmdlet to use TLS 1.2 to avoid error "The request was aborted: Could not create SSL/TLS secure channel."
99100
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
@@ -104,25 +105,25 @@ Expand-Archive -LiteralPath $ZipFile -DestinationPath $INSTALL_PATH -Force
104105
Remove-Item -Force $ZipFile
105106

106107
# copy back over the config and db file
107-
if (Test-Path -Path "$tempPath/ipban.config")
108+
if (Test-Path -Path "$tempPath\ipban.config")
108109
{
109-
& copy "$tempPath/ipban.config" "$INSTALL_PATH"
110-
& rm "$tempPath/ipban.config"
110+
copy-Item "$tempPath\ipban.config" "$INSTALL_PATH"
111+
remove-Item "$tempPath\ipban.config"
111112
}
112-
if (Test-Path -Path "$tempPath/ipban.override.config")
113+
if (Test-Path -Path "$tempPath\ipban.override.config")
113114
{
114-
& copy "$tempPath/ipban.override.config" "$INSTALL_PATH"
115-
& rm "$tempPath/ipban.override.config"
115+
copy-Item "$tempPath\ipban.override.config" "$INSTALL_PATH"
116+
remove-Item "$tempPath\ipban.override.config"
116117
}
117-
if (Test-Path -Path "$tempPath/ipban.sqlite")
118+
if (Test-Path -Path "$tempPath\ipban.sqlite")
118119
{
119-
& copy "$tempPath/ipban.sqlite" "$INSTALL_PATH"
120-
& rm "$tempPath/ipban.sqlite"
120+
copy-Item "$tempPath\ipban.sqlite" "$INSTALL_PATH"
121+
remove-Item "$tempPath\ipban.sqlite"
121122
}
122-
if (Test-Path -Path "$tempPath/nlog.config")
123+
if (Test-Path -Path "$tempPath\nlog.config")
123124
{
124-
& copy "$tempPath/nlog.config" "$INSTALL_PATH"
125-
& rm "$tempPath/nlog.config"
125+
copy-Item "$tempPath\nlog.config" "$INSTALL_PATH"
126+
remove-Item "$tempPath\nlog.config"
126127
}
127128

128129
# ensure audit policy is logging
@@ -135,16 +136,16 @@ if (Test-Path -Path "$tempPath/nlog.config")
135136
& sc.exe failure IPBAN reset= 9999 actions= "restart/60000/restart/60000/restart/60000"
136137
if ($autostart -eq $True)
137138
{
138-
& sc.exe start IPBAN
139+
Start-Service IPBAN
139140
}
140141
else
141142
{
142-
& echo "IPBAN Service is in stopped state, you must start it manually."
143+
Write-Output "IPBAN Service is in stopped state, you must start it manually."
143144
}
144145

145146
if ($silent -eq $False)
146147
{
147148
# open config
148-
& echo "Opening config file, make sure to whitelist your trusted ip addresses!"
149+
Write-Output "Opening config file, make sure to whitelist your trusted ip addresses!"
149150
& notepad $CONFIG_FILE
150151
}

0 commit comments

Comments
 (0)