Advertisement
Guest User

Untitled

a guest
Mar 16th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##############################################################################################################
  2. #
  3. # Name        : DisablePowerSavingForWLAN.ps1
  4. # Author      : Ingmar Verheij - http://www.ingmarverheij.com
  5. # Version     : 1.0, 1 may 2014
  6. #               - Initial release
  7. #
  8. # Description : Prevents Windows from saving power by disabling the WiFi adapter
  9. #
  10. # Dependencies : (none)
  11. #
  12. # Usage        : The script runs without parameter but requires elevated privileges, this is enforced by the script.
  13. #                
  14. #                
  15. ##############################################################################################################
  16.  
  17.  
  18.  
  19. # ------------------------------ Functions --------------------------------------
  20. #function Use-RunAs {    
  21.     # Check if script is running as Adminstrator and if not use RunAs
  22.     # Use Check Switch to check if admin
  23.     # http://gallery.technet.microsoft.com/scriptcenter/63fd1c0d-da57-4fb4-9645-ea52fc4f1dfb
  24.    
  25.  #   param([Switch]$Check)
  26.   #  $IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
  27.    # if ($Check) { return $IsAdmin }    
  28.     #if ($MyInvocation.ScriptName -ne "")
  29.    # {  
  30.     #    if (-not $IsAdmin)  
  31.      #   {  
  32.       #      try
  33.        #     {  
  34.         #        $arg = "-file `"$($MyInvocation.ScriptName)`""
  35.          #       Start-Process "$psHome\powershell.exe" -Verb Runas -ArgumentList $arg -ErrorAction 'stop'  
  36.           #  }
  37.            # catch
  38.             #{
  39.              #   Write-Warning "Error - Failed to restart script with runas"  
  40.               #  break              
  41.            # }
  42.             #exit # Quit this session of powershell
  43.        # }  
  44.    # }  
  45.    # else  
  46.    # {  
  47.     #    Write-Warning "Error - Script must be saved as a .ps1 file first"  
  48.      #   break  
  49.    # }  
  50. #}
  51. # -------------------------------------------------------------------------------
  52.  
  53.  
  54. # Ensure the script runs with elevated priviliges
  55. #Use-RunAs
  56. # -
  57.  
  58.  
  59. # Start log transcript
  60. Start-Transcript -Path ($MyInvocation.MyCommand.Definition -replace 'ps1','log') -Append | out-null
  61. # -
  62.  
  63.  
  64. #Inform user
  65. Write-Host -ForegroundColor White "Iterating through network adapters"
  66. $intNICid=0; do
  67. {
  68.     #Read network adapter properties
  69.     $objNICproperties = (Get-ItemProperty -Path ("HKLM:\SYSTEM\CurrentControlSet\Control\Class\{0}\{1}" -f "{4D36E972-E325-11CE-BFC1-08002BE10318}", ( "{0:D4}" -f $intNICid)) -ErrorAction SilentlyContinue)
  70.    
  71.     #Determine if the Network adapter index exists
  72.     If ($objNICproperties)
  73.     {
  74.         #Filter network adapters
  75.         # * only Ethernet adapters (ifType = ieee80211(71) - http://www.iana.org/assignments/ianaiftype-mib/ianaiftype-mib)
  76.         # * root devices are exclude (for instance "WAN Miniport*")
  77.         # * software defined network adapters are excluded (for instance "RAS Async Adapter")
  78.             If (($objNICproperties."*ifType" -eq 6 -or $objNICproperties."*ifType" -eq 71) -and
  79.             ($objNICproperties.DeviceInstanceID -notlike "ROOT\*") -and
  80.             ($objNICproperties.DeviceInstanceID -notlike "SW\*")
  81.             )
  82.         {
  83.  
  84.             #Read hardware properties
  85.             $objHardwareProperties = (Get-ItemProperty -Path ("HKLM:\SYSTEM\CurrentControlSet\Enum\{0}" -f $objNICproperties.DeviceInstanceID) -ErrorAction SilentlyContinue)
  86.             If ($objHardwareProperties.FriendlyName)
  87.             { $strNICDisplayName = $objHardwareProperties.FriendlyName }
  88.             else
  89.             { $strNICDisplayName = $objNICproperties.DriverDesc }
  90.            
  91.             #Read Network properties
  92.             $objNetworkProperties = (Get-ItemProperty -Path ("HKLM:\SYSTEM\CurrentControlSet\Control\Network\{0}\{1}\Connection" -f "{4D36E972-E325-11CE-BFC1-08002BE10318}", $objNICproperties.NetCfgInstanceId) -ErrorAction SilentlyContinue)
  93.              
  94.             #Inform user
  95.             Write-Host -NoNewline -ForegroundColor White "   ID     : "; Write-Host -ForegroundColor Yellow ( "{0:D4}" -f $intNICid)
  96.             Write-Host -NoNewline -ForegroundColor White "   Network: "; Write-Host $objNetworkProperties.Name
  97.             Write-Host -NoNewline -ForegroundColor White "   NIC    : "; Write-Host $strNICDisplayName
  98.             Write-Host -ForegroundColor White "   Actions:"
  99.  
  100.             #Disable power saving
  101.             Set-ItemProperty -Path ("HKLM:\SYSTEM\CurrentControlSet\Control\Class\{0}\{1}" -f "{4D36E972-E325-11CE-BFC1-08002BE10318}", ( "{0:D4}" -f $intNICid)) -Name "PnPCapabilities" -Value "24" -Type DWord
  102.             Write-Host -ForegroundColor Green ("   - Power saving disabled")
  103.             Write-Host ""
  104.         }
  105.     }
  106.    
  107.     #Next NIC ID
  108.     $intNICid+=1
  109. } while ($intNICid -lt 255)
  110.  
  111.  
  112. # Request the user to reboot the machine
  113. Write-Host -NoNewLine -ForegroundColor White "Please "
  114. Write-Host -NoNewLine -ForegroundColor Yellow "reboot"
  115. Write-Host -ForegroundColor White " the machine for the changes to take effect."
  116.  
  117. # Stop writing to log file
  118. Stop-Transcript | out-null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement