Created
July 11, 2016 19:43
-
-
Save nsmr0604/b0c69da63abab551521b5b805a352204 to your computer and use it in GitHub Desktop.
今更ならがらWindows10へのアップグレード広告を切るスクリプト ref: http://qiita.com/nsmr0604@github/items/fbf26e91944117a3db3d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off && setlocal | |
set "cache=%temp%\%~nx0" | |
CLS | |
IF NOT EXIST "%cache%" ( | |
ECHO. | |
ECHO *********************************** | |
ECHO Caching scripts | |
ECHO *********************************** | |
copy "%~0" "%cache%" | |
) ELSE GOTO :is_cache | |
:is_cache | |
IF NOT "%~0"=="%cache%" ( | |
ECHO. | |
ECHO *********************************** | |
ECHO Run script form cache | |
ECHO *********************************** | |
@powershell -NoProfile -ExecutionPolicy unrestricted -Command "Start-Process %cache%" | |
exit /b | |
) ELSE goto :is_admin | |
:is_admin | |
date %date% >nul 2>&1 | |
IF NOT '%errorlevel%' == '0' ( | |
ECHO. | |
ECHO *********************************** | |
ECHO Invoking UAC to escalate privilege | |
ECHO *********************************** | |
@powershell -NoProfile -ExecutionPolicy unrestricted -Command "Start-Process %cache% -Verb runas" | |
exit /b | |
) ELSE goto :main | |
:main | |
CLS | |
ECHO. | |
ECHO *********************************** | |
ECHO MAIN SCRIPT START. | |
ECHO *********************************** | |
cd /d %temp% | |
ECHO. | |
ECHO *********************************** | |
ECHO Stop Windowns Update | |
ECHO *********************************** | |
net stop wuauserv | |
net stop cryptSvc | |
net stop bits | |
net stop msiserver | |
ECHO. | |
ECHO *********************************** | |
ECHO Killing GWX.exe and GWXUX.exe | |
ECHO *********************************** | |
taskkill /f /t /im gwx.exe | |
taskkill /f /t /im gwxux.exe | |
ECHO. | |
ECHO *********************************** | |
ECHO Disabling GWX and OSUpgrade | |
ECHO *********************************** | |
reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows\Gwx" /v "DisableGwx" /t REG_DWORD /d 00000001 /f | |
reg add "HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate" /v "DisableOSUpgrade" /t REG_DWORD /d 00000001 /f | |
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\OSUpgrade" /v "AllowOSUpgrade" /t REG_DWORD /d 0 /f | |
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\OSUpgrade" /v "ReservationsAllowed" /t REG_DWORD /d 0 /f | |
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\GWX.exe" /v "Debugger" /t REG_SZ /d "rundll32.exe" /f | |
ECHO. | |
ECHO *********************************** | |
ECHO Removing GWX Downloading cache | |
ECHO *********************************** | |
start /wait takeown /F C:\$Windows.~BT\* /R /A /D Y | |
start /wait icacls C:\$Windows.~BT\*.* /T /grant administrators:F | |
rmdir /S /Q C:\$Windows.~BT\ | |
start /wait takeown /F C:\$Windows.~WS\* /R /A /D Y | |
start /wait icacls C:\$Windows.~WS\*.* /T /grant administrators:F | |
rmdir /S /Q C:\$Windows.~WS\ | |
ECHO. | |
ECHO *********************************** | |
ECHO Restart Windowns Update | |
ECHO *********************************** | |
start /wait net start wuauserv | |
ECHO. | |
ECHO *********************************** | |
ECHO Uninstall and Hide KBs | |
ECHO *********************************** | |
@powershell "$s=[scriptblock]::create((gc \"%~f0\"|?{$_.readcount -gt 93})-join\"`n\");&$s" %* | |
exit /b | |
REM POWERSHELL SCRIPT BLOCK | |
$KBNumbers = 2876229,2952664,2976978,2977759,2990214,3021917,3022345,3035583,3044374,3068708,3075249,3080149,3123862,3150513,3173040 | |
[Boolean]$ErrFound = $false | |
$ComputerName = $env:COMPUTERNAME | |
# Uninstall | |
$hotfixes = Get-WmiObject -ComputerName $ComputerName -Class Win32_QuickFixEngineering | select hotfixid | |
Foreach ($KBNumber in $KBNumbers) { | |
Write-Host "Checking whether update $KBNumber needs to be uninstalled" | |
$KBID = "KB"+$KBNumber | |
if($hotfixes -match $KBID) { | |
Write-host "Found update $KBNumber. Uninstalling." | |
$UninstallString = "cmd.exe /c wusa.exe /uninstall /KB:$KBNumber /quiet /norestart" | |
Write-Host "Executing '($UninstallString)'" | |
([WMICLASS]"\\$ComputerName\ROOT\CIMV2:win32_process").Create($UninstallString) | out-null | |
while (@(Get-Process wusa -computername $ComputerName -ErrorAction SilentlyContinue).Count -ne 0) { | |
Start-Sleep 3 | |
Write-Host "Waiting for update removal to finish ..." | |
} | |
Write-Host "Completed the uninstallation of update $KBNumber" | |
} | |
else { | |
Write-Host "Update $KBNumber not installed so no uninstall needed" | |
} | |
Write-Host "-----------" | |
} # Foreach $KBNumber | |
# Hide (block) the update | |
try { | |
Write-Host "Listing Windows Update List..." | |
$UpdateSession = New-Object -ComObject Microsoft.Update.Session | |
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher() | |
$UpdateSearcher.IncludePotentiallySupersededUpdates = $true | |
$SearchResult = $UpdateSearcher.Search("IsInstalled=0") | |
Foreach ($KBNumber in $KBNumbers) { | |
Write-Host "Checking whether update $KBNumber needs to be hidden" | |
[Boolean]$KBListed = $false | |
Foreach ($Update in $SearchResult.updates) { | |
Foreach ($KBArticleID in $Update.KBArticleIDs) { | |
# Next line is for debugging | |
# Write-Host "$KBArticleID, $($Update.IsHidden), $($Update.title)" | |
if ($KBArticleID -eq $KBNumber) { | |
$KBListed = $true | |
if ($Update.IsHidden -eq $false) { | |
Write-Host "Hiding update $KBNumber (UpdateID $($Update.Identity.UpdateID), deployed $($Update.LastDeploymentChangeTime.ToString('MM/dd/yyyy')))" | |
$Update.IsHidden = $true | |
} else { | |
Write-Host "Update $KBNumber (UpdateID $($Update.Identity.UpdateID), deployed $($Update.LastDeploymentChangeTime.ToString('MM/dd/yyyy'))) is already hidden" | |
} # if $Update.IsHidden | |
} # if $KBArticleID -eq $KBNumber | |
} # Foreach $KBArticleID | |
} # Foreach $Update | |
if ($KBListed -eq $false) { | |
Write-Host "Update $KBNumber was not found searching Windows Update" | |
} | |
Write-Host "-----------" | |
} # Foreach $KBNumber | |
Write-Host "" | |
$objAutoUpdateSettings = (New-Object -ComObject "Microsoft.Update.AutoUpdate").Settings | |
$objSysInfo = New-Object -ComObject "Microsoft.Update.SystemInfo" | |
if ($objSysInfo.RebootRequired) { | |
Write-Host "A reboot is required to complete the process" | |
} else { | |
Write-Host "No reboot is required" | |
} | |
Write-Host "" | |
Write-Host "Script execution succeeded" | |
$ExitCode = 0 | |
} | |
catch { | |
Write-Host "" | |
$error[0] | |
Write-Host "" | |
Write-Host "Hiding update(s) failed" | |
$ExitCode = 1001 | |
} | |
Write-Host "Finished pathing..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment