How to check if a service is running via batch file and start it, if it is not running?

后端 未结 14 1589
暖寄归人
暖寄归人 2020-11-30 18:15

I want to write a batch file that performs the following operations:

  • Check if a service is running
    • If is it running, quit the batch
    • If
14条回答
  •  Happy的楠姐
    2020-11-30 18:25

    Starting Service using Powershell script. You can link this to task scheduler and trigger it at intervals or as needed. Create this as a PS1 file i.e. file with extension PS1 and then let this file be triggered from task scheduler.

    To start stop service

    in task scheduler if you are using it on server use this in arguments

    -noprofile -executionpolicy bypass -file "C:\Service Restart Scripts\StopService.PS1"

    verify by running the same on cmd if it works it should work on task scheduler also

    $Password = "Enter_Your_Password"
    $UserAccount = "Enter_Your_AccountInfor"
    $MachineName = "Enter_Your_Machine_Name"
    $ServiceList = @("test.SocketService","test.WcfServices","testDesktopService","testService")
    $PasswordSecure = $Password | ConvertTo-SecureString -AsPlainText -Force
    $Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $UserAccount, $PasswordSecure 
    
    $LogStartTime = Get-Date -Format "MM-dd-yyyy hh:mm:ss tt"
    $FileDateTimeStamp = Get-Date -Format "MM-dd-yyyy_hh"
    $LogFileName = "C:\Users\krakhil\Desktop\Powershell\Logs\StartService_$FileDateTimeStamp.txt" 
    
    
    #code to start the service
    
    "`n####################################################################" > $LogFileName
    "####################################################################" >> $LogFileName
    "######################  STARTING SERVICE  ##########################" >> $LogFileName
    
    for($i=0;$i -le 3; $i++)
    {
    "`n`n" >> $LogFileName
    $ServiceName = $ServiceList[$i]
    "$LogStartTime => Service Name: $ServiceName" >> $LogFileName
    
    Write-Output "`n####################################"
    Write-Output "Starting Service - " $ServiceList[$i]
    
    "$LogStartTime => Starting Service: $ServiceName" >> $LogFileName
    Start-Service $ServiceList[$i]
    
    $ServiceState = Get-Service | Where-Object {$_.Name -eq $ServiceList[$i]}
    
    if($ServiceState.Status -eq "Running")
    {
    "$LogStartTime => Started Service Successfully: $ServiceName" >> $LogFileName
    Write-Host "`n Service " $ServiceList[$i] " Started Successfully"
    }
    else
    {
    "$LogStartTime => Unable to Stop Service: $ServiceName" >> $LogFileName
    Write-Output "`n Service didn't Start. Current State is - "
    Write-Host $ServiceState.Status
    }
    }
    
    #code to stop the service
    
    "`n####################################################################" > $LogFileName
    "####################################################################" >> $LogFileName
    "######################  STOPPING SERVICE  ##########################" >> $LogFileName
    
    for($i=0;$i -le 3; $i++)
    {
    "`n`n" >> $LogFileName
    $ServiceName = $ServiceList[$i]
    "$LogStartTime => Service Name: $ServiceName" >> $LogFileName
    
    Write-Output "`n####################################"
    Write-Output "Stopping Service - " $ServiceList[$i]
    
    "$LogStartTime => Stopping Service: $ServiceName" >> $LogFileName
    Stop-Service $ServiceList[$i]
    
    $ServiceState = Get-Service | Where-Object {$_.Name -eq $ServiceList[$i]}
    
    if($ServiceState.Status -eq "Stopped")
    {
    "$LogStartTime => Stopped Service Successfully: $ServiceName" >> $LogFileName
    Write-Host "`n Service " $ServiceList[$i] " Stopped Successfully"
    }
    else
    {
    "$LogStartTime => Unable to Stop Service: $ServiceName" >> $LogFileName
    Write-Output "`nService didn't Stop. Current State is - "
    Write-Host $ServiceState.Status
    }
    }
    

提交回复
热议问题