Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for apipa.ps

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

  1. param(
  2. 	[parameter( ValueFromRemainingArguments = $true )]
  3. 	[string[]]$args # Leave all argument validation to the script, not to PowerShell
  4. )
  5.  
  6. function Show-Help {
  7. 	#
  8. 	# APIPA.ps1,  Version 1.00
  9. 	# Get or set the IP AutoConfiguration status (aka APIPA) for all network adapters
  10. 	#
  11. 	# Usage:  powershell  ./APIPA.ps1  [ newstatus ]
  12. 	#
  13. 	# Where:  newstatus   is either Enabled (or 1, On, Set, True or Yes),
  14. 	#                     or Disabled (or 0, Off, Reset, False or No).
  15. 	#
  16. 	# Notes:  Be careful when changing system settings in the registry; if you
  17. 	#         do not fully understand what this script does, then don't use it.
  18. 	#         More information on APIPA: http://www.tech-faq.com/apipa.html
  19. 	#
  20. 	# Written by Rob van der Woude
  21. 	# http://www.robvanderwoude.com
  22. 	#
  23.  
  24. 	Clear-Host
  25.  
  26. 	Write-Host
  27. 	Write-Host "APIPA.ps1,  Version 1.00"
  28.  
  29. 	Write-Host "Get or set the IP AutoConfiguration status (aka APIPA) for " -NoNewline
  30. 	Write-Host "all " -ForegroundColor White -NoNewline
  31. 	Write-Host "network adapters"
  32.  
  33. 	Write-Host
  34.  
  35. 	Write-Host "Usage:  " -NoNewline
  36. 	Write-Host "powershell  ./APIPA.ps1  [ newstatus ]" -ForegroundColor White
  37.  
  38. 	Write-Host
  39.  
  40. 	Write-Host "Where:  " -NoNewline
  41. 	Write-Host "newstatus   " -ForegroundColor White -NoNewline
  42. 	Write-Host "is either " -NoNewline
  43. 	Write-Host "Enabled " -ForegroundColor White -NoNewline
  44. 	Write-Host "(or " -NoNewline
  45. 	Write-Host "1" -ForegroundColor White -NoNewline
  46. 	Write-Host ", " -NoNewline
  47. 	Write-Host "On" -ForegroundColor White -NoNewline
  48. 	Write-Host ", " -NoNewline
  49. 	Write-Host "Set" -ForegroundColor White -NoNewline
  50. 	Write-Host ", " -NoNewline
  51. 	Write-Host "True " -ForegroundColor White -NoNewline
  52. 	Write-Host "or " -NoNewline
  53. 	Write-Host "Yes" -ForegroundColor White -NoNewline
  54. 	Write-Host "),"
  55.  
  56. 	Write-Host "                    or " -NoNewline
  57. 	Write-Host "Disabled " -ForegroundColor White -NoNewline
  58. 	Write-Host "(or " -NoNewline
  59. 	Write-Host "0" -ForegroundColor White -NoNewline
  60. 	Write-Host ", " -NoNewline
  61. 	Write-Host "Off" -ForegroundColor White -NoNewline
  62. 	Write-Host ", " -NoNewline
  63. 	Write-Host "Reset" -ForegroundColor White -NoNewline
  64. 	Write-Host ", " -NoNewline
  65. 	Write-Host "False " -ForegroundColor White -NoNewline
  66. 	Write-Host "or " -NoNewline
  67. 	Write-Host "No" -ForegroundColor White -NoNewline
  68. 	Write-Host ")."
  69.  
  70. 	Write-Host
  71. 	Write-Host "Notes:  Be careful when changing system settings in the registry; if you"
  72. 	Write-Host "        do not fully understand what this script does, then don't use it."
  73. 	Write-Host "        More information on APIPA: " -NoNewline
  74. 	Write-Host "http://www.tech-faq.com/apipa.html" -ForegroundColor DarkGray
  75. 	Write-Host
  76. 	Write-Host "Written by Rob van der Woude"
  77. 	Write-Host "http://www.robvanderwoude.com"
  78. 	Write-Host
  79.  
  80. 	Exit 1
  81. }
  82.  
  83. if ( ( $args.Length -gt 1 ) -or $h ) {
  84. 	Show-Help
  85. }
  86.  
  87. $action = ""
  88.  
  89. # Interpret the command line argument
  90. if ( $args.Length -eq 1 ) {
  91. 	switch ( $args[0] ) {
  92. 		"0"        { $action = "Off"; break }
  93. 		"1"        { $action = "On";  break }
  94. 		"Disable"  { $action = "Off"; break }
  95. 		"Disabled" { $action = "Off"; break }
  96. 		"Enable"   { $action = "On";  break }
  97. 		"Enabled"  { $action = "On";  break }
  98. 		"False"    { $action = "Off"; break }
  99. 		"No"       { $action = "Off"; break }
  100. 		"Off"      { $action = "Off"; break }
  101. 		"On"       { $action = "On";  break }
  102. 		"Reset"    { $action = "Off"; break }
  103. 		"Set"      { $action = "On";  break }
  104. 		"True"     { $action = "On";  break }
  105. 		"Yes"      { $action = "On";  break }
  106. 		default    { Show-Help;       break }
  107. 	}
  108. }
  109.  
  110. # Make sure the variable is not set
  111. Clear-Variable apipa -ErrorAction SilentlyContinue
  112.  
  113. $regpath = "HKLM:\SYSTEM\CurrentControlSet\services\Tcpip\Parameters"
  114.  
  115. # Read the registry value
  116. $IPParameters = ( Get-ItemProperty -Path $regpath -ErrorAction SilentlyContinue )
  117. $apipa = $IPParameters.IPAutoconfigurationEnabled
  118.  
  119. # Show current status before change
  120. Write-Host "Current APIPA Status: " -NoNewline
  121. [string]$apipa = $IPParameters.IPAutoconfigurationEnabled
  122. # switch( $apipa ) cannot be used here because it handles 0, null
  123. # and whitespace all as 0, so we'll have to use if..elseif..else
  124. if ( $apipa -eq "" ) {
  125. 	Write-Host "not set"  -ForegroundColor White
  126. } elseif ( $apipa -eq 0 ) {
  127. 	Write-Host "disabled" -ForegroundColor White
  128. } elseif ( $apipa -eq 1 ) {
  129. 	Write-Host "enabled"  -ForegroundColor White
  130. } else {
  131. 	Write-Host "unknown"  -ForegroundColor White
  132. }
  133.  
  134. # Change the registry value if requested
  135. if ( $action ) {
  136. 	if ( $action -eq "On" ) {
  137. 		$newvalue = 1
  138. 	}
  139. 	if ( $action -eq "Off" ) {
  140. 		$newvalue = 0
  141. 	}
  142.  
  143. 	# Set the new registry value
  144. 	New-ItemProperty -Path $regpath -Name "IPAutoconfigurationEnabled" -Value $newvalue -PropertyType DWORD -ErrorAction SilentlyContinue -ErrorVariable err | Out-Null
  145. 	if ( $err ) {
  146. 		if ( $err[0].CategoryInfo.Category -eq "ResourceExists" ) {
  147. 			# Clear the ResourceExists error
  148. 			$err.Clear( )
  149. 			# If the registry value already exists, just set it to its new value
  150. 			Set-ItemProperty -Path $regpath -Name "IPAutoconfigurationEnabled" -Value $newvalue -ErrorAction SilentlyContinue | Out-Null
  151. 		} else {
  152. 			# Abort on all errors except ResourceExists
  153. 			Write-Host "Error: " -ForegroundColor Red -NoNewline
  154. 			Write-Host $err[0].Exception.Message -ForegroundColor White
  155. 			Exit 1
  156. 		}
  157. 	}
  158.  
  159. 	# Reread the registry value
  160. 	$IPParameters = ( Get-ItemProperty -Path $regpath -ErrorAction SilentlyContinue )
  161. 	$apipa = $IPParameters.IPAutoconfigurationEnabled
  162.  
  163. 	# Show status after change
  164. 	Write-Host "Changed APIPA Status: " -NoNewline
  165. 	if ( $apipa -eq "" ) {
  166. 		Write-Host "not set"  -ForegroundColor White
  167. 	} elseif ( $apipa -eq 0 ) {
  168. 		Write-Host "disabled" -ForegroundColor White
  169. 	} elseif ( $apipa -eq 1 ) {
  170. 		Write-Host "enabled"  -ForegroundColor White
  171. 	} else {
  172. 		Write-Host "unknown"  -ForegroundColor White
  173. 	}
  174. }
  175.  

page last uploaded: 2017-08-21, 14:26