0

By writing a script in PowerShell, I'd like to determine whether a Windows service is running or not running. For this, I have constructed the following script:

#Variables

$winupdate = 'Windows Update'
$running = 'Running'

#Function

function CheckServiceStatus {
param($winupdate)
$getservice = Get-Service -Name $winupdate
if($getservice.Status -ne $running){
    Start-Service $winupdate 
    Write-host "Starting" $winupdate "service"|out-file "C:\Users\ArifSohM\Desktop\Stuff for PowerShell\results.txt"
    }
}

To interpret, what I've tried to do here is simply, create a function called "CheckServiceStatus". Within that function, I have created a parameter and placed the variable of the Windows service name within this parameter. Then, I have placed the "Get-Service" cmdlet into another variable called "$getservice". I then went onto starting an IF statement that is supposed to check if the service is running, so what I've said here is, if the service is NOT running, start the service, create a text file and output a confirmation message into it.

After hitting run on the above script, nothing seems to happen. Am I doing something wrong? Am I missing something? Any help will be much appreciated!

1 Answer 1

1

you are using service display name instead of service name.

the service name of windows update "wuauserv" change the winupdate variable part and you should be fine

$winupdate = 'wuauserv'

function definition should be before calling that function and function call statement

full code:

function CheckServiceStatus {
param($winupdate)
$getservice = Get-Service -Name $winupdate
if($getservice.Status -ne $running){
    Start-Service $winupdate 
    Write-output "Starting" $winupdate "service"|out-file "C:\Users\ArifSohM\Desktop\Stuff for PowerShell\results.txt"
    }
}

#Variables

$winupdate = 'wuauserv'
$running = 'Running'
CheckServiceStatus $winupdate
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your response, I changed it to the suggested name and ran the script, unfortunately, nothing happened. What it does is, when I hit F5 to execute the script, it prints it in the blue container and that's followed by "PS C:\Users\ArifSohM\Desktop. I also checked the folder where the output file should be, there's nothing there.
@MuhammadSohailArif i have updated the full code,check now
Yes, I checked it, it worked, BUT, it didn't print anything in the output file.
@MuhammadSohailArif use write-output instead of write-host ,then i believe it will pass the output through the pipeline ,check and let me know
That didn't work also. But I will mark you answer as complete because it dealt with my main issue. Thank you!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.