none
Why are all services being changed instead of just the if condition service?

    Question

  • I am writing a script that will be passing in two variables and a csv file to change services on a computer listed in csv file.

    the command will be in this format: ex. start SQLService or stop PBIService

    given csv file:

    server,service

    server1,SQLService

    server2,PBIService

    server3,PBIService

    param($task, $service) #arguments from cmd line input
    
    if($task -eq "start")
    {
        Set-Variable -Name "task" -Value "running"
    }
    elseif($task -eq "stop")
    {
        Set-Variable -Name "task" -Value "stopped"
    }
    
    if($service -eq "SQLService")
    {
        Set-Variable -Name "SQLsvc" -Value "SQLService"
    }
    elseif($service -eq "PBIService")
    {
        Set-Variable -Name "PBIsvc" -Value "PBIService"
    }
    
    Import-CSV .\csvfile.csv |
        ForEach {
            if($_.service -eq "SQLService")
            {
                $getService = Get-Service $SQLsvc -ComputerName $_.Server
                $oldstatus = $getService.status
                $getService | 
                Set-Service -Status $task -PassThru |
                    Select MachineName, Name, Status, 
                        @{n='OldStatus';e={$oldStatus}}
            }
            elseif($_.Service -eq "PBIService")
            {
                $getService = Get-Service $PBIsvc -ComputerName $_.Server
                $oldstatus = $getService.status
                $getService | 
                Set-Service -Status $task -PassThru |
                    Select MachineName, Name, Status, 
                        @{n='OldStatus';e={$oldStatus}}
            }
        } |
        tee output.txt

    However, when i run this for some reason it affects ALL services...

    MachineName                    Name                    Status OldStatus
    -----------                    ----                    ------ ---------
    server1                         SQLService          Running   Running
    server2                         PBIService          Running   Running
    Set-Service : Service 'Microsoft Monitoring Agent Audit Forwarding (AdtAgent)' cannot be started due to the following
    error: Cannot start service AdtAgent on computer 'server3'.
    +             Set-Service -Status $task -PassThru |
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Set-Service], Se
       rviceCommandException
        + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.SetServiceCommand
    
    server3 AdtAgent               Stopped ...ed...}
    Set-Service : Service 'AllJoyn Router Service (AJRouter)' cannot be started due to the following error: Cannot start
    service AJRouter on computer 'server3'.
    +             Set-Service -Status $task -PassThru |
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Set-Service], Se
       rviceCommandException
        + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.SetServiceCommand
    
    server3 AJRouter               Stopped ...ed...}

    Also, why if i command start SQLService, PBIService also starts? the condition isnt working properly for some reason... the only affected server and service in this case should server1 because per if condition, that server has the service = SQLService

    Thursday, December 6, 2018 7:30 PM

Answers

  • Actually, i had to do this, now it works :): 

    Import-CSV .\csvfile.csv | Where-Object Service -eq $Service |

    • Marked as answer by cataster Thursday, December 6, 2018 10:08 PM
    • Unmarked as answer by jrvModerator Thursday, December 6, 2018 10:18 PM
    • Marked as answer by cataster Monday, December 10, 2018 6:06 AM
    Thursday, December 6, 2018 10:08 PM

All replies

  • $SQLSvc is null which will return all service.

    Do this to see:

    Get-Service $null

    Set-Service will not start "ServiceDependsOn"services.; Start-Service will.


    \_(ツ)_/

    • Proposed as answer by BOfH_666 Thursday, December 6, 2018 10:02 PM
    Thursday, December 6, 2018 9:44 PM
    Moderator
  • Actually, i had to do this, now it works :): 

    Import-CSV .\csvfile.csv | Where-Object Service -eq $Service |

    • Marked as answer by cataster Thursday, December 6, 2018 10:08 PM
    • Unmarked as answer by jrvModerator Thursday, December 6, 2018 10:18 PM
    • Marked as answer by cataster Monday, December 10, 2018 6:06 AM
    Thursday, December 6, 2018 10:08 PM
  • Actually, i had to do this, now it works :): 

    Import-CSV .\csvfile.csv | Where-Object Service -eq $Service |

    That won't work correctly either.


    \_(ツ)_/

    Thursday, December 6, 2018 10:18 PM
    Moderator
  • Actually, i had to do this, now it works :): 

    Import-CSV .\csvfile.csv | Where-Object Service -eq $Service |

    That won't work correctly either.


    \_(ツ)_/

    but it does...i tested it thoroughly

    of course i had to edit a couple other things like get rid of the SSASsvc and PBIsvc and just use $service

    Thursday, December 6, 2018 10:19 PM
  • Hi,

    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,

    Lee


    Just do it.

    Monday, December 10, 2018 6:05 AM
    Moderator