From eecf96cf8b45c2db89c56ce32c12fa3601a223d0 Mon Sep 17 00:00:00 2001 From: Yorick Date: Wed, 16 Mar 2016 14:36:07 +0100 Subject: [PATCH] Add lock_dir function that delete GWX folder --- remove-kb.ps1 | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/remove-kb.ps1 b/remove-kb.ps1 index 1a44453..ac90d9f 100755 --- a/remove-kb.ps1 +++ b/remove-kb.ps1 @@ -22,6 +22,10 @@ $sheduledTasks=( @{name = "refreshgwxcontent"; directory = "\Microsoft\Windows\Setup\GWX"} ) +$gwx_dirs=( + "$env:windir\system32\GWX", +) + # You need to modify this variable whith administrator group name # Here for French Windows this is Administrateurs $adminGroup="Administrateurs" @@ -77,7 +81,7 @@ function hide_update() { $found = 1 if (!$update.IsHidden) { $update.IsHidden = "True" - Write-Host -ForegroundColor green "Hi dden" + Write-Host -ForegroundColor green "Hidden" } else { Write-Host -ForegroundColor Yellow "Already hidden" @@ -87,24 +91,31 @@ function hide_update() { if (!$found){ Write-Host -ForegroundColor Red "Not found" } } } +#Remove GWX Files (test) +function lock_dir { + param([string]$dir) + Write-Host -ForegroundColor white "removing $dir content ... " + takeown /F "$dir" /R /D $yes 2>&1 | Out-Null + icacls "$dir" /C /grant $adminGroup":F" /T 2>&1 | Out-Null + Try{ Remove-Item $dir\* -Force -Recurse -ErrorAction SilentlyContinue} + Catch {Write-Host -ForegroundColor Red "Some files can't be deleted."} + } Write-Host -Nonewline -ForegroundColor white "Searching for GWX process ... " if (Get-Process -name GWX -ErrorAction SilentlyContinue) { Write-Host -ForegroundColor DarkGreen -NoNewLine "Running " - Write-Host -Nonewline -ForegroundColor white "removing ... " + Write-Host -ForegroundColor white "removing ... " Try {Stop-Process -name GWX -Force -ErrorAction SilentlyContinue} Catch { Write-Host -ForegroundColor Red "Error"} } else { Write-Host -ForegroundColor Yellow ("Not running")} -#Remove GWX Files (test) -takeown /F "$env:WINDIR\System32\GWX" /R /D $yes 2>&1 | Out-Null -icacls "$env:WINDIR\System32\GWX" /C /grant $adminGroup":F" /T 2>&1 | Out-Null -Try{ Remove-Item $env:WINDIR\System32\GWX\* -Force -Recurse -ErrorAction SilentlyContinue} -Catch {Write-Host -ForegroundColor Red "Some files can't be deleted."} -#lock GWX directory -icacls "$env:WINDIR\System32\GWX" /deny Everyone:(CI)(OI)F 2>&1 | Out-Null +Write-Host -ForegroundColor white "`nRemoving and locking GWX folders ... " +$gwx_dirs | ForEach { + lock_dir $_ +} +Write-Host -ForegroundColor white "`nRemoving and locking GWX folders ... " Foreach($kbID in $kbIDs){ $kbNum = $kbID.Replace("KB","")