# Koppeling van de agents van Bouwbureau Nederland volledig sluiten en opruimen. # Werkt zowel online (irm pc.bbnld.nl/tunnel | iex) als lokaal (C:\BBN-Support\pc-inrichten\verwijder.ps1). # Pure ASCII voor Windows PowerShell 5.1 en PowerShell 7. $ErrorActionPreference = 'Continue' try { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor 3072 } catch { } function Test-IsAdminLok { $id = [Security.Principal.WindowsIdentity]::GetCurrent() (New-Object Security.Principal.WindowsPrincipal $id).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) } if (-not (Test-IsAdminLok)) { Write-Host 'Beheerdersrechten nodig om de toegang te verwijderen. Klik op Ja in het venster dat opent.' -ForegroundColor Yellow $lokaal = 'C:\BBN-Support\pc-inrichten\verwijder.ps1' if (-not (Test-Path $lokaal)) { $lokaal = Join-Path $env:TEMP 'bbn-pc-verwijder.ps1' Invoke-WebRequest -Uri 'https://pc.bbnld.nl/verwijder.ps1' -OutFile $lokaal -UseBasicParsing try { Unblock-File -Path $lokaal -ErrorAction SilentlyContinue } catch { } } Start-Process powershell.exe -Verb RunAs -ArgumentList @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', "`"$lokaal`"") exit } $libLokaal = 'C:\BBN-Support\pc-inrichten\lib.ps1' if (Test-Path $libLokaal) { . $libLokaal } else { Invoke-Expression (Invoke-RestMethod -Uri 'https://pc.bbnld.nl/bestanden/lib.ps1' -UseBasicParsing) } $ErrorActionPreference = 'Continue' $Host.UI.RawUI.WindowTitle = 'Toegang van Bouwbureau Nederland verwijderen' Write-Host '' Write-Host '=====================================================================' -ForegroundColor Yellow Write-Host ' Toegang van de agents van Bouwbureau Nederland verwijderen' -ForegroundColor Yellow Write-Host '=====================================================================' -ForegroundColor Yellow Write-Host '' if ($env:BBN_STIL -ne '1') { $a = Read-Host ' Typ STOP en druk op Enter om de koppeling volledig te sluiten' if ($a -ne 'STOP') { Write-Host ' Niets veranderd.' Read-Host ' Druk op Enter om te sluiten' | Out-Null exit } } $stand = Lees-Json $script:StandPad Kop 'OpenSSH-server' $andere = @() if (Test-Path $script:SleutelPad) { $pubPad = Join-Path $script:PakketMap 'ssh\servers.pub' $onze = @() if (Test-Path $pubPad) { $onze = @(Get-Content $pubPad -ErrorAction SilentlyContinue | Where-Object { $_ -match '^ssh-' } | ForEach-Object { ($_.Trim() -split '\s+')[1] }) } if (-not $onze.Count) { $onze = @('AAAAC3NzaC1lZDI1NTE5AAAAIKYFQ45UhhX1p90nScrKBlHGOe0+YtQyVKw5XN07wfJy') } $andere = @(Get-Content $script:SleutelPad | Where-Object { $r = $_; $r.Trim() -and -not ($onze | Where-Object { $r -like "*$_*" }) }) if ($andere.Count) { [IO.File]::WriteAllText($script:SleutelPad, (($andere -join "`n") + "`n"), (New-Object Text.ASCIIEncoding)) Goed "Onze sleutels weggehaald; $($andere.Count) andere sleutel(s) blijven staan" } else { Remove-Item $script:SleutelPad -Force -ErrorAction SilentlyContinue Goed 'Sleutels van onze servers weggehaald' } } if (-not $andere.Count) { if (Get-Service sshd -ErrorAction SilentlyContinue) { Stop-Service sshd -Force -ErrorAction SilentlyContinue Set-Service sshd -StartupType Disabled -ErrorAction SilentlyContinue Goed 'sshd gestopt en uitgezet' } if ($stand.sshdConfigBackup -and (Test-Path (Join-Path $script:SshData $stand.sshdConfigBackup))) { Copy-Item (Join-Path $script:SshData $stand.sshdConfigBackup) (Join-Path $script:SshData 'sshd_config') -Force Goed "Oude sshd_config teruggezet ($($stand.sshdConfigBackup))" } if ($stand.defaultShellVoorBbn) { New-ItemProperty -Path 'HKLM:\SOFTWARE\OpenSSH' -Name DefaultShell -Value $stand.defaultShellVoorBbn -PropertyType String -Force | Out-Null Goed "DefaultShell teruggezet ($($stand.defaultShellVoorBbn))" } if ($stand.sshdDoorOns -eq $true) { if ($stand.sshdBron -eq 'msi') { $prod = Get-CimInstance Win32_Product -Filter "Name LIKE 'OpenSSH%'" -ErrorAction SilentlyContinue | Select-Object -First 1 if ($prod) { Start-Process msiexec.exe -ArgumentList @('/x', $prod.IdentifyingNumber, '/qn', '/norestart') -Wait Goed 'OpenSSH (MSI) verwijderd' } } elseif ($stand.sshdBron -eq 'windows') { Remove-WindowsCapability -Online -Name 'OpenSSH.Server~~~~0.0.1.0' -ErrorAction SilentlyContinue | Out-Null Goed 'OpenSSH-server (Windows-onderdeel) verwijderd' } } } Kop 'Firewall' Get-NetFirewallRule -Name $script:RegelNaam -ErrorAction SilentlyContinue | Remove-NetFirewallRule -ErrorAction SilentlyContinue Goed "Regel $($script:RegelNaam) weggehaald" if (-not $andere.Count) { foreach ($n in @($stand.firewallUitgezet)) { if ($n) { Enable-NetFirewallRule -Name $n -ErrorAction SilentlyContinue Info "Oude regel weer aan: $n" } } } Kop 'Geplande taken en agentbrowser' Get-ScheduledTask -ErrorAction SilentlyContinue | Where-Object { $_.TaskName -like 'BBN agent-*' } | ForEach-Object { Unregister-ScheduledTask -TaskName $_.TaskName -Confirm:$false -ErrorAction SilentlyContinue Info "Taak weg: $($_.TaskName)" } Get-CimInstance Win32_Process -Filter "Name='chrome.exe' OR Name='msedge.exe'" -ErrorAction SilentlyContinue | Where-Object { $_.CommandLine -like '*BBN-Support\agent\browser*' -and $_.CommandLine -notlike '*--type=*' } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue } Goed 'Taken en agentbrowser gestopt' if ($stand.lokaalAccountDoorOns) { Kop 'Lokaal hulpaccount' $acc = [string]$stand.lokaalAccountDoorOns try { Remove-LocalUser -Name $acc -ErrorAction Stop Goed "Lokaal account '$acc' verwijderd" } catch { & net.exe user $acc /delete 2>$null | Out-Null Goed "Lokaal account '$acc' verwijderd" } $hideKey = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList' if (Test-Path $hideKey) { Remove-ItemProperty -Path $hideKey -Name $acc -ErrorAction SilentlyContinue } } Kop 'Tailscale' if (Test-Path $script:TsExe) { $afmelden = $true if ($env:BBN_STIL -ne '1') { $t = Read-Host ' Tailscale ook afmelden op deze pc? [Enter = Ja, typ N om te laten staan]' if ($t -match '^[nN]') { $afmelden = $false } } if ($afmelden) { Start-Native $script:TsExe @('logout') | Out-Null Goed 'Tailscale afgemeld' if ($stand.tailscaleDoorOns -eq $true) { $wegTs = $true if ($env:BBN_STIL -ne '1') { $u = Read-Host ' Tailscale ook de-installeren van deze pc? [Enter = Ja, typ N om te laten staan]' if ($u -match '^[nN]') { $wegTs = $false } } if ($wegTs) { $tsProd = Get-CimInstance Win32_Product -Filter "Name LIKE 'Tailscale%'" -ErrorAction SilentlyContinue | Select-Object -First 1 if ($tsProd) { Info 'Tailscale wordt verwijderd (even geduld)...' Start-Process msiexec.exe -ArgumentList @('/x', $tsProd.IdentifyingNumber, '/qn', '/norestart') -Wait Goed 'Tailscale volledig gede-installeerd' } else { Info 'Tailscale is afgemeld; het programma zelf kan eventueel weg via Instellingen > Apps.' } } } } else { Info 'Tailscale blijft verbonden (zonder ssh-server en sleutels kan de agent er niet meer in).' } } Remove-Item $script:PcJson -Force -ErrorAction SilentlyContinue Write-Host '' Write-Host '=====================================================================' -ForegroundColor Green Write-Host ' Klaar. De agents van Bouwbureau Nederland kunnen niet meer bij deze pc.' -ForegroundColor Green Write-Host ' Tip: zeg in de Machinekamer "werkpc ontkoppel " om hem uit de lijst te halen.' -ForegroundColor Green Write-Host '=====================================================================' -ForegroundColor Green if ($env:BBN_STIL -ne '1') { Write-Host '' Read-Host 'Druk op Enter om te sluiten' | Out-Null }