tobitobsen Powershell Script: Toggle LAN WLAN

Created by tobitobsen last modified
# Running this script with elevated previleges
# The following is taken from SO: by Andrew Odri

# Get the ID and security principal of the current user account
$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();
$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);

# Get the security principal for the administrator role
$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;

# Check to see if we are currently running as an administrator
if ($myWindowsPrincipal.IsInRole($adminRole))
    # We are running as an administrator, so change the title and background colour to indicate this
    $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)";
    $Host.UI.RawUI.BackgroundColor = "DarkBlue";
	#clear the screen
    # We are not running as an administrator, so relaunch as administrator
    # Create a new process object that starts PowerShell
    $newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell";

    # Specify the current script path and name as a parameter with added scope and support for scripts with spaces in it's path
    $newProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'"  + " -ExecutionPolicy ByPass"
    # Indicate that the process should be elevated
    $newProcess.Verb = "runas";

    # Start the new process

    # Exit from the current, unelevated, process

# We are now running with elevated rights
# Useful: check first with the following command if there are only two interfaces available: 
# Get-NetAdapter | Format-Table -a Status,Name,ifIndex

# get the adapters, sorting "up"-Adapters first.
$adapters = Get-NetAdapter | Sort-Object Status -descending 
foreach ($adapter in $adapters) 
	if($adapter.Status -eq "Up") 
        Disable-NetAdapter -InputObject $adapter -Confirm:$false 
        Write-Host "Disabled: " $adapter.Name
	elseif(($adapter.Status -eq "Disconnected") -or ($adapter.Status -eq "Disabled") -or ($adapter.Status -eq "Not Present"))
        Enable-NetAdapter -InputObject $adapter -Confirm:$false
        Write-Host "Enabled: " $adapter.Name
Write-Host "Error: " $_.Exception.Message
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Comments (0)