Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for restart_physicalnetworkadapters.ps

(view source code of restart_physicalnetworkadapters.ps as plain text)

  1. <#
  2. .SYNOPSIS
  3. Restart all physical network adapters
  4.  
  5. .DESCRIPTION
  6. If run with elevated privileges, this script will disable all physical network adapters on the local computer, wait for a specified number of seconds (default: 5), and then reenable all physical network adapters.
  7. If run without elevated privileges, this script will only show each physical network adapter's status.
  8.  
  9. .PARAMETER Delay
  10. The number of seconds between disabling/enabling the network card and checking the new status.
  11.  
  12. .PARAMETER Version
  13. Show this script's version number.
  14. If combined with -Verbose show full script path, version number and last/modified/release date.
  15.  
  16. .PARAMETER Verbose
  17. Show verbose version info when -Version switch is used.
  18.  
  19. .EXAMPLE
  20. . './Restart_PhysicalNetworkAdapters.ps1'
  21.  
  22. Output will look like this:
  23.  
  24. Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed
  25. ----                      --------------------                    ------- ------       ----------             ---------
  26. Ethernet                  NVIDIA nForce Networking Controller           6 Up           01-23-45-67-89-AB         1 Gbps
  27. Ethernet                  NVIDIA nForce Networking Controller           6 Disabled     01-23-45-67-89-AB         1 Gbps
  28. Ethernet                  NVIDIA nForce Networking Controller           6 Up           01-23-45-67-89-AB         1 Gbps
  29.  
  30. .LINK
  31. Script written by Rob van der Woude
  32. https://www.robvanderwoude.com/
  33. #>
  34.  
  35. param(
  36. 	[ValidateRange(5,60)]
  37. 	[Int]$Delay = 5,
  38. 	[switch]$Version,
  39. 	[switch]$Verbose
  40. )
  41.  
  42. $progver = '1.01'
  43.  
  44. if ( $Version ) {
  45. 	if ( $Verbose ) {
  46. 		$lastmod = ( [System.IO.File]::GetLastWriteTime( $PSCommandPath ) )
  47. 		if ( $lastmod.ToString( "h.mm" ) -eq $progver ) {
  48. 			"`"{0}`", Version {1}, release date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
  49. 		} else {
  50. 			# if last modified time is not equal to program version, the script has been tampered with
  51. 			"`"{0}`", Version {1}, last modified date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
  52. 		}
  53. 	} else {
  54. 		$progver
  55. 	}
  56. 	exit 0
  57. }
  58.  
  59. Get-NetAdapter -Physical
  60. if ( ( [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent( ) ).IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator ) ) {
  61. 	Get-NetAdapter -Physical | Disable-NetAdapter -AsJob | Out-Null
  62. 	Start-Sleep $Delay
  63. 	Get-NetAdapter -Physical
  64. 	Get-NetAdapter -Physical | Enable-NetAdapter -AsJob | Out-Null
  65. 	Start-Sleep $Delay
  66. 	Get-NetAdapter -Physical
  67. }

page last modified: 2024-04-16; loaded in 0.0223 seconds