(view source code of restart_ethernet.ps as plain text)
<#
.SYNOPSIS
Restart Ethernet network adapter
.DESCRIPTION
If run with elevated privileges, this script will disable the Ethernet network adapter on the local computer, wait for a specified number of seconds (default: 5), and then reenable the Ethernet network adapter; before and after each step the Ethernet network adapter's status is shown.
If run without elevated privileges, this script will only show the Ethernet network adapter's status.
.PARAMETER Delay
The number of seconds between disabling/enabling the network card and checking the new status
.PARAMETER Version
Show this script's version number.
If combined with -Verbose show full script path, version number and last/modified/release date.
.PARAMETER Verbose
Show verbose version info when -Version switch is used.
.EXAMPLE
. './Restart_Ethernet.ps1'
If run with elevated privileges, output will look like this:
Ethernet adapter status: Up
Ethernet adapter status: Disabled
Ethernet adapter status: Up
If run without elevated privileges, output will look like this:
Ethernet adapter status: Up
.LINK
script written by Rob van der Woude
https://www.robvanderwoude.com/
#>
param(
[ValidateRange(5,60)]
[Int]$Delay = 5,
[switch]$Version,
[switch]$Verbose
)
$progver = '1.01'
if ( $Version ) {
if ( $Verbose ) {
$lastmod = ( [System.IO.File]::GetLastWriteTime( $PSCommandPath ) )
if ( $lastmod.ToString( "h.mm" ) -eq $progver ) {
"`"{0}`", Version {1}, release date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
} else {
# if last modified time is not equal to program version, the script has been tampered with
"`"{0}`", Version {1}, last modified date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
}
} else {
$progver
}
exit 0
}
Write-Host ( "Ethernet adapter status: {0}" -f ( Get-NetAdapter -Name 'Ethernet' ).Status )
if ( ( [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent( ) ).IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator ) ) {
Write-Host "Disabling Ethernet adapter . . ."
Disable-NetAdapter -Name 'Ethernet' -AsJob | Out-Null
Start-Sleep $Delay
Write-Host ( "Ethernet adapter status: {0}" -f ( Get-NetAdapter -Name 'Ethernet' ).Status )
Write-Host "Enabling Ethernet adapter . . ."
Enable-NetAdapter -Name 'Ethernet' -AsJob | Out-Null
Start-Sleep $Delay
Write-Host ( "Ethernet adapter status: {0}" -f ( Get-NetAdapter -Name 'Ethernet' ).Status )
}
page last modified: 2024-04-16; loaded in 0.0074 seconds