PowerShell/Scripts/install-unbound.ps1

59 lines
1.9 KiB
PowerShell
Raw Normal View History

2022-05-22 22:35:21 +02:00
<#
.SYNOPSIS
Installs Unbound (needs admin rights)
.DESCRIPTION
This PowerShell script installs Unbound, a validating, recursive, caching DNS resolver. It needs admin rights.
.EXAMPLE
PS> ./install-unbound
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
#Requires -RunAsAdministrator
try {
$StopWatch = [system.diagnostics.stopwatch]::startNew()
"⏳ Step 1/8: Updating package infos..."
2022-05-22 22:35:21 +02:00
& sudo apt update -y
if ($lastExitCode -ne "0") { throw "'sudo apt update' failed" }
"⏳ Step 2/8: Installing Unbound package..."
2022-05-22 22:35:21 +02:00
& sudo apt install unbound -y
if ($lastExitCode -ne "0") { throw "'sudo apt install unbound' failed" }
"⏳ Step 3/8: Setting up Unbound..."
2022-05-22 22:35:21 +02:00
& sudo unbound-control-setup
if ($lastExitCode -ne "0") { throw "'unbound-control-setup' failed" }
"⏳ Step 4/8: Updating DNSSEC Root Trust Anchors..."
2022-05-22 22:35:21 +02:00
& sudo unbound-anchor
if ($lastExitCode -ne "0") { throw "'unbound-anchor' failed" }
"⏳ Step 5/8: Checking config file..."
& unbound-checkconf "$PSScriptRoot/../Data/unbound.conf"
if ($lastExitCode -ne "0") { throw "'unbound-checkconf' failed - check the syntax" }
"⏳ Step 6/8: Copying config file to /etc/unbound/unbound.conf ..."
2022-05-22 22:35:21 +02:00
& sudo cp "$PSScriptRoot/../Data/unbound.conf" /etc/unbound/unbound.conf
if ($lastExitCode -ne "0") { throw "'cp' failed" }
"⏳ Step 7/8: (Re-)starting Unbound..."
2022-05-22 22:35:21 +02:00
& sudo unbound-control stop
& sudo unbound-control start
if ($lastExitCode -ne "0") { throw "'unbound-control start' failed" }
"⏳ Step 8/8: Checking Unbound status..."
2022-05-22 22:35:21 +02:00
& sudo unbound-control status
if ($lastExitCode -ne "0") { throw "'unbound-control status' failed" }
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ installed Unbound in $Elapsed sec"
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}