Unexpected Token with service script

Welcome Forums General PowerShell Q&A Unexpected Token with service script

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

 
Participant
1 month, 1 week ago.

  • Author
    Posts
  • #121024

    Participant
    Points: 90
    Rank: Member

    I wrote a script that manages services on the server and starts/stops etc...depending on the Task in the csv file, as well as changes the startuptype, again depending on the start up type in the csv file.

    CSV file:

    Server,Service,Startup Type,Task
    server1,SQL Server Analysis Services (MSSQLSERVER),automatic,start
    server2,"SQL Server Analysis Services (MSSQLSERVER), SQL Server Analysis Services (MSSQLSERVER) CEIP",Manual,stop

    Script

    $csvFile = Import-CSV .\SSAS_services.csv
    
    $ServiceState = Get-Service -Name
    $ServiceStartupType = Get-Service | select -property name,starttype
    
    ForEach ($row in $csvFile)
    { 
    #checking if service in csv file exists on server
    if (Get-Service $row.Service | Select-Object Name, StartType, Status -ComputerName $row.Server -ErrorAction SilentlyContinue)
    {
    "$row.Service not found on $row.Server!" | out-file .\SSAS_services.txt -Append
    }
    else 
    {
    Get-Service $row.Service | Select-Object Name, StartType, Status -ComputerName $row.Server | select machinename,name | format-table -AutoSize
    }
    
    # Change the service on the server
    
    if ($row.Task -eq "stop" -and $row.Server $ServiceState $row.Service -ne "stop")
    {
    Invoke-Command -ScriptBlock { Stop-Service $using:row.Service } -ComputerName $row.Server -ArgumentList $row
    Start-Sleep 60
    }
    elseif ($row.Task -eq "start" -and $row.Server $ServiceState $row.Service -ne "start")
    {
    Invoke-Command -ScriptBlock { Start-Service $using:row.Service } -ComputerName $row.Server -ArgumentList $row
    Start-Sleep 60
    }
    elseif ($row.Task -eq "pause" -and $row.Server $ServiceState $row.Service -ne "pause")
    {
    Invoke-Command -ScriptBlock { Suspend-Service $using:row.Service } -ComputerName $row.Server -ArgumentList $row
    Start-Sleep 60
    }
    elseif ($row.Task -eq "Restart")
    {
    Invoke-Command -ScriptBlock { Restart-Service $using:row.Service } -ComputerName $row.Server -ArgumentList $row
    Start-Sleep 60
    }
    
    #changing startup type if different
    
    if ($row."Startup Type" -eq $ServiceStartupType -ComputerName $row.Server)
    {
    "Changing Startup Type from '$ServiceStartupType' to $row.'Startup Type'"
    
    Invoke-Command -ScriptBlock { Set-Service $using:row.Service -StartupType $using:row."Startup Type" } -ComputerName $row.Server -ArgumentList $row
    }
    
    } | Tee-object .\SSAS_services.txt -Append

    I am getting the following errors:

    Unexpected token '$ServiceState' in expression or statement. + if ($row.Task -eq "stop" -and $row.Server $ServiceState $row.Serv ...

    Missing closing ')' after expression in 'if' statement. + ... if ($row.Task -eq "stop" -and $row.Server $ServiceState $row.Service ...

    Missing closing '}' in statement block or type definition. + ... sk -eq "stop" -and $row.Server $ServiceState $row.Service -ne "stop") +

    Unexpected token ')' in expression or statement. + ... elseif ($row.Task -eq "start" -and $row.Server $ServiceState $row.Se ...
    ~

    Unexpected token '$ServiceState' in expression or statement. + elseif ($row.Task -eq "start" -and $row.Server $ServiceState $row ... +

    Unexpected token '$row' in expression or statement. + ... -eq "start" -and $row.Server $ServiceState $row.Service -ne "start") + Unexpected token ')' in expression or statement. + ... elseif ($row.Task -eq "pause" -and $row.Server $ServiceState $row.Se ... +
    ~~~~~~~~~~~~~ Unexpected token '$ServiceState' in expression or statement. Not all parse errors were reported. Correct the reported errors and try again. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken ~

  • #121036

    Moderator
    Points: 54
    Team Member
    Rank: Member

    Your IF statement seems off
    You want to fix that for all the if/elseif statements.

    if ($row.Task -eq "stop" -and $row.Server $ServiceState $row.Service -ne "stop")
    # should be:
    if ($row.Task -eq "stop" -and $row.Service -ne "stop")
    
    • #121057

      Participant
      Points: 90
      Rank: Member

      Your IF statement seems off

      You want to fix that for all the if/elseif statements.

      PowerShell
      4 lines

      1
      2
      3
      4
      if ($row.Task -eq "stop" -and $row.Server $ServiceState $row.Service -ne "stop")
      # should be:
      if ($row.Task -eq "stop" -and $row.Service -ne "stop")
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      well i want to compare to the server service state, how will it know from your version what the service status is?

  • #121044

    Moderator
    Points: 54
    Team Member
    Rank: Member

    Also since you use a foreach, rather than a foreach-object, I don't think 'tee-object' will work at the end.
    Either assign a variable to the foreach, or use foreach-object

    $foreach_result = foreach ($something in $stuff){< #do all the things#>}
    $foreach_result | tee-object .\SSAS_services.txt -Append
    # or
    Import-CSV .\SSAS_services.csv | foreach {
    < #Here you'd use '$_' or '$PSItem' rather than '$row' as you want to use the current pipeline value#>
    } | tee-object .\SSAS_services.txt -Append
    
  • #121107

    Participant
    Points: 87
    Helping Hand
    Rank: Member

    The easiest thing to do when you come across a largeish script is to break it down into small chunks. If not the errors can sometimes be too overwhelming trying to find what is breaking where. 1st i would suggest reading up on each cmdlet that you're using so you know how they work. There seems to be some confusion on which parameters go with which cmdlet and that is causing you problems. Just as an example let's look at line 9. Get-Service accepts a -computername parameter but Select-object does not.

    if (Get-Service $row.Service | Select-Object Name, StartType, Status -ComputerName $row.Server -ErrorAction SilentlyContinue)
    

    The if statement is looking for a True or False return but the logic is not quite up to scratch.

    1st part is Get-Service and this is looking for service on the local computer as you have not specified a computername parameter to this cmdlet. This is where your IF code should stop. There is no need to select anything from the get-service cmdlet as we are only wanting to know if the service exists. So it should read

    if (Get-Service $row.Service -ComputerName $row.Server -ErrorAction SilentlyContinue)
    

    Get reading up on each cmdlet and break this down to smaller chunks and look at all your logic and know 100% what your asking PowerShell to do.

     

     

You must be logged in to reply to this topic.