locked
Script made to check service status on remote servers and start service if stopped is not working as expected. please assist. RRS feed

  • Question

  • #############################################################
    #
    # This script willl start the BES Client service on servers
    # where if finds the service to be in stopped state and will
    # Put the results in output file.
    #
    #############################################################
    
    $Time = get-date -f "dd-MM-yyy_HH.mm.ss"
    $InputPath = "C:\Users\tarwaniv\Desktop\Remediation\RemediationServers.txt"  
    $LogPath = "C:\Users\tarwaniv\Desktop\Remediation\" +"Log_" +"$Time"+".txt"
    $OutputPath = "C:\Users\tarwaniv\Desktop\Remediation\output.txt" + "RemediationResults_" +"$Time" + ".txt"
    $ppath = "C:\Users\tarwaniv\Desktop\Remediation\pwd.txt"
    $ServerList = Get-Content -Path $InputPath
    
    new-item -type file $Outpath
    #Logging
    Start-Transcript -Path $LogPath 
    
    #Saving Encrypted Credentials
    # (Get-Credential).Password | ConvertFrom-SecureString | Out-File "C:\Users\test\pwd.txt"
    
    $UserName = 'doamin\vtarwani'
    $Password = Get-Content $ppath | ConvertTo-SecureString
    $Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $Password
    
    $Servicename = 'BESClient'
    
        ForEach ($Server in $ServerList) 
        {
             $sess=$sess_status=$ServiceStatus=$Remediation=$null
        
    
            #Establishing the Remote Session
            $sess = Get-Service -ComputerName $Server -Name $servicename
    
            if($sess)
    
            {
            $sess_status = "$Server : Connected"
            Write-Output $sess_status
                $Check_Service = Get-Service -ComputerName $Server -Name $servicename
                if($Check_Service.status -eq "Stopped")
    	    {
    	    Start-Service -InputObject (Get-Service -ComputerName $Server -Name $servicename)
    		$echo "Service has started" >> $OutputPath
    	    if ($Check_Service1.status -eq "Running")
    	    {
    	    Write-Output $Server : Started BES Client service Successfully"
    	    }
    	   else 
    	   { 	
    	   $ServiceStatus = "$Server : Failed to Start the Service, Need manual remediation"
               }
    		} 
    		}
    
    
    
    
    
    
    


    • Edited by Vishal Tarwani Monday, July 13, 2020 12:02 PM Hiding the domain name for security reasons.
    Monday, July 13, 2020 11:56 AM

Answers

  • Why are you not just reusing the $sess variable?

    Also, to manage remote service, try using the Set-Service cmdlet.

    $sess | Set-Service -Status Running


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Monday, July 13, 2020 3:02 PM

All replies

  • What error do you get? What output does it produce, and what output do you expect? 
    Monday, July 13, 2020 12:42 PM
  • Why are you not just reusing the $sess variable?

    Also, to manage remote service, try using the Set-Service cmdlet.

    $sess | Set-Service -Status Running


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Monday, July 13, 2020 3:02 PM
  • Hi,
    As this thread has been quiet for a while, so here is a quick question.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,
    Yang Yang

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, July 23, 2020 5:39 AM