From 545a63db036bcd830d3af93b40fc0efa0c844b9d Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Wed, 4 Apr 2018 23:30:08 +0200 Subject: [PATCH] Rework DisableService() --- cleanW10.ps1 | 54 +++++++++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/cleanW10.ps1 b/cleanW10.ps1 index eea54de..15df906 100755 --- a/cleanW10.ps1 +++ b/cleanW10.ps1 @@ -101,7 +101,7 @@ function FwBlockOutputIP { [object]$params ) if ( $params.ContainsKey('file') ) { - foreach ($line in Get-Content $params.file ){ FwBlockOutputIP @{"ip"="$line"} } + Get-Content $params.file | where { $_ -notmatch "^#.*$|^$" } | Foreach { FwBlockOutputIP @{ip=$_} } } elseif ( $params.ContainsKey('ip') ) { if (-not $params.ContainsKey('name') -or $params.name -eq "" ) { @@ -145,7 +145,7 @@ function FwBlockProgram { [object]$params ) if ( $params.ContainsKey('file') ) { - foreach ($line in Get-Content $params.file ){ FwBlockProgram @{"path"="$line"} } + Get-Content $params.file | where { $_ -notmatch "^#.*$|^$" } | Foreach { FwBlockProgram @{path=$_} } } elseif ( $params.ContainsKey('path') ) { $path = Invoke-Expression """$($params.path)""" @@ -188,9 +188,7 @@ function RemoveScheduledTask () { [object]$params ) if ( $params.ContainsKey('file') ) { - Get-Content $params.file | foreach { - RemoveScheduledTask @{name=$_} - } + Get-Content $params.file | where { $_ -notmatch "^#.*$|^$" } | foreach { RemoveScheduledTask @{name=$_} } } elseif ( $params.ContainsKey('name') ) { $command = "Get-ScheduledTask -ErrorAction Stop -TaskName `"$($params.name)`"" @@ -354,9 +352,7 @@ function DisableFeature { [object]$params ) if ( $params.ContainsKey('file') ) { - Get-Content $params.file | foreach { - DisableFeature @{name=$_} - } + Get-Content $params.file | where { $_ -notmatch "^#.*$|^$" } | foreach { DisableFeature @{name=$_} } } elseif ( $params.ContainsKey('name') ) { $feature = $(dism /online /Get-FeatureInfo /FeatureName:$($params.name) /English) @@ -397,11 +393,11 @@ function UninstallModernApp { ) if ( $params.ContainsKey('file') ) { $pkgs = $(Get-AppxPackage -AllUsers).name - $uninstall_list = Get-Content $params.file - $pkgs | Where-Object { $_ -in $uninstall_list } | foreach { + $uninstall_list = Get-Content $params.file | Where { $_ -notmatch "^#.*$|^$" } + $pkgs | Where-Object { $_ -in $uninstall_list } | Foreach { UninstallModernApp @{name=$_} } - $uninstall_list | Where-Object { $_ -notin $pkgs } | foreach { + $uninstall_list | Where-Object { $_ -notin $pkgs } | Foreach { Write-Host -ForegroundColor Yellow "`tModern App $_ not installed" } } @@ -440,11 +436,11 @@ function UninstallModernProvisonnedApp { if ( $params.ContainsKey('file') ) { $pkgs = $(Get-AppxProvisionedPackage -Online).DisplayName - $list = Get-Content $params.file - $pkgs | Where-Object { $_ -in $list } | foreach { + $list = Get-Content $params.file | Where { $_ -notmatch "^#.*$|^$" } + $pkgs | Where-Object { $_ -in $list } | Foreach { UninstallModernProvisonnedApp @{name=$_} } - $list | Where-Object { $_ -notin $pkgs } | foreach { + $list | Where-Object { $_ -notin $pkgs } | Foreach { Write-Host -ForegroundColor Yellow "`tProvisionned App $_ not found" } } @@ -478,8 +474,8 @@ function DisableService { [object]$params ) if ( $params.ContainsKey('file') ) { - $services = $(Get-Service).name - $list = Get-Content $params.file + $services = $(Get-Service).name + $list = Get-Content $params.file | Where { $_ -notmatch "^#.*$|^$" } $services | Where-Object { $_ -in $list } | Foreach { DisableService @{name=$_} } @@ -489,22 +485,24 @@ function DisableService { } } elseif ( $params.ContainsKey('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 + } try { - $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 - } - Stop-Service -InputObject $service -PassThru | Set-Service -StartupType disabled + + Stop-Service -InputObject $service + $service | Set-Service -StartupType disabled -ErrorAction Stop Write-Host -ForegroundColor Green "done" } catch { - Write-Host -NoNewLine -ForegroundColor Red "`tError`n`t" + Write-Host -NoNewLine -ForegroundColor Red "Error`n`t" write-Host -ForegroundColor DarkRed $Error[0].Exception.Message } finally {