-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoke-internet-outage.ps1
More file actions
49 lines (42 loc) · 1.73 KB
/
invoke-internet-outage.ps1
File metadata and controls
49 lines (42 loc) · 1.73 KB
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
# Requires -RunAsAdministrator
# Self-elevating PowerShell script to toggle a network adapter off for 3 seconds, then back on.
# --- Configuration ---
$adapter = "Ethernet 2"
function Ensure-Elevated {
$currentIdentity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($currentIdentity)
$isAdmin = $principal.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
if (-not $isAdmin) {
# Keep it simple: assume `pwsh` is on PATH and re-launch the script elevated.
$scriptPath = if ($PSCommandPath) { $PSCommandPath } else { $MyInvocation.MyCommand.Path }
$argList = @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', $scriptPath)
try {
Start-Process -FilePath pwsh -ArgumentList $argList -Verb RunAs -ErrorAction Stop
exit
} catch {
Write-Error "Elevation was canceled or failed. Exiting."
exit 1
}
}
}
function Main {
try {
Write-Host "Disabling adapter '$adapter'..." -ForegroundColor Yellow
Disable-NetAdapter -Name $adapter -Confirm:$false -ErrorAction Stop
Write-Host "Sleeping for 3 seconds..." -ForegroundColor Cyan
Start-Sleep -Seconds 3
} catch {
Write-Error "Error while disabling adapter: $($_.Exception.Message)"
} finally {
try {
Write-Host "Re-enabling adapter '$adapter'..." -ForegroundColor Yellow
Enable-NetAdapter -Name $adapter -ErrorAction Stop
Write-Host "Adapter re-enabled." -ForegroundColor Green
} catch {
Write-Error "Failed to re-enable adapter: $($_.Exception.Message)"
Write-Host "You may need to re-enable it manually: Enable-NetAdapter -Name `"$adapter`""
}
}
}
Ensure-Elevated
Main