Rework DisableService()

This commit is contained in:
Yorick Barbanneau 2018-04-04 23:30:08 +02:00
parent a4df335b49
commit 545a63db03

View file

@ -101,7 +101,7 @@ function FwBlockOutputIP {
[object]$params [object]$params
) )
if ( $params.ContainsKey('file') ) { 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') ) { elseif ( $params.ContainsKey('ip') ) {
if (-not $params.ContainsKey('name') -or $params.name -eq "" ) { if (-not $params.ContainsKey('name') -or $params.name -eq "" ) {
@ -145,7 +145,7 @@ function FwBlockProgram {
[object]$params [object]$params
) )
if ( $params.ContainsKey('file') ) { 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') ) { elseif ( $params.ContainsKey('path') ) {
$path = Invoke-Expression """$($params.path)""" $path = Invoke-Expression """$($params.path)"""
@ -188,9 +188,7 @@ function RemoveScheduledTask () {
[object]$params [object]$params
) )
if ( $params.ContainsKey('file') ) { if ( $params.ContainsKey('file') ) {
Get-Content $params.file | foreach { Get-Content $params.file | where { $_ -notmatch "^#.*$|^$" } | foreach { RemoveScheduledTask @{name=$_} }
RemoveScheduledTask @{name=$_}
}
} }
elseif ( $params.ContainsKey('name') ) { elseif ( $params.ContainsKey('name') ) {
$command = "Get-ScheduledTask -ErrorAction Stop -TaskName `"$($params.name)`"" $command = "Get-ScheduledTask -ErrorAction Stop -TaskName `"$($params.name)`""
@ -354,9 +352,7 @@ function DisableFeature {
[object]$params [object]$params
) )
if ( $params.ContainsKey('file') ) { if ( $params.ContainsKey('file') ) {
Get-Content $params.file | foreach { Get-Content $params.file | where { $_ -notmatch "^#.*$|^$" } | foreach { DisableFeature @{name=$_} }
DisableFeature @{name=$_}
}
} }
elseif ( $params.ContainsKey('name') ) { elseif ( $params.ContainsKey('name') ) {
$feature = $(dism /online /Get-FeatureInfo /FeatureName:$($params.name) /English) $feature = $(dism /online /Get-FeatureInfo /FeatureName:$($params.name) /English)
@ -397,11 +393,11 @@ function UninstallModernApp {
) )
if ( $params.ContainsKey('file') ) { if ( $params.ContainsKey('file') ) {
$pkgs = $(Get-AppxPackage -AllUsers).name $pkgs = $(Get-AppxPackage -AllUsers).name
$uninstall_list = Get-Content $params.file $uninstall_list = Get-Content $params.file | Where { $_ -notmatch "^#.*$|^$" }
$pkgs | Where-Object { $_ -in $uninstall_list } | foreach { $pkgs | Where-Object { $_ -in $uninstall_list } | Foreach {
UninstallModernApp @{name=$_} UninstallModernApp @{name=$_}
} }
$uninstall_list | Where-Object { $_ -notin $pkgs } | foreach { $uninstall_list | Where-Object { $_ -notin $pkgs } | Foreach {
Write-Host -ForegroundColor Yellow "`tModern App $_ not installed" Write-Host -ForegroundColor Yellow "`tModern App $_ not installed"
} }
} }
@ -440,11 +436,11 @@ function UninstallModernProvisonnedApp {
if ( $params.ContainsKey('file') ) { if ( $params.ContainsKey('file') ) {
$pkgs = $(Get-AppxProvisionedPackage -Online).DisplayName $pkgs = $(Get-AppxProvisionedPackage -Online).DisplayName
$list = Get-Content $params.file $list = Get-Content $params.file | Where { $_ -notmatch "^#.*$|^$" }
$pkgs | Where-Object { $_ -in $list } | foreach { $pkgs | Where-Object { $_ -in $list } | Foreach {
UninstallModernProvisonnedApp @{name=$_} UninstallModernProvisonnedApp @{name=$_}
} }
$list | Where-Object { $_ -notin $pkgs } | foreach { $list | Where-Object { $_ -notin $pkgs } | Foreach {
Write-Host -ForegroundColor Yellow "`tProvisionned App $_ not found" Write-Host -ForegroundColor Yellow "`tProvisionned App $_ not found"
} }
} }
@ -479,7 +475,7 @@ function DisableService {
) )
if ( $params.ContainsKey('file') ) { if ( $params.ContainsKey('file') ) {
$services = $(Get-Service).name $services = $(Get-Service).name
$list = Get-Content $params.file $list = Get-Content $params.file | Where { $_ -notmatch "^#.*$|^$" }
$services | Where-Object { $_ -in $list } | Foreach { $services | Where-Object { $_ -in $list } | Foreach {
DisableService @{name=$_} DisableService @{name=$_}
} }
@ -489,7 +485,6 @@ function DisableService {
} }
} }
elseif ( $params.ContainsKey('name') ) { elseif ( $params.ContainsKey('name') ) {
try {
$service = Get-Service -Name $params.name $service = Get-Service -Name $params.name
if ( -not $service ){ if ( -not $service ){
Write-Host -ForegroundColor "`t Service $($params.name) not found" Write-Host -ForegroundColor "`t Service $($params.name) not found"
@ -500,11 +495,14 @@ function DisableService {
Write-Host -ForegroundColor Yellow "already disabled" Write-Host -ForegroundColor Yellow "already disabled"
return return
} }
Stop-Service -InputObject $service -PassThru | Set-Service -StartupType disabled try {
Stop-Service -InputObject $service
$service | Set-Service -StartupType disabled -ErrorAction Stop
Write-Host -ForegroundColor Green "done" Write-Host -ForegroundColor Green "done"
} }
catch { 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 write-Host -ForegroundColor DarkRed $Error[0].Exception.Message
} }
finally { finally {