Guest User

Set-InterfaceOrderAndNetworkOrder.ps1

a guest
Jul 25th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $remote = 0
  2. $computer = "localhost"
  3.  
  4. if($remote -eq 1){
  5.     $cred = Get-Credential #Domain\User
  6.     Enter-PSSession $computer -Credential $cred
  7. }
  8.  
  9. #Settings
  10. $pInfo = "mydomain.com"
  11. $pCheck = "Domain" # could change this to DefaultGateway
  12.  
  13.  
  14. Push-Location
  15. Set-Location hklm:\SYSTEM\CurrentControlSet\Control\NetworkProvider\Order\
  16.  
  17. Write-Host "Reg Before: " (Get-ItemProperty '.\' -Name ProviderOrder).ProviderOrder
  18. # Variables
  19. $order = (Get-ItemProperty '.\' -Name ProviderOrder).ProviderOrder
  20. $topItem = “LanmanWorkstation”
  21. $strNewProvs = $topItem
  22. $i = 1
  23.  
  24. #Split and check if LanmanWorkstation is at the top
  25. $order.Split(",") | ForEach {
  26.     #Check if we are at the start
  27.     if($i -eq 1){
  28.         #Check if LanmanWorkstation is first in the list
  29.         if($_ -eq $topItem){
  30.             Write-Host "Exiting: $_ is at the top"
  31.             #Exit the script, someone else did our job
  32.             Exit
  33.         }
  34.     }
  35.     #Skip LanmanWorkstation
  36.     if($_ -ne $topItem){
  37.         $i= $i + 1
  38.         $strNewProvs = $strNewProvs +,+ $_
  39.         #Debug
  40.         #Write-Host "$_ is a token"
  41.     }
  42. }
  43.  
  44. Write-Host "Attempting to apply to registry: " $strNewProvs
  45.  
  46. # Write the new string back to the registry
  47. # Note the user running this script needs the needed access to the registry, ie Administrator
  48. Set-ItemProperty '.\' -Name ProviderOrder -Value $strNewProvs
  49.  
  50.  
  51. Write-Host "Reg After: " (Get-ItemProperty '.\' -Name ProviderOrder).ProviderOrder
  52.  
  53.  
  54.  
  55.  
  56. #Script
  57. $outer = ""
  58. $gotit = 0
  59. Set-Location hklm:\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces
  60. Get-ChildItem '.\' | foreach {
  61.     $inter = $_.Name.Split("\")[-1]
  62.     cd $inter
  63.     $varCheck = (Get-ItemProperty ".\" -Name $pCheck).$pCheck
  64.     cd ..
  65.     if ($varCheck -eq $pInfo) {
  66.         $outer = $inter
  67.         $gotit = 1
  68.     }
  69. }
  70.  
  71. #Check if we got our interface, if not then we quit.
  72. if ($gotit -eq 0) {
  73.     write-host "Well shit.. We didn't find $pInfo."
  74.     Exit
  75. }
  76.  
  77. #---------result = $outer
  78.  
  79. Set-Location hklm:\System\CurrentControlSet\Services\Tcpip\Linkage\
  80.  
  81. #----Bind
  82. $curBindOrder = (Get-ItemProperty '.\' -Name Bind).Bind
  83. $order = $curBindOrder
  84. $topItem = “\Device\$outer
  85. $strNewProvs = $topItem
  86. $i = 1
  87.  
  88. $order -Split([environment]::NewLine) | ForEach {
  89.     if($i -eq 1){
  90.         if($_ -eq $topItem){
  91.             Write-Host "Exiting: $_ is at the top"
  92.             Pop-Location
  93.             Exit
  94.         }
  95.     }
  96.     if($_ -ne $topItem){
  97.         $i= $i + 1
  98.         $strNewProvs = $strNewProvs + [environment]::NewLine + $_}
  99. }
  100. $newBindOrder = $strNewProvs
  101.  
  102. #----Export
  103. $curExportOrder = (Get-ItemProperty '.\' -Name Export).Export
  104. $order = $curExportOrder
  105. $topItem = “\Device\Tcpip_$outer
  106. $strNewProvs = $topItem
  107. $i = 1
  108.  
  109. $order -Split([environment]::NewLine) | ForEach {
  110.     if($i -eq 1){
  111.         if($_ -eq $topItem){
  112.             Write-Host "Exiting: $_ is at the top"
  113.             Pop-Location
  114.             Exit
  115.         }
  116.     }
  117.     if($_ -ne $topItem){
  118.         $i= $i + 1
  119.         $strNewProvs = $strNewProvs + [environment]::NewLine + $_}
  120. }
  121. $newExportOrder = $strNewProvs
  122.  
  123. #----Route
  124. $curRouteOrder = (Get-ItemProperty '.\' -Name Route).Route
  125. $order = $curRouteOrder
  126. $topItem = '"' + $outer + '"'
  127. $strNewProvs = $topItem
  128. $i = 1
  129. $newline = '"' + [environment]::NewLine + '"'
  130. $order -Split($newline) | ForEach {
  131.     if($i -eq 1){
  132.         if($_ -eq $topItem){
  133.             Write-Host "Exiting: $_ is at the top"
  134.             Pop-Location
  135.             Exit
  136.         }
  137.     }
  138.     if($_ -ne $topItem){
  139.     write-host $topItem
  140.         $i= $i + 1
  141.         $strNewProvs = $strNewProvs + [environment]::NewLine + $_}
  142. }
  143. $newRouteOrder = $strNewProvs
  144.  
  145. Write-Host $newBindOrder
  146. Write-Host $newExportOrder
  147. Write-Host $newRouteOrder
  148.  
  149.  
  150.  
  151. Set-ItemProperty '.\' -Name Bind -Value $newBindOrder
  152. Set-ItemProperty '.\' -Name Export -Value $newExportOrder
  153. Set-ItemProperty '.\' -Name Route -Value $newRouteOrder
  154.  
  155.  
  156. if($remote -eq 1){
  157.     Exit-PSSession
  158. }
  159. Pop-Location
Advertisement
Add Comment
Please, Sign In to add comment