Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, March 2, 2019 1:17 AM
I am trying to check for a service if it exists or not, if it exists i need to set the service StartupType to Automatic. I am doing the following
$service = Get-Service -name WIN
Write-Output ("Service exist return: [" +Service.ExitCode + "]")
if(!($service)){
$Process = Set-Service $service -StartupType Automatic
Write-Output ("Service auto start exist return: [" +Process.ExitCode + "]")
}
Is this the right way to check for a service and if exist make it auto start? Suggest me the right method.
All replies (8)
Saturday, March 2, 2019 2:00 AM
I'd recommend to wait for an answer there before you start crossposting your question to some other forums
Live long and prosper!
(79,108,97,102|%{[char]$_})-join''
Saturday, March 2, 2019 2:14 AM
I'd do it like this:
try
{
get-service alg -ErrorAction Stop | Out-Null
try
{
Set-Service alg -StartupType Automatic -ErrorAction Stop
Write-Output "Service exists, StartupType set"
}
catch
{
Write-Output "Service exists but failed to set StartupType"
}
}
catch
{
Write-Output "Service doesn't exist"
}
Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
Saturday, March 2, 2019 3:36 AM
This is all that is required:
try {
Set-Service alg -StartupType Automatic -ErrorAction Stop
Write-Host 'Service exists, StartupType set' -fore green
}
catch{
Write-Host $_ -fore Red
}
Alter ate simple method:
if($svc = get-service alg -ErrorAction SilentlyContinue){
Set-Service $svc -StartupType Automatic
}
\(ツ)_/
Saturday, March 2, 2019 8:26 PM
I agree about the "required", but the OP seemed to want some feedback (perhaps to learn?) about whether the service existed, whether it could (or couldn't) be set to start automatically, or if the action was successful. So, between "reauired" and "wanted" I went with "wanted". :-)
Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
Saturday, March 2, 2019 9:00 PM
I agree about the "required", but the OP seemed to want some feedback (perhaps to learn?) about whether the service existed, whether it could (or couldn't) be set to start automatically, or if the action was successful. So, between "reauired" and "wanted" I went with "wanted". :-)
Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
My first method does that. The second also does that.
The value of using an exception can be that it is useful within a larger context. It is also simpler to code.
To get the status we would do this:
if($svc = get-service alg -ErrorAction SilentlyContinue){
Set-Service $svc -StartupType Automatic
}
$svc.Refresh()
The advantage here is that we get both the full detailed error and the service object. "Refresh()" re-gets the service status so we can see any effect.
The suggestions are alternate forms.
Your method is not wrong or bad but does more steps than necessary. Also my first method does not interpret the error but returns the exact error. Your method does not tells us why it failed it only notes that the action failed. Your method also sends plain text into the pipeline. This makes the pipeline unusable it we are collecting output objects.
Your method works but may have unintended side effects.
Your approach only requires one Try/Catch block to test all steps. The error will tell the exact cause and line of the failure.
Like this:
try {
$svc = get-service alg -ErrorAction Stop
Write-Host 'Service found setting service StartupType' -Fore green
Set-Service $svc -StartupType Automatic -ErrorAction Stop
Write-Host 'Service exists, StartupType set' -Fore Green
}
catch{
Write-Host $_ -Fore Red
}
\(ツ)_/
Saturday, March 2, 2019 9:22 PM
To satisfy those who have a paranoid misconception about "Write-Host"
try {
$svc = get-service alg -ErrorAction Stop
Write-Verbose 'Service found setting service StartupType' -Verbose
Set-Service $svc -StartupType Automatic -ErrorAction Stop
Write-Verbose 'Service exists, StartupType set' -Verbose
}
catch {
Throw # re-throw exception
}
This addresses that issue, preserves the pipeline and preserves the exception.
\(ツ)_/
Saturday, March 2, 2019 9:34 PM
Which allows us to generalize the code as we need
function Set-ServiceStartupType{
Param(
[Parameter(Mandatory)]
$Name,
[Parameter(Mandatory)]
[ValidateSet('Automatic','Manual','Disabled')]
$StartupType,
[switch]$PassThru
)
try {
$svc = get-service $Name -ErrorAction Stop
Write-Verbose 'Service found setting service StartupType'
Set-Service $svc -StartupType Automatic -ErrorAction Stop
Write-Verbose 'Service exists, StartupType set'
if($PassThru){
$svc.Refresh()
$svc
}
} catch {
Throw # rethrow exception
}
}
$alg = Set-ServiceStartupType -Name alg -StartupType Automatic -Verbose -PassThru
\(ツ)_/
Monday, March 4, 2019 8:41 AM
Hi,
Was your issue resolved?
If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
If no, please reply and tell us the current situation in order to provide further help.
Best Regards,
Lee
Just do it.