Why is the if condition not working as intended?

Welcome Forums General PowerShell Q&A Why is the if condition not working as intended?

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
5 days, 16 hours ago.

  • Author
    Posts
  • #128500

    Participant
    Points: 90
    Rank: Member

    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

  • #128521

    Participant
    Points: 259
    Helping Hand
    Rank: Contributor
    • #128523

      Participant
      Points: 90
      Rank: Member

      i just figured it out and posted the answer there as well

       

      I had to do this:

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

You must be logged in to reply to this topic.