0

im a writing a script in powershell that show services running on a computer and start the services that is not running. My command is the following

Show all services on Powershell with startmode "Auto"

$shse = Get-CimInstance -ClassName win32_service -Computername pshells '
    -Filter "startmode = 'auto'" |
    ConvertTo-Html

Service start all services with status = stopped.

$Service = (Get-CimInstance -ClassName win32_service - Computername pshells '
    -Filter "startmode = 'auto'")

If ($Service.status -eq "Stopped"){
        Start-Service}

Else {}

Convert Output to HTML.

ConvertTo-Html -body "$shse " |

Out-File C:\Scripts\Service.htm

When running this in powershell i get this error

At line:6 char:30
+     -Filter "startmode = 'auto'")
+                                 ~
Unexpected token ')' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordExceptio 
   n
    + FullyQualifiedErrorId : UnexpectedToken
7
  • I can't seem to fix the problem? when removing ')' the whole parameter is not valid Commented Sep 13, 2017 at 8:17
  • As far as I can see there is something wrong with your quote pairs: the single quote behind pshells is not closed. Commented Sep 13, 2017 at 8:32
  • What did you edit Rohit? It is still not valid Commented Sep 13, 2017 at 8:40
  • 3
    The line continuation character is not a ' but a `. Commented Sep 13, 2017 at 8:41
  • 1
    and you have a space between - and Computername Commented Sep 13, 2017 at 8:43

4 Answers 4

1

try this:

Get-Service -ComputerName pshells  | where {$_.StartType -EQ 'Automatic' -and $_.Status -eq 'Stopped'} | Start-Service
Sign up to request clarification or add additional context in comments.

Comments

0

Replace your existing code with this:

$Services =Get-CimInstance -ClassName win32_service -Computername pshells -Filter "startmode = 'auto'"
foreach($service in $Services)
{
    If ($Service.state -eq "Stopped"){Start-Service}
    Else {"Service is already running."}
}

Few things you need to keep in mind , if you wish to use a line continuation then you should use baktick(`) not single-quote(') .

You are getting all the services status and you wish to start them all if stopped. So as a whole it will all the return you as OK since you were checking status. You should check state not status.

As a whole if it is returning and in case any of them is already running, then PS will have conflict in the condition, thats when you should use foreach loop.

Iterate each value you got in $services, check the state and take necessary action and loop over ; proceed with the same for the next one unless the list is exhausted.

Hope it helps you.

Comments

0

On the line:

$Service = (Get-CimInstance -ClassName win32_service - Computername pshells '
    -Filter "startmode = 'auto'")

You aren't using a back-tick (`) you're using a single quote ('). It should be a back-tick to escape the new line.

Comments

0

So i got the command to work, i typed in the following

Do
{

$shse = Get-CimInstance -ClassName win32_service -Computername WIN-9HMIGR3QFE6 -Filter "startmode = 'auto'" |
    ConvertTo-Html

$Service = (Get-CimInstance -ClassName win32_service -Computername WIN-9HMIGR3QFE6 -Filter "startmode = 'auto'")

If ($Service.status -eq "Stopped"){

        Start-Service}

Else {}

ConvertTo-Html -body "$shse " |

Out-File C:\Test\Service01.html

Start-sleep -Seconds 600

}until ($infinity)  

It created an html, where i can look at the services... But there is one problem, when i close down a service, the script is set to start all scripts again after 600 seconds, but service never gets up again.. So now the stopped-start service function is not working correctly

Comments

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.