diff --git a/cleanW10.ps1 b/cleanW10.ps1 index 7a4ee97..245ef20 100755 --- a/cleanW10.ps1 +++ b/cleanW10.ps1 @@ -1,9 +1,10 @@ #requires -RunAsAdministrator -Import-Module NetSecurity #Useful to manipulate firewall rules - +Import-Module NetSecurity #Useful to manipulate firewall rules +Set-StrictMode -Version 2 $HOST_FILE = "$env:windir\System32\drivers\etc\hosts" $HOST_IP = "0.0.0.0" -$ErrorActionPreference = "SilentlyContinue" +$ErrorActionPreference = "Continue" +$ProgressPreference = "SilentlyContinue" #Thanks to https://gist.github.com/markembling/173887 function BlockHost { @@ -15,10 +16,11 @@ function BlockHost { )] [object]$params ) - if ( $params.file ) { + if ( $params.ContainsKey('file') ) { + Foreach ($line in Get-Content $params.file ){ BlockHost -params @{host=$line} } } - if ( $params.host ) { + if ( $params.ContainsKey('host') ) { Write-Host -NoNewline "`t$($params.host) : " try { if ( ! $(IsHostAlreadyBlocked $HOST_FILE $params.host) ){ @@ -54,10 +56,10 @@ function FwBlockOutputIP { param( [object]$params ) - if ( $params.file ) { + if ( $params.ContainsKey('file') ) { foreach ($line in Get-Content $params.file ){ FwBlockOutputIP @{"ip"="$line"} } } - if ( $params.ip) { + elseif ( $params.ContainsKey('ip') ) { Write-Host -NoNewline "`t$($params.ip) : " if ( Get-NetFirewallRule -Name Blacklist_$($params.ip) -ErrorAction SilentlyContinue) { Write-Host -ForegroundColor Yellow "already blacklisted" @@ -74,27 +76,59 @@ function FwBlockOutputIP { Write-Host -ForegroundColor Green "done" } } + else { + Write-Host -ForegroundColor Red "`tError : No name or file for action $($MyInvocation.MyCommand.Name)" + } } -function DisablesheduledTask () { - param($taskList) - Foreach ($task in $taskList){ - Write-Host -NoNewline "`t$task : " - if ($PSVersionTable.PSVersion.Major -gt 2) { - if (Get-ScheduledTask -TaskName $task -ErrorAction SilentlyContinue) { - Write-Host -NoNewline -ForegroundColor DarkGreen "found! " - Write-Host -Nonewline -ForegroundColor white "removing : " - Try {Unregister-ScheduledTask -TaskName $task -ErrorAction SilentlyContinue -Confirm:$false} - Catch { - Write-Host -Nonewline -ForegroundColor Red "error" - } - Write-Host -ForegroundColor Green "done" - } - else { Write-Host -ForegroundColor Yellow "already removed"} +function RemoveScheduledTask () { + param ( + [cmdletbinding( + DefaultParameterSetName='params' + )] + [Parameter( + ValueFromPipeline=$False, + ParameterSetName="params", + Position = 0 + )] + [object]$params + ) + if ( $params.ContainsKey('file') ) { + Get-Content $params.file | foreach { + try { + $line = $_ + RemoveScheduledTask -task $( Get-ScheduledTask -TaskName $line -ErrorAction Stop) } - else { - Write-Host -ForegroundColor Red "damned! this is not Windows 10!" + catch [Microsoft.PowerShell.Cmdletization.Cim.CimJobException]{ + Write-Host -ForegroundColor Yellow "`tScheduled Task $line not found" } + catch { + Write-Host -NoNewline -ForegroundColor Red "`tError in RemoveSheduledTask`n`t" + Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message + } + } + } + elseif ( $params.ContainsKey('name') ) { + $command = "Get-ScheduledTask -ErrorAction Stop -TaskName `"$($params.name)`"" + if ($params.path) { + $command += " -TaskPath `"$($params.path)`"" + } + try { + $task = $(Invoke-Expression $command) + Write-Host -NoNewline "`tRemove task $($param.name) : " + $task | Unregister-ScheduledTask -ErrorAction SilentlyContinue -Confirm:$false + Write-Host -ForegroundColor Green "done" + } + catch [Microsoft.PowerShell.Cmdletization.Cim.CimJobException]{ + Write-Host -ForegroundColor Yellow "`tScheduled Task $($params.path)$($params.name) not found" + } + catch { + Write-Host -NoNewLine -ForegroundColor Red "`tError in RemoveSheduledTask`n`t" + Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message + } + } + else { + Write-Host -ForegroundColor Red "`tError : No name or file for action $($MyInvocation.MyCommand.Name)" } } @@ -103,28 +137,33 @@ function AddRegKey { [Parameter(Mandatory=$true)] [object]$params ) - Write-Host -NoNewline "`t$($params.key) reg key to $($params.value) : " - if ( -not $params.path -or -not $params.key -or -not $params.value ) { + if ( -not $params.ContainsKey('path') -or -not $params.ContainsKey('key') -or -not $params.ContainsKey('value') ) { Write-Host -ForegroundColor Red -NoNewline "Error in AddRegKey : no path, key or value" } - if ( -not $params.type ){ $params.type="DWORD" } + if ( -not $params.ContainsKey('type') -or $params.type -eq "" ){ $params.type="DWord" } + Write-Host -NoNewline "`t$($params.key) reg key to $($params.value) : " if ( -not (Test-Path $params.path) ){ Write-Host -NoNewline "- creating path - " - New-Item -Path $params.path -Force | Out-Null + try { + New-Item -Path $params.path -Force | Out-Null + } + catch { + Write-Host -NoNewLine -ForegroundColor Red "Error`n`t" + Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message + return + } } try { - Set-ItemProperty -Path $params.path -Name $params.key -Value $params.value -Type $params.type -Force + Set-ItemProperty -Path $params.path -Name $params.key -Value $($params.value) -Type $params.type -Force + Write-Host -ForegroundColor Green "done" } catch [System.Security.SecurityException]{ - Write-Host -ForegroundColor Red "access denied" - return + Write-Host -ForegroundColor Red "Error (access denied)" } catch { Write-Host -NoNewLine -ForegroundColor Red "`tError`n`t" - write-Host -ForegroundColor DarkRed $Error[0].Exception.Message - return + Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message } - Write-host -ForegroundColor Green "done" } function DelRegKey { @@ -139,18 +178,16 @@ function DelRegKey { } try { Remove-ItemProperty -Path $path -Name $key + Write-host -ForegroundColor Green "done" } catch [System.Security.SecurityException]{ Write-Host -ForegroundColor Red "Error in DelRegKey`n`t" Write-Host -ForegounndColor DarkRed "Access to $($params.path)\$($params.key) denied" - return } catch { Write-Host -ForegroundColor Red -NoNewLine "Error in DelRegKey`n`t" Write-Host -ForegounndColor DarkRed $Error[0].Exception.Message - return } - Write-host -ForegroundColor Green "done" } function DisableFeature { @@ -163,38 +200,35 @@ function DisableFeature { ParameterSetName="params", Position = 0 )] - [object]$params, - - [Parameter( - ValueFromPipeline=$True, - ParameterSetName="feature", - Position = 0 - )] - [Object]$feature + [object]$params ) - if ( $params.file ) { + if ( $params.ContainsKey('file') ) { Get-Content $params.file | foreach { - DisableFeature -feature $(dism /online /Get-FeatureInfo /FeatureName:$_ /English) + DisableFeature @{name=$_} } } - elseif ( $params.name ) { - $(dism /online /Get-FeatureInfo /FeatureName:$($params.name) /English) | DisableFeature - } - elseif ( $feature ) { - try { - $name = $feature | Select-String "Feature Name" | %{($_ -split " : ")[1]} + elseif ( $params.ContainsKey('name') ) { + $feature = $(dism /online /Get-FeatureInfo /FeatureName:$($params.name) /English) + $name = $feature | Select-String "Feature Name" | %{($_ -split " : ")[1]} + if (-not $name){ + Write-Host -ForegroundColor Yellow "`tFeature $params.name not found" + return + } Write-Host -NoNewline "`tDisable Feature $name : " if ( $($feature | Select-String "state") -match "Disable" ){ Write-Host -ForegroundColor Yellow "already disable" return } + try { Dism /online /Disable-Feature /FeatureName:$name /NoRestart | Out-Null Write-Host -ForegroundColor Green "done" } catch { Write-Host -ForegroundColor Red "error" - Return - } + } + } + else { + Write-Host -ForegroundColor Red "`tError : No name or file for action $($MyInvocation.MyCommand.Name)" } } @@ -208,36 +242,34 @@ function UninstallModernApp { ParameterSetName="params", Position = 0 )] - [object]$params, - - [Parameter( - ValueFromPipeline=$True, - ParameterSetName="pkg", - Position = 0 - )] - [Object]$pkg + [object]$params ) - if ( $params.file ) { - Get-AppxPackage -AllUsers | Where-Object { $_.name -in $(Get-Content $params.file) } | foreach { - $_ | UninstallModernApp + if ( $params.ContainsKey('file') ) { + $pkgs = $(Get-AppxPackage -AllUsers).name + $uninstall_list = Get-Content $params.file + $pkgs | Where-Object { $_ -in $uninstall_list } | foreach { + UninstallModernApp @{name=$_} } + $uninstall_list | Where-Object { $_ -notin $pkgs } | foreach { + Write-Host -ForegroundColor Yellow "`tModern App $_ not installed" + } } - elseif ( $params.name ) { - $(Get-AppxPackage -AllUsers | Where-Object { $_.name -like "*$($params.name)*" } ) | UninstallModernApp - } - elseif ( $pkg ) { + elseif ( $params.ContainsKey('name') ) { + Write-Host -NoNewLine "`tUninstall $($params.name) :" try { - Write-Host -NoNewLine "`tUninstall $($pkg.Name) :" - $pkg | Remove-AppxPackage | Out-Null + Get-AppxPackage -AllUsers | Where-Object { $_.name -like "*$($params.name)*" } | Remove-AppxPackage -ErrorAction Continue -ErrorVariable $Error | Out-Null Write-Host -ForegroundColor Green "done" } - catch { - Write-Host -NoNewLine -ForegroundColor Red "`tError in UninstallModernApp`n`t" - write-Host -ForegroundColor DarkRed $Error[0].Exception.Message - return - } + catch { + Write-Host -NoNewLine -ForegroundColor Red "`tError `n`t" + write-Host -ForegroundColor DarkRed "Impossible to Uninstall, this app sees to be a system one." + } } - if ( $params.removeProvisionned ) { + else { + Write-Host -ForegroundColor Red "`tError : No name or file for action $($MyInvocation.MyCommand.Name)" + return + } + if ( $params.ContainsKey('removeProvisionned' ) ) { UninstallModernProvisonnedApp $params } } @@ -249,38 +281,37 @@ function UninstallModernProvisonnedApp { )] [Parameter( ValueFromPipeline=$False, - #ParameterSetName="params", + ParameterSetName="params", Position = 0 )] - [object]$params, - - [Parameter( - ValueFromPipeline=$True, - ParameterSetName="pkg", - Position = 0 - )] - [Object]$pkg + [object]$params ) - if ( $params.file ) { - Get-AppxProvisionedPackage -Online | Where-Object { $_.DisplayName -in $(Get-Content $params.file) } | foreach { - UninstallModernProvisonnedApp -pkg $_ + + if ( $params.ContainsKey('file') ) { + $pkgs = $(Get-AppxProvisionedPackage -Online).DisplayName + $list = Get-Content $params.file + $pkgs | Where-Object { $_ -in $list } | foreach { + UninstallModernProvisonnedApp @{name=$_} + } + $list | Where-Object { $_ -notin $pkgs } | foreach { + Write-Host -ForegroundColor Yellow "`tProvisionned App $_ not found" } } - elseif ( $params.name ) { - UninstallModernProvisonnedApp -pkg $(Get-AppxProvisionedPackage -Online | Where-Object { $_.DisplayName -like "*$($params.name)*" }) - } - elseif ( $pkg ) { + elseif ( $params.ContainsKey('name') ){ + Write-Host -NoNewLine "`tUninstall Provisonned $($params.name) :" try { - Write-Host -NoNewLine "`tUninstall Provisonned $($pkg.DisplayName) :" - $pkg | Remove-AppxProvisionedPackage -Online | Out-Null + $(Get-AppxProvisionedPackage -Online | Where-Object { $_.DisplayName -eq $($params.name) }) | Remove-AppxProvisionedPackage -Online | Out-Null Write-Host -ForegroundColor Green "done" } - catch { - Write-Host -NoNewLine -ForegroundColor Red "`tError in UninstallModernApp`n`t" + catch { + Write-Host -NoNewLine -ForegroundColor Red "`tError`n`t" write-Host -ForegroundColor DarkRed $Error[0].Exception.Message return } } + else { + Write-Host -ForegroundColor Red "`tError : No name or file for action $($MyInvocation.MyCommand.Name)" + } } function DisableService { @@ -293,25 +324,27 @@ function DisableService { ParameterSetName="params", Position = 0 )] - [object]$params, - - [Parameter( - ValueFromPipeline=$True, - ParameterSetName="service" - )] - [Object]$service + [object]$params ) - if ( $params.file ) { - Get-Service | Where-Object { $_.name -in $( Get-Content $params.file ) } | Foreach { - $_ | DisableService + if ( $params.ContainsKey('file') ) { + $services = $(Get-Service).name + $list = Get-Content $params.file + $services | Where-Object { $_ -in $list } | Foreach { + DisableService @{name=$_} + } + + $list | Where-Object { $_ -notin $services } | Foreach { + Write-Host -ForegroundColor Yellow "`t Service $_ not found" } } - elseif ( $params.name ) { - DisableService-service $(Get-Service -name $params.name) - } - elseif ( $service ) { + elseif ( $params.ContainsKey('name') ) { try { - Write-Host -NoNewline "`tDisable service $($service.name) : " + $service = Get-Service -Name $params.name + if ( -not $service ){ + Write-Host -ForegroundColor "`t Service $($params.name) not found" + return + } + Write-Host -NoNewline "`tDisable service $($params.name) : " if ( $service.StartType -eq "Disable") { Write-Host -ForegroundColor Yellow "already disabled" return @@ -320,11 +353,14 @@ function DisableService { Write-Host -ForegroundColor Green "done " } catch { - Write-Host -NoNewLine -ForegroundColor Red "`tError in DisableService`n`t" + Write-Host -NoNewLine -ForegroundColor Red "`tError`n`t" write-Host -ForegroundColor DarkRed $Error[0].Exception.Message return } } + else { + Write-Host -ForegroundColor Red "`tError : No name or file for action $($MyInvocation.MyCommand.Name)" + } } function KillProcess { @@ -332,6 +368,7 @@ function KillProcess { [cmdletbinding( DefaultParameterSetName='params' )] + [Parameter( ValueFromPipeline=$False, ParameterSetName="params", @@ -369,7 +406,7 @@ function DelFile { return } $command = "Remove-Item $command -ErrorAction SilentlyContinue -Force -Path `"$path`"" - if ( $params.recurse -eq $true ) { + if ( $params.ContainsKey('recurse') -and $params.recurse -eq $true ) { $command += "-Recurse" } try { @@ -377,7 +414,7 @@ function DelFile { Write-Host -ForegroundColor Green "done" } catch { - Write-Host -NoNewLine -ForegroundColor Red "`Error in DelFile`n`t" + Write-Host -NoNewLine -ForegroundColor Red "`Error`n`t" write-Host -ForegroundColor DarkRed $Error[0].Exception.Message } } @@ -415,46 +452,61 @@ Write-Output "_________________________________________`n" try { Write-Host -NoNewline "Mount Default user registery hive : " - reg load "hku\Default" "C:\Users\Default\NTUSER.DAT" | Out-Null + reg load "HKU\Default" "C:\Users\Default\NTUSER.DAT" | Out-Null New-PSDrive -PSProvider Registry -Root HKEY_USERS -Name HKU | Out-Null Write-Host -ForegroundColor Green "done" -} -catch { - Write-Host -NoNewline -ForegroundColor Red "Error`n`t" - Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message - exit 1 -} - -try { Write-Host -NoNewline "Mount HK_CLASSES_ROOT registery hive : " New-PSDrive -PSProvider Registry -Root HKEY_CLASSES_ROOT -Name HKCR | Out-Null + New-PSDrive -PSProvider Registry -Root HKEY_CURRENT_USER -Name HKCU | Out-Null Write-Host -ForegroundColor Green "done" } catch { - Write-Host -NoNewline -ForegroundColor Red "Error`n`t" + Write-Host -NoNewline -ForegroundColor Red "Error while mounting Registery`n`t" Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message - exit 1 + return } Get-ChildItem -Path $PSScriptRoot"\modules.d" -Filter "*.conf" | foreach { $module = "" - $module = Get-Content $_.FullName -Raw | ConvertFrom-Json - + try { + $module = Get-Content $_.FullName -Raw | ConvertFrom-Json + } + catch { + Write-Host -NoNewline -ForegroundColor Red "Error While Loading JSON : $_.FullName" + Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message + return + } Write-Host -ForegroundColor White "`nProcess Module $($module.name)" $module_dir = $_.Directory.FullName + "\" + $_.BaseName + "\" $module.actions | Foreach { $action_file = "" - $current_action = $_ + $current_action = @{} + foreach( $p in $_.psobject.properties.name ){ + $current_action[$p] = $_.$p + } # If action content a file element, need to test if file exist - if ( $_.file) { - $action_file = $module_dir + $_.file + if ( $current_action.ContainsKey('file')) { + $action_file = $module_dir + $current_action.file if ( -not (Test-Path $action_file) ) { Write-Host -ForegroundColor Red "`tError in $($module.name) : file $($_.file) not found" return } - $_.file = $action_file + $current_action.file = $action_file } # Invoke function - Invoke-Expression "$($_.action) `$_" + Invoke-Expression "$($_.action) `$current_action" } } +#Unmount Registery +try { + Write-Host -NoNewline "`nUnmount HKU and HKCR : " + Remove-PSDrive -Name HKCR + Remove-PSDrive -Name HKCU + Remove-PSDrive -Name HKU + reg unload "HKU\Default" | Out-Null + Write-Host -ForegroundColor Green "done" +} +catch { + Write-Host -NoNewline -ForegroundColor Red "Error`n`t" + Write-Host -ForegroundColor DarkRed $Error[0].Exception.Message +} \ No newline at end of file diff --git a/lib/tasks.txt b/lib/tasks.txt index e9005a3..5795c68 100755 --- a/lib/tasks.txt +++ b/lib/tasks.txt @@ -9,3 +9,4 @@ Microsoft-Windows-DiskDiagnosticDataCollector DmClient MNO Metadata Parser QueueReporting +Metadata Refresh \ No newline at end of file diff --git a/modules.d/DelModernApp/apps.txt b/modules.d/DelModernApp/apps.txt index d954926..a9255b0 100644 --- a/modules.d/DelModernApp/apps.txt +++ b/modules.d/DelModernApp/apps.txt @@ -43,7 +43,7 @@ king.com.CandyCrushSodaSaga f5.vpn.client SonicWALL.MobileConnect Microsoft.BingMaps -Microsoft.XboxLIVEGame +Microsoft.XboxLIVEGames Microsoft.Reader Microsoft.WindowsReadingList Microsoft.WindowsScan \ No newline at end of file diff --git a/modules.d/DisableServices/features.txt b/modules.d/DisableServices/features.txt deleted file mode 100644 index f50f2f1..0000000 --- a/modules.d/DisableServices/features.txt +++ /dev/null @@ -1,4 +0,0 @@ -Internet-Explorer-Optional-amd64 -FaxServicesClientPackage -WindowsMediaPlayer -MediaPlayback \ No newline at end of file diff --git a/modules.d/UninstallOnedrive.conf b/modules.d/UninstallOnedrive.conf index 728e879..984c978 100644 --- a/modules.d/UninstallOnedrive.conf +++ b/modules.d/UninstallOnedrive.conf @@ -8,7 +8,7 @@ }, { "action" : "KillProcess", - "name" : "git" + "name" : "explorer" }, { "_comment" : "OneDrive Uninstaller x64 version", @@ -51,13 +51,6 @@ "path" : "HKLM:\\Software\\Policies\\Microsoft\\Windows\\OneDrive", "type" : "" }, - { - "action" : "AddRegKey", - "value" : "0300000021B9DEB396D7D001", - "key" : "OneDrive", - "path" : "HKCU:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StartupApproved\\Run", - "type" : "Binary" - }, { "action" : "AddRegKey", "value" : "0", diff --git a/modules.d/disable/BlockHosts.conf b/modules.d/disable/BlockHosts.conf deleted file mode 100644 index 1909b97..0000000 --- a/modules.d/disable/BlockHosts.conf +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name" : "Block unwanted Host", - "description" : "This module block some hosts from Microsoft", - "actions" : [ - { - "action" : "BlockHost", - "file" : "hosts.txt", - "host" : "" - } - ] -} \ No newline at end of file diff --git a/modules.d/disable/BlockIP.conf b/modules.d/disable/BlockIP.conf deleted file mode 100644 index 37328cd..0000000 --- a/modules.d/disable/BlockIP.conf +++ /dev/null @@ -1,12 +0,0 @@ -{ - "name" : "Block IP From MS servers", - "description" : "Disable Advertising", - "actions" : [ - { - "action" : "FwBlockOutputIP", - "ip" : "", - "file" : "ip.txt" - } - ] - -} \ No newline at end of file diff --git a/modules.d/disable/DelModernApp.conf b/modules.d/disable/DelModernApp.conf deleted file mode 100644 index 39ee397..0000000 --- a/modules.d/disable/DelModernApp.conf +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name" : "Delete Metro App", - "description" : "This module delete all useless modern app", - "actions" : [ - { - "action" : "UninstallModernApp", - "file" : "apps.txt", - "removeProvisionned" : "true" - } - ] -} \ No newline at end of file diff --git a/modules.d/disable/DisableAdvertising.conf b/modules.d/disable/DisableAdvertising.conf deleted file mode 100644 index 6cb4cef..0000000 --- a/modules.d/disable/DisableAdvertising.conf +++ /dev/null @@ -1,14 +0,0 @@ -{ - "name" : "Disable Advertising", - "description" : "Disable Advertising", - "actions" : [ - { - "action" : "AddRegKey", - "value" : "1", - "key" : "DisabledByGroupPolicy", - "path" : "HKLM:\\SOFTWARE\\Policies\\Microsoft\\Windows\\AdvertisingInfo", - "type" : "" - } - ] - -} \ No newline at end of file diff --git a/modules.d/disable/DisableFeatures.conf b/modules.d/disable/DisableFeatures.conf deleted file mode 100644 index 36922ea..0000000 --- a/modules.d/disable/DisableFeatures.conf +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name" : "Disable Features", - "description" : "This module disable some useless Windows Features", - "actions" : [ - { - "action" : "DisableFeature", - "file" : "features.txt", - "name" : "" - } - ] -} \ No newline at end of file diff --git a/modules.d/disable/DisableServices.conf b/modules.d/disable/DisableServices.conf deleted file mode 100644 index 947f01d..0000000 --- a/modules.d/disable/DisableServices.conf +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name" : "Disable Service", - "description" : "This module delete services known to send data to Microsoft", - "actions" : [ - { - "action" : "DisableService", - "file" : "services.txt", - "name" : "" - } - ] -} \ No newline at end of file