param( [string] $Service = $(throw "Service required."), [string] $Server = $(throw "Server required."), [string] $DoThis = $(throw "DoThis required.") #throw exception if no value provided ) $svc = get-wmiobject Win32_Service -computer $Server -filter "Name='$Service'" if (!($svc)) { Write-host "ERROR: No such service" exit } switch ($DoThis) { "Start" { $Result = $Svc.StartService() } "Stop" { $Result = $Svc.StopService() } "Disable" { $Result = $Svc.ChangeStartMode("Disabled") } "Manual" { $Result = $Svc.ChangeStartMode("Manual") } "Auto" { $Result = $Svc.ChangeStartMode("Auto") } default { Write-host "Usage: -DoThis [Start, Stop, Disable, Manual, Auto]" } } switch ($Result.ReturnValue) { 0 { Write-host "Success" } 5 { Write-host "ERROR: Cannot Stop, Service already Stopped" } 10 { Write-host "ERROR: Cannot Start, Service already Started" } 14 { Write-host "ERROR: Cannot Start, Service is Disabled" } default { Write-Host "ERROR: Something else happened, sorry. Return val = " $Result.ReturnValue } }