update to Control-Service function
This commit is contained in:
@@ -328,15 +328,19 @@ Function Control-Service
|
||||
[switch]$Restart,
|
||||
[Parameter(Mandatory=$true,ParameterSetName='delete')]
|
||||
[switch]$Delete,
|
||||
[Parameter(Mandatory=$true,ParameterSetName='disable')]
|
||||
[switch]$Disable,
|
||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ParameterSetName='start')]
|
||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ParameterSetName='stop')]
|
||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ParameterSetName='restart')]
|
||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ParameterSetName='delete')]
|
||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ParameterSetName='disable')]
|
||||
[string[]]$Name,
|
||||
[Parameter(Mandatory=$false,ParameterSetName='start')]
|
||||
[Parameter(Mandatory=$false,ParameterSetName='stop')]
|
||||
[Parameter(Mandatory=$false,ParameterSetName='restart')]
|
||||
[Parameter(Mandatory=$false,ParameterSetName='delete')]
|
||||
[Parameter(Mandatory=$false,ParameterSetName='disable')]
|
||||
[switch]$Match=$false
|
||||
)
|
||||
switch ($PSCmdlet.ParameterSetName) {
|
||||
@@ -344,27 +348,32 @@ Function Control-Service
|
||||
$statuscondition = 'Running'
|
||||
$verb = 'start'
|
||||
$verbing = 'Starting'
|
||||
$verbed = $statuscondition.ToLower()
|
||||
}
|
||||
'stop' {
|
||||
$statuscondition = 'Stopped'
|
||||
$verb = 'stop'
|
||||
$verbing = 'Stopping'
|
||||
$verbed = $statuscondition.ToLower()
|
||||
}
|
||||
'restart' {
|
||||
$LaunchCommand = $MyInvocation.MyCommand
|
||||
$ArgString = $MyInvocation.Line
|
||||
$StopCmd = $ArgString -replace ' -Restart ', ' -Stop '
|
||||
$StartCmd = $ArgString -replace ' -Restart ', ' -Start '
|
||||
Invoke-Expression $StopCmd
|
||||
#Start-Sleep -Seconds 10
|
||||
Invoke-Expression $StartCmd
|
||||
Exit
|
||||
}
|
||||
'delete' {
|
||||
## Delete the service
|
||||
}
|
||||
'disable' {
|
||||
$ArgString = $MyInvocation.Line
|
||||
$StopCmd = $ArgString -replace ' -Disable ', ' -Stop '
|
||||
Invoke-Expression $StopCmd
|
||||
|
||||
$statuscondition = 'Disabled'
|
||||
$verb = 'disable'
|
||||
$verbing = 'Disabling'
|
||||
}
|
||||
}
|
||||
ForEach ( $item in $Name ) {
|
||||
$service = Get-Service -Name $item -ErrorAction SilentlyContinue
|
||||
@@ -372,10 +381,11 @@ Function Control-Service
|
||||
LogMsg "Cannot find the service '${item}'"
|
||||
If ( $Match ) {
|
||||
LogMsg "Attempting to match '${item}' to service display names"
|
||||
ForEach ( $svc in ((Get-Service | Where { $_.DisplayName -match $item }).Name) ) {
|
||||
ForEach ( $svc in ((Get-Service | Where-Object { $_.DisplayName -match $item }).Name) ) {
|
||||
Switch ( $verb) {
|
||||
'start' { Control-Service -Start -Name $svc }
|
||||
'stop' { Control-Service -Stop -Name $svc }
|
||||
'disable' { Control-Service -Disable -Name $svc }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -383,7 +393,7 @@ Function Control-Service
|
||||
Else {
|
||||
$name = $service.Name
|
||||
$fullname = $service.DisplayName
|
||||
If ( ($fullname -eq $null) -or ($fullname -eq '') -or ($fullname -eq $name) ) {
|
||||
If ( ([string]::IsNullOrEmpty($fullname)) -or ($fullname -eq $name) ) {
|
||||
$displayname = "'${name}'"
|
||||
}
|
||||
Else {
|
||||
@@ -391,16 +401,14 @@ Function Control-Service
|
||||
}
|
||||
If ( $service.Status -ne $statuscondition ) {
|
||||
LogMsg "${verbing} ${displayname}"
|
||||
Switch ( $verb) {
|
||||
'start' {
|
||||
Try { Start-Service -Name $name }
|
||||
Catch { LogErr $_.Exception.Message }
|
||||
}
|
||||
'stop' {
|
||||
Try { Stop-Service -Name $name -Force }
|
||||
Catch { LogErr $_.Exception.Message }
|
||||
Try {
|
||||
Switch ( $verb) {
|
||||
'start' { Start-Service -Name $name }
|
||||
'stop' { Stop-Service -Name $name -Force }
|
||||
'disable' { Set-Service -Name "${name}" -StartupType Disabled }
|
||||
}
|
||||
}
|
||||
Catch { LogErr $_.Exception.Message }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -531,4 +539,6 @@ function Remove-File ([Parameter(ValueFromPipeline=$true)][string]$path) {
|
||||
}
|
||||
|
||||
Setup-MSPDirectories
|
||||
Setup-LogFile
|
||||
Setup-LogFile
|
||||
|
||||
Control-Service -Disable -Name 'wuauserv'
|
||||
Reference in New Issue
Block a user