New Page
<#
Praktijkleren Examenopdracht – PowerShell
Onderwerp: Subnetcalculator
Auteur: <jouw naam>
Datum: <datum>
Dit script berekent netwerkgegevens op basis van
een IP-adres en subnetmasker (CIDR).
#>
function Vraag-IPAdres {
$pogingen = 0 # variabele
while ($pogingen -lt 3) { # lus met limiet
$ip = Read-Host "Voer een IP-adres in (bijv. 192.168.1.10)"
$delen = $ip.Split(".")
if ($delen.Count -eq 4 -and ($delen | ForEach-Object { $_ -match '^\d+$' -and [int]$_ -le 255 })) {
return $ip
}
else {
$pogingen++
Write-Host "Ongeldig IP-adres. Poging $pogingen van 3." -ForegroundColor Red
}
}
Write-Host "Te veel foutieve pogingen." -ForegroundColor Yellow
return $null
}
function Vraag-Subnetmasker {
$cidr = Read-Host "Voer subnetmasker in (CIDR, bijv. 24)"
if ($cidr -match '^\d+$' -and [int]$cidr -ge 0 -and [int]$cidr -le 32) {
return [int]$cidr
}
else {
Write-Host "Ongeldig subnetmasker." -ForegroundColor Red
return $null
}
}
function Bereken-Hosts {
param (
[int]$CIDR
)
$hostBits = 32 - $CIDR
$hosts = [math]::Pow(2, $hostBits) - 2
return [int]$hosts
}
# Hoofdprogramma
$ip = Vraag-IPAdres
if ($null -eq $ip) { return }
$cidr = Vraag-Subnetmasker
if ($null -eq $cidr) { return }
$aantalHosts = Bereken-Hosts -CIDR $cidr
Write-Host "`nResultaat:"
Write-Host "IP-adres:" $ip
Write-Host "Subnetmasker: /$cidr"
Write-Host "Aantal mogelijke hosts:" $aantalHosts
No comments to display
No comments to display