PowerShell restarting certain services

Answered PowerShell restarting certain services

  • Tuesday, February 19, 2013 10:41 PM
     
      Has Code

    I've have several Blackberry servers running on Server 2003. I'm trying to write a PowerShell script that will pull all the Blackberry services (display name contains "Blackberry") and restart them. I have two challenges. First, I need to ensure that three of the services are started in a particular order (Router, Dispatcher, Controller) and I'm not sure if there is a better way than setting up a second loop and some crazy filter in the second. Second, the code I have (see below) doesn't pull the services with a startmode equal to Automatic. The $objWmiService doesn't have anything in it.

    What am I missing? Thanks.

    $strClass = "win32_service"
    $strServices = Get-Service -DisplayName Blackberry*
    foreach ($strService in $strServices) {
    	$objWmiService = Get-Wmiobject -Class $strClass -filter "name = '$strService'"
    }
    $objWmiService

All Replies

  • Tuesday, February 19, 2013 10:49 PM
    Moderator
     
      Has Code

    Why retrieve the WMI instance of a service when you already have a ServiceController object?


    PS C:\> get-service -displayname blackberry* | foreach-object { $_.Name }

    Bill

  • Tuesday, February 19, 2013 10:57 PM
    Moderator
     
      Has Code

    Further clarification. This line:

    $strServices = Get-Service -DisplayName Blackberry*

    Doesn't produce a list of strings. It is a collection of matching ServiceController objects. Thus your foreach loop:

    foreach ($strService in $strServices)

    is not iterating a list of strings but rather a collection of ServiceController objects. The use of the "str" prefix for the variable names is not recommended because those variables don't contain strings.

    Bill

  • Tuesday, February 19, 2013 11:01 PM
     
      Has Code

    First of all you should use the Service Name instead of the Display Name of the service

    PowerShell has a built in service start/stop/restart Cmdlet

    Start-Service "ServiceName"
    Stop-Service "ServiceName"
    Restart-Service "ServiceName"

    If you need to start/stop/restart services on a remote computer, you can do the following:

    $objService = Get-Service -Name "ServiceName" -ComputerName "ComputerName"
    Start-Service -InputObj $objService
    Stop-Service -InputObj $objService
    Restart-Service -InputObj $objService


    Best Regards
    Claus Codam
    Consultant, Developer
    Coretech - Blog

  • Wednesday, February 20, 2013 2:18 PM
     
     

    Sorry I wasn't more clear and didn't include part of the code (I haven't written it yet). I always want to restart three of the Blackberry services. Of the remaining Blackberry services, I only want to restart those that have a startmode of Automatic. As I understand it, Get-Service won't tell me if a service is set to automatically start, which is why I was using the WMI query (I couldn't get the WMI query to give me back Blackberry*).

    I chose to use the services' display name because all of the Blackberry services start with "Blackberry", while the service names vary.

  • Wednesday, February 20, 2013 2:48 PM
     
     

    PowerShell would work with display names as well. Each of the above service cmdlets works with display name.

    Here is the code which will get you those blackberry services which have start up mode as auto.

    $services = Get-Service -DisplayName Blackberry* | select -ExpandProperty displayname

    foreach ($service in $services ){

    $startmode = gwmi win32_service -Filter "displayname='$service'" | select -ExpandProperty startmode
    if($startmode -eq "auto"){
    Write-Host "$service is in $startmode start up mode"}

     }

    For starting up the services in order, you can user -contains operator of the serivce display name to match what service you are looking for and use a counter to ensure the order. Give a try yourself once.

  • Wednesday, February 20, 2013 6:08 PM
     
     

    This would restart your blackberry services which are set to auto start mode.

    $services = Get-Service -DisplayName Blackberry* | select -ExpandProperty displayname

    foreach ($service in $services ){

    $startmode = gwmi win32_service -Filter "displayname='$service'" | select -ExpandProperty startmode
    if($startmode -eq "auto"){

    Restart-Service -DisplayName $service
    }

     }

  • Wednesday, February 20, 2013 9:27 PM
     
      Has Code

    I must be running into a limitation of PS v1. When I run:

    $services = Get-Service -DisplayName Blackberry* | select -ExpandProperty displayname

    I get:

    At line:1 char:58 + $services = Get-Service -DisplayName Blackberry* | select <<<< -ExpandProper ty displayname Select-Object : Cannot expand property "displayname" because it has nothing to expand.

    When I remove "-ExpandProperty", I get stuff in $services, but nothing in $startmode.

    • Edited by mhashemi Wednesday, February 20, 2013 9:29 PM
    •  
  • Wednesday, February 20, 2013 9:45 PM
    Moderator
     
      Has Code

    Hi,

    It sounds like a little background in how objects work in PowerShell would be helpful to you.

    Get-Service returns one or more ServiceController objects. Each ServiceController object has properties. You can see what these are by typing:


    PS C:\> get-service browser | get-member
    

    (Browser is just a sample name that exists on every machine.) If you look in that list, you will see that these objects don't have a StartMode property. The Win32_Service WMI class, however, does:


    PS C:\> $service = get-wmiobject Win32_Service -filter "DisplayName LIKE 'computer browser%'"
    PS C:\> $service.DisplayName
    Computer Browser
    PS C:\> $service.StartMode
    Manual
    

    ...etc.

    Bill

  • Thursday, February 21, 2013 3:46 PM
     
      Has Code

    Okay, so I did this: 

    $service = get-wmiobject Win32_Service -filter "DisplayName LIKE 'Blackberry%'"

    I can see that $service is full of stuff (ExitCode, Name, ProcessId, StartMode, State, Status). When I do:

    write-host $service.name

    Nothing is displayed, I'm just kicked back to the prompt. I tried all of the attributes and I didn't get any lists back.

  • Thursday, February 21, 2013 3:52 PM
     
     Proposed Has Code

    Because $service might contain several service object, you will need to go through them all.

    You can do the following

    $service | % { Write-Host $_.service.Name }


    Best Regards
    Claus Codam
    Consultant, Developer
    Coretech - Blog

  • Thursday, February 21, 2013 4:03 PM
    Moderator
     
     Answered Has Code

    If you say:


    $service = get-wmiobject Win32_Service -filter "DisplayName LIKE 'Blackberry%'"
    

    If there are multiple matches, then the $service variable (even though you didn't name it $services) will contain multiple objects. Thus you would need to enumerate them. Try this:


    get-wmiobject Win32_Service -filter "DisplayName LIKE 'BlackBerry%'" | foreach-object {
      $_.Name
    }
    

    When you have a collection of objects, foreach-object will iterate all of the objects in the collection. Inside the script block { } after foreach-object, $_ means "the current object from the collection," so thus $_.Name means "output the Name property for each object in the collection."

    I think if you do some of the PowerShell tutorials it will be easier to understand how to get to what you want to accomplish.

    Bill

    • Marked As Answer by mhashemi Friday, February 22, 2013 10:29 PM
    •