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!